I'm setting up a remote editing capability through Tramp from my Mac OS X laptop to an Ubuntu box, and have defined the following:

(setq tramp-default-user "myusername"
      tramp-default-host "myhostname")
(setq tramp-default-method "ssh")

;;Open a file on my linux box
(defun remote-find-file (file-name)
  "Like find file, but opens the file on my box"
  (interactive "FRemote Find File: ")
  (let ((tramp-file-name (expand-file-name file-name)))
    (find-file tramp-file-name)))
(global-set-key (kbd "C-x C-u") 'remote-find-file)

The issue I have is that expand-file-name is expanded using the Mac OS X conventions, so that ~ turns into /Users/myusername instead of /home/myusername. I suspect there's a way to fix this using file name completion, but I can't figure it out from the Tramp documentation.

  • Do you need to expand-file-name at all? I think TRAMP works fine if you pass the name unexpanded.
    – PythonNut
    Apr 20 '16 at 20:28
  • I guess I should have thought of that. Duh. Thanks.
    – rogerl
    Apr 20 '16 at 22:48

expand-file-name doesn't use any convention, it asks your operating system what the home directory of the user is. It can't guess that you want the home directory of a user with the same name but on a different machine.

In any case, your function doesn't do anything useful as it stands. To open a file in your home directory on the remote machine, type


Put a slash after /myhostname: to give an absolute path on the remote host.

You can abbreviate myhostname by defining an alias in your SSH configuration (example), so you'd have very little to type — with a one-letter alias, you'd just need to type C-x C-f /r: and then the file name to open a remote file. So I don't think the command you're trying to define is useful anyway.

  • Thanks, that's a much better solution. Now, can I also edit as superuser?
    – rogerl
    Apr 21 '16 at 0:21
  • @rogerl Yes, but it's more verbose: /ssh:myhostname|sudo::/path/to/file (but you can define a shortcut for it). Apr 21 '16 at 8:50
  • Thanks. I had discovered the first syntax, but couldn't figure out from the documentation you pointed to how to actually set up a shortcut (I'm not really a LISP hacker). Would you mind putting an example in your answer?
    – rogerl
    Apr 21 '16 at 12:30
  • @rogerl This is getting to deviate quite a lot from your original question, please post a new question. Apr 21 '16 at 12:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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