Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save indraniel/09695877e37f8386a578ef8548913d2f to your computer and use it in GitHub Desktop.
Save indraniel/09695877e37f8386a578ef8548913d2f to your computer and use it in GitHub Desktop.
Editing remote files in Vim with SSH

Editing remote files in Vim with SSH

  1. Configure SSH

    In ~/.ssh/config, include the lines:

    Host *
    ControlPath ~/.ssh/sockets/%r@%h-%p
    
  2. Start SSH

    In a shell, start an SSH session to the server on which the file is stored:

    ssh -MNv username@server.example.org &

    💡 Note: If SSH isn't configured to connect to the server without prompting for a password, omit the & symbol from the end of the command above. Also, the command shown in the next step will need to be run in another shell.

  3. Start Vim

    There are two options for specifying the path to the file to be edited:

    💡 Note: If the username on the local computer is the same as the username on the remote server, the username@ portion of the SCP URLs shown below may omitted. If unsure, it's best to include the username as shown.

    1. Using a path relative to the user's home directory

      After SSH has connected, start Vim with an SCP URL for the file to be edited:

      vim scp://username@server.example.org/path/to/personal/file.txt

      Now, any file operations in Vim will be sent over the network by SSH.

      The path to the file given in the example above is relative to the home directory of the username. If the home directory is /home/username, then the file being edited in the example would be /home/username/path/to/personal/file.txt.

    2. Using a path absolute to the server's root filesystem

      To use an absolute path, one that begins at the root filesystem of server.example.org, two slashes must be used between the hostname and the file path ("//"). An absolute path example equivalent to the one above, assuming the home directory is /home/username, would be:

      vim scp://username@server.example.org//home/username/path/to/personal/file.txt

      Another example of an absolute path, using a commonly-available file, /etc/shells:

      vim scp://username@server.example.org//etc/shells
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment