how to ssh to remote server and use local emacs to edit files?

5,409

Solution 1

There are various ways to do this, but in generally you are going about this problem backwards. Copy your emacs configs to the remote machine and use emacs local to the files you are editing.

The "various ways to do this" would fall into two categories. First would be ways of mounting a remote drive locally through something like sshfs, some fuse file system, samba, nfs, or any number of other mounting systems to bring file level access to your machine. Second would be various programs that allow you to login and browse a remote system, then operate on files by transferring a copy of them to a local temp file, editing it, then transferring it back. Several programs make that process look pretty seamless, but it's a hack.

Solution 2

I do think that Tramp is what you are looking for. You can use Dired mode to browse the remote directories and look for the file you want.

For example, if I'm looking for some file in /etc/ on a remote machine rem, I'd do the following:

  • Open emacs
  • hit C-x C-f
  • remove the path already present
  • type /rem:/etc/ and hit enter.

You'll get an output like this:

/ssh:rem:/etc:
total 1460
drwxr-xr-x 141 root root    12288 Aug 12 06:46 .
drwxr-xr-x  23 root root     4096 Jul 18 23:48 ..
drwx------   6 root root     4096 Nov 17  2009 .bzr
-rw-------   1 root root      749 Jul 18 23:30 .bzrignore
-rwx------   1 root root    10015 Aug 12 09:17 .etckeeper
drwxr-xr-x   3 root root     4096 Aug  6 00:47 .java
-rw-------   1 root root        0 Nov 17  2009 .pwd.lock

Then you can use e.g. the arrow keys to find the file you wanted to edit. Hit enter when the cursor is on the file to edit it.

Solution 3

Or you can open emacs, run eshell, then cd to the remote machine, like:

cd /hostname:~

Now, run shell commands, navigate to the file you want and open it:

find-file filename
Share:
5,409

Related videos on Youtube

starcorn
Author by

starcorn

Updated on September 18, 2022

Comments

  • starcorn
    starcorn over 1 year

    Is there a way to use the emacs installed my local computer to access remote files I have in a remote server? I have googled and found out that I should use TrampMode however it is necessary for me to know the exact file path. Isn't there a way for me to open an ssh connection to the remote server and from there open the files with the local emacs in my computer?

  • tcoolspy
    tcoolspy about 13 years
    @Gilles: I know nothing of Tramp, so my "hack" alegation must needs not apply. I was particularly thinking of various file manager programs that have menu items like "open in an editor". While these to work to get a file open, they are typically brittle in the way they require the file being copied, modified, copied back, etc. Intermediate file saves before the editor closes are often not pushed upstream making them an awkward way to work on files. If you know a way around the limitations in Tramp @starcorn complained of by all means add another answer!
  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' about 13 years
    With Tramp, the editor is doing the work, so it knows to push when saving and so on. The ideal solution would be to make emacsserver and ssh interoperate, but that's hard. The two workarounds I would have offered are sshfs and eshell, both mentioned already.