101

I connect to the remote machine with ssh user@192.168.1.5. 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.

Community
  • 1
  • 1
Zeynel
  • 13,145
  • 31
  • 100
  • 145
  • 1
    What are some examples of commands that do work? (Sometimes different keybindings exist on the remote machine - check your .emacs file on the remote machine - and sometimes an entire key works differently - e.g. GNU screen by default absorbs the [Ctrl-A part of a command](http://superuser.com/questions/422126/remap-ctrl-a-in-gnu-screen) before it can get to emacs.) – kbshimmyo Dec 17 '13 at 01:15
  • 1
    C-space, C-F,C-g work – Zeynel Dec 17 '13 at 01:19
  • I could not find the .emacs file in the remote machine now but I know I did not make any new key bindings in the other machine – Zeynel Dec 17 '13 at 01:24
  • yes, I'm not sure what leads to this set of keybindings not working. Does emacs open the file [in the same mode](http://stackoverflow.com/questions/683425/globally-override-key-binding-in-emacs) on the remote server as it would open it locally (where all the commands have been working)? Perhaps [this](http://stackoverflow.com/questions/3675835/list-all-keybindings-for-a-certain-emacs-mode) could give a clue - `M-x describe-mode`. – kbshimmyo Dec 17 '13 at 01:31
  • Do you also run emacs on your local machine? Is there any reason you want to use the remote emacs vs. a local one? – Carl Groner Dec 17 '13 at 01:33
  • @kbshimmyo yes, I opened it in remote maching the same way from the command line and C-w, M-w, C-y... were all working correctly. – Zeynel Dec 17 '13 at 01:39
  • @CarlGroner I am using the remote laptop as a server and it would be nice to work with config files through ssh, although at this point, I keep the remote laptop close too. But I am planning to buy a server and put it away, in that case I would like to edit files with ssh. – Zeynel Dec 17 '13 at 01:41
  • So... it was an issue of being in a different emacs mode? (not sure if I'm understanding your response correctly.) – kbshimmyo Dec 17 '13 at 14:37
  • Slightly similar question...Is it possible to use eww or w3m or other emacs-browser to open an html file located in a remote-machine? Thanks – Ajned Apr 03 '20 at 19:59

6 Answers6

181

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

Carl Groner
  • 4,149
  • 1
  • 19
  • 20
  • 32
    OP might be interested to know that this functionality is called: `tramp` and it is one of the most useful things you will ever use. It even compiles remotely! – pmr Dec 17 '13 at 12:07
  • This works great and all commands appear to work, but I see that it won't let me edit `index.html` because it says buffer is read-only. But I can edit the same file if I open on the remote computer. By the way, when I opened the file with `C-x C-f /ssh:user@host filename` it asked me the password. – Zeynel Dec 17 '13 at 12:10
  • I tried another file from Documents directory and I could edit that one without problem. – Zeynel Dec 17 '13 at 12:28
  • 1
    Do you know how to specify port with this? – Philip Kirkbride Aug 12 '17 at 21:13
  • 8
    @PhilipKirkbride The full documented syntax is `/method:user@host#port:filename` – Carl Groner Aug 16 '17 at 19:26
  • @CarlGroner This method works for local Unix* machine, right? I cannot make it work on local Windows 7. – KeNVin Favo Jun 07 '19 at 04:00
  • Is it possible to use eww or w3m or other emacs-browser to open an html file located in a remote-machine with this syntax? I've tried it with eww-open-file and got `url-file: File does not exist: file:///ssh:myname@myremote:/path_to_htmlfile.` – Ajned Apr 03 '20 at 20:02
  • I have realized that saving a file on the remote file is pretty slow like `emacs` halts for few seconds, is it possible to do saving more efficiently or at least do it in the background? @CarlGroner – alper Oct 10 '20 at 20:12
38

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

(defun connect-remote ()
  (interactive)
  (dired "/user@192.168.1.5:/"))

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.

abo-abo
  • 20,038
  • 3
  • 50
  • 71
  • This is great. I'll try it. Do you also have an answer for the question I asked in the comment above: I can edit this file `/usr/share/nginx/html/index.html` if I open it in the remote machine; but when I open it with ssh, `index.html` is read-only. Why is this? – Zeynel Dec 17 '13 at 12:36
  • 1
    I'm guessing that you're not connecting with root user, and usually only he can write to `/usr/share/`. – abo-abo Dec 17 '13 at 12:44
  • but how do I connect as root user? I tried a few things like `C-x C-f /sudo ssh:user@host filename` but that does not work. – Zeynel Dec 17 '13 at 12:46
  • 1
    This is a problem that I tried to solve some time ago with no success. I can use tramp to connect to remote, or to connect to local as sudo, but not both. A workaround is to make yourself the owner of `/usr/share/nginx/html/` folder with `chown`. – abo-abo Dec 17 '13 at 12:48
  • I opened the file `/etc/nginx/sites-available/example.com.conf` and edited it and saved it and closed it. Now I opened it again, and this time I cannot edit it? I assume it has to do something with permissions. Can you help me understand why this is happenning? – Zeynel Dec 17 '13 at 16:36
  • I had to change the owner of the file to me... Is this because I am logged in as root in the command line? – Zeynel Dec 17 '13 at 16:50
  • Just look at the file permissions. `C-x d` will show you that. `C-x C-q` can switch in and out of read-only mode, provided that you have the necessary permissions. – abo-abo Dec 17 '13 at 17:02
  • I realize saving on the remote file is pretty slow like `emacs` halts for few seconds, is it possible to do saving more efficiently? – alper Oct 10 '20 at 20:10
  • Shouldn't we have `ssh:` before `user`? – alper Feb 26 '22 at 12:20
19

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.

Drew
  • 29,895
  • 7
  • 74
  • 104
14

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

Alex
  • 301
  • 4
  • 4
4

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!

JimLohse
  • 1,209
  • 4
  • 19
  • 44
  • Hello, thank you for this. It's an excellent solution, much more straight forward than the others suggested here. Only downside I can see is the refresh rate on my client machine. Do you know how I could reduce the latency of refreshing Emacs page as I scroll down the code so that it's not noticeable by the eye that much? Both machines are in the same LAN, but the client is on wifi and the server on ethernet-cable. Thanks again ! – milia May 11 '23 at 04:05
  • 1
    I don't really know, it's an interesting question, not a situation I dealt with. Hopefully someone else will know, you could ask a new question about this. But it feels like it would be better on Stack Exchange for Networking? Which doesn't exist, LOL just Network Engineering. Maybe follow this and as on Super User or ServerFault https://serverfault.com/ (see https://meta.stackexchange.com/a/88305/312650) – JimLohse May 16 '23 at 05:15
  • a colleague suggested `ssh -CX`, which makes things better. A better solution I found by asking in #emacs on LiberaChat IRC server was [xpra](https://www.xpra.org/). The first solution is simple and works fine, the second one is much more powerful. Thank you again for your reply ! Using `xpra` I can even fire up IDEs like Intellij on a terminal from within my server, and the IDE works with minimal latency on my client :)! – milia May 17 '23 at 17:24
3

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.

Drake Sobania
  • 484
  • 3
  • 15