Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

emacs

ssh

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.

like image 762
Zeynel Avatar asked Dec 17 '13 01:12

Zeynel


People also ask

How do I open emacs files?

Use Ctrl-x f to open a file from within Emacs.

How do I open a remote file in Linux?

enter sftp://host/ and press Enter (replace "host" with your target host) The file system of the remote host will be displayed in file manager now, and you can navigate to your target directory and double-click the file to open it.


2 Answers

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:[email protected]:/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...

like image 183
Carl Groner Avatar answered Nov 29 '22 10:11

Carl Groner


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.

like image 41
abo-abo Avatar answered Nov 29 '22 09:11

abo-abo