What is the best way to open remote files with emacs and ssh

EmacsSsh

Emacs Problem Overview


I connect to the remote machine with ssh [email protected]. When I need to open a file in the remote machine I do, e.g.,

emacs /usr/share/nginx/html/index.html

and that opens the index.html file in the shell. I noticed that some emacs commands work but others do not work. For instance, C-w does not work; M-< does not work. How can I fix this, and what is the best way to work with emacs and ssh?

I found this question but it made me more confused.

Emacs Solutions


Solution 1 - Emacs

I generally prefer opening remote files from a local Emacs instance.

While running Emacs on your local machine, opening a remote file over ssh is not much different than opening any other file besides a slightly different syntax.

For ssh, you can type C-x C-f. Now, in the minubuffer you want to type /ssh:user@host:/path/to/file (Note that tab completion will work once you start typing a path. Also note the leading / character). See the full docs here.

In your example, that would be:

C-x C-f /ssh:user@192.168.1.5:/usr/share/nginx/html/index.html

Now you can edit remote files over ssh in Emacs while using your local configuration and any installed packages, etc...

Solution 2 - Emacs

Just to add to the answer above, you can write shortcuts for machines that you use frequently:

(defun connect-remote ()
  (interactive)
  (dired "/[email protected]:/"))

This will open a dired buffer on a remote machine. You can navigate this buffer as you would a local one.

If you have set up ssh keys for the remote machine, you don't even have to enter the password.

If you have a bunch of remote machines, you can give some recognizable name to each function, e.g. connect-cupcake, connect-kitkat and use smex package for completion.

Solution 3 - Emacs

And to add to @abo-abo's post about "shortcuts" --

Use Emacs bookmarks. Just create bookmarks normally, when you visit a remote file or directory. Then just use C-x r b to jump to a remote bookmark, whose name you provide (with completion).

If you use Bookmark+ then remote bookmarks are highlighted specially in the *Bookmark List*, so you can recognize them more easily. And remote bookmarks that must be accessed by su or sudo (root) are highlighted differently.

If you use Dired+ then you can also quickly bookmark multiple remote files or directories, by visiting their containing remote directory in Dired, marking them, and hitting C-x b. No need to give the bookmarks names; they are named after the files. Even if you never use those bookmarks for navigating to the remote files, you can use them with Bookmark+ tags to organize the files and thus operate on subsets of them.

If you use Icicles then whenever you use a command to jump to a bookmark, you can narrow the completion candidates to those that are remote by hitting C-M-@ during completion.

Solution 4 - Emacs

The original poster expressed interest in opening remote files as the root user. This can be done with the command:

C-x C-f /ssh:you@remotehost|sudo:remotehost:/path/to/file RET

More documentation can be found here: https://www.emacswiki.org/emacs/TrampMode#toc14

Solution 5 - Emacs

A Simple Answer that focuses on the remote machine:

If I plan to do all my emacs work on the remote machine, I use

ssh -X username@hostname

and then run emacs in the remote session, displaying back on my local machine. It's an old question but I wanted to throw this in for completeness. Granted there are some xhost / X config issues but in many networks this will work right off the bat!

Solution 6 - Emacs

SSH mode for emacs is what you're looking for.

Once you have it set up you just run

M-x ssh RET hostname RET

Then it prompts you for your password twice (once for the command line, once for loading files).

For the most part you can treat it like any other shell (non-interactive and a few minor differences, but that's it).

It keeps track of which directory you're in, so when you want to open a file from the directory you're looking at it automatically starts in the right directory and you just need to enter in the file name.

Emacs Wiki has more info too.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionZeynelView Question on Stackoverflow
Solution 1 - EmacsCarl GronerView Answer on Stackoverflow
Solution 2 - Emacsabo-aboView Answer on Stackoverflow
Solution 3 - EmacsDrewView Answer on Stackoverflow
Solution 4 - EmacsAlexView Answer on Stackoverflow
Solution 5 - EmacsJimLohseView Answer on Stackoverflow
Solution 6 - EmacsDrake SobaniaView Answer on Stackoverflow