SSH in Emacs with TRAMP

I use emacs a lot, sometimes I work on another machine though ssh to edit file. In emacs, you can use TRAMP to do EVERYTHING in remote machine.

Syntax

1
C-x C-f /protocol:user@remote_address#port:/path/to/file
  • protocol : ssh, scp, ftp and even docker
  • user : username to remote in remote machine
  • remote_address : it can be ip address or domain name of remote address
  • port : port of protocol

Example

1
C-x C-f /ssh:root@localhost:/home

When the connection is established, emacs will open a dired - emacs’s file manager for you to interact with remote machine. You can edit file and save, you can open terminal, you can copy file from local machine to remote machine and vice versa.

TRAMP dired mode

If you haven’t use this before, don’t worry, just remember some following keys to use:

  • enter or f : enter directory or open file
  • D : delete
  • C : copy
  • R : move
  • d : change state to delete
  • m : mark
  • u : unmark
  • U : unmark all
  • x : execute
  • ^ : back to parent directory

To open terminal, just call “shell” command

1
M-x shell