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.
Use Ctrl-x f to open a file from within Emacs.
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.
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...
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With