Skip to content

Instantly share code, notes, and snippets.

@carlinmack
Last active February 10, 2024 22:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carlinmack/c148564e24ce1feee8e1575029052e2c to your computer and use it in GitHub Desktop.
Save carlinmack/c148564e24ce1feee8e1575029052e2c to your computer and use it in GitHub Desktop.
VS Code and SSH

Windows

Source: https://code.visualstudio.com/docs/remote/ssh

  1. Ensure VS Code version is >= 1.35 (May 2019)

  2. Install Remote Development through the VS Code marketplace

  3. Install OpenSSH client and server through Settings>Apps & Features>Manage Optional Features

  4. If you are using Pageant and Putty follow these instructions. Ensure your public key is in ~/.ssh/authorised_keys.

    1. Else add this line to settings.json in vscode

      "remote.SSH.showLoginTerminal"*: true,

      Unfortunately you will have to enter your password multiple times. Research can be done from here to find out how to avoid this. It will probably involve running ssh-keygen in the command line.

  5. Either from the command palette (F1) or from the >< Icon in the bottom left run

    1. Remote-SSH: Open Configuration File... > Configure SSH Hosts

    2. Select the first option and add the following template to the config file

      Read more about SSH config files: https://linux.die.net/man/5/ssh_config
      Host name-of-ssh-host-here
          User your-user-name-on-host
          HostName host-fqdn-or-ip-goes-here
          IdentityFile C:\path\to\your\exported\private\keyfile\from\step\3
          ForwardAgent yes
      

      For example

      Host virtualMachine
          HostName dyn822
          User carlin
          ForwardAgent yes
          IdentityFile C:\Users\carlin\.ssh\private
      
  6. Now run Remote-SSH: Connect to Host... > name-of-ssh-host-here. A new VS Code window should open and it should connect to the virtual machine.

Linux

Perform steps 1, 2 and 5

Disconnecting from SSH

As of now the only way I have found of disconnecting is to close the VS Code window. Typing exit into the terminal doesn't work and there doesn't seem to be any options in the Remote-SSH left panel.

Using the Terminal

After opening the new SSH VS code window there should be a panel open with a terminal which says:

Connected to SSH Host - Please do not close this terminal

If you click the plus in the bar above this message, a bash terminal should open at the directory you are currently in. cding won't move your position in the explorer but git etc will work as expected. To open files you can use code path/to/file

Tips and Tricks

  • To perform a hound-like text search in the open directory press Ctrl-Shift-F

  • To find a file by name use Ctrl-P

  • To search for an action to perform (such as Toggle Word Wrap, Format Document, Reload Window etc) use Ctrl+Shift+P or F1

  • To go to a line number use Ctrl-G

Worthy Extensions

  • Git Lens (eamodio.gitlens) - Highly capable git capabilities that allow lots of interesting features like inline blame annotations, multiple repositories, file history, dynamic comparison of branches and much more. Better to stick to command line for actual git commands, but for viewing diffs, blame etc it's priceless

  • Prettier (esbenp.prettier-vscode) - Code formatter for many languages, can be manipulated with JSON. Works best with the editor.formatOnSave setting set to true.

  • Perltidy - Same as above but uses the perl library. These command line arguments work better with Netcraft syntax "perltidy.additionalArguments": ["--cuddled-else", "--all-containers-tightness=2"]

  • Bookmarks (alefragnani.bookmarks) - Useful for large files where changes need to be made across multiple functions, bookmarks can be toggled with a keyboard shortcut and appear in the scroll bar.

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