Use function abbreviate-file-name
. C-h f
tells us:
abbreviate-file-name
is a compiled Lisp function in files.el
.
(abbreviate-file-name FILENAME)
Return a version of FILENAME
shortened using directory-abbrev-alist
.
This also substitutes ~
for the user's home directory (unless the
home directory is a root directory) and removes automounter prefixes
(see the variable automount-dir-prefix
).
When this function is first called, it caches the user's home
directory as a regexp in abbreviated-home-dir
, and reuses it
afterwards (so long as the home directory does not change;
if you want to permanently change your home directory after having
started Emacs, set abbreviated-home-dir
to nil so it will be recalculated).
If you want to use that in frame-title-format
and mode-line-format
, try this:
(setq-default frame-title-format
'((:eval (list (abbreviate-file-name
(expand-file-name buffer-file-name))
" [%m] - Emacs"))))
(setq-default mode-line-buffer-identification
'((:eval (list (abbreviate-file-name
(expand-file-name buffer-file-name))))))