Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save pidb/7d4d65b287cec4583d4d8094fc65e632 to your computer and use it in GitHub Desktop.
Save pidb/7d4d65b287cec4583d4d8094fc65e632 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:

    💡 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://[email protected]/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://[email protected]//home/username/path/to/personal/file.txt

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

      vim scp://[email protected]//etc/shells
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment