Given ~/foo/bar, I can get /home/sds/foo/bar using expand-file-name.

However, given /Users/sds/foo/bar, how do I get ~/foo/bar?

Use case: I want to copy a file between computers and they have different physical paths to my home directory.

1 Answer 1


Use function abbreviate-file-name. C-h f says:

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`).

See also the Elisp manual, node Directory Names.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.