Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 63 You must be signed in to star a gist
  • Fork 11 You must be signed in to fork a gist
  • Save sloanlance/f481b7b8ffc0bfa3f46a1c942c7e7b78 to your computer and use it in GitHub Desktop.
Save sloanlance/f481b7b8ffc0bfa3f46a1c942c7e7b78 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
@ycd
Copy link

ycd commented Feb 3, 2021

Legendary.

@ganindu7
Copy link

Eipc

@abest0
Copy link

abest0 commented Nov 15, 2021

🥲 ...just so beautiful.

@mihaigalos
Copy link

This is awesome.

@dtanphat9388
Copy link

you can use sshfs to mount remote files to local and edit with VIM https://www.youtube.com/watch?v=-0jyrvMl0Ic

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment