8

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.

Drew
  • 75,699
  • 9
  • 109
  • 225
sds
  • 5,928
  • 20
  • 39

1 Answers1

9

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.

Drew
  • 75,699
  • 9
  • 109
  • 225