Tips for SSH, VSCode, Vim, iTerm, and more

CS 110 is many students’ first experience working with complex projects on the command line. In this handout, I’m attempting to aggregate resources that may help you become more efficient. I use Mac OS, iTerm, and vim, and unfortunately I don’t know of too many resources for other platforms, but please make suggestions and I’ll aggregate the tools you all find helpful!

General SSH tips

Avoid SSH timeouts

If you let an SSH session sit too long without activity (sometimes even as short as a few minutes), you’ll often get connection issues:

Write Failed: broken pipe

This is easy to mitigate by adding the following to ~/.ssh/config (you may need to create this file if it doesn’t exist):

Host *
    ServerAliveInterval 60

This works on Mac and Linux. It may also apply to Windows, depending on which terminal emulator you’re using (it should apply if you’re using Ubuntu on Windows).

Avoid having to type your password every time

Add this to your ~/.ssh/config, replacing yourSunetId with your Stanford username:

Host myth myth.stanford.edu
    HostName myth.stanford.edu
    User yourSunetId
    ControlMaster auto
    ControlPath ~/.ssh/%r@%h:%p
    ControlPersist yes

Now, you can simply type ssh myth. The first time, it’ll ask you for your password, but if you fire up more SSH sessions after that, it log in without asking for credentials.

This works on OS X, and I believe it should work on Linux as well. If you’re using Ubuntu on Windows, you can try it, but I have no idea if it works.

Terminal emulators

iTerm2 (OS X)

https://www.iterm2.com/

iTerm is a replacement for the Terminal.app that ships with all Macs. I highly recommend it; it’s loaded with way more features than I know how to use, but also designed very well such that those features never get in your way. You can download it and start using it without knowing anything about it in particular. The features I use most often:

Using VSCode

Usually, we recommend editing with vim or emacs, but this can be pretty rough if you live far from California and/or have a bad internet connection. I started using VSCode for work, and it has been a good experience, so I recorded a video on how to use it for CS 110. Feel free to post on Ed if you run into trouble.

Using vim

I think vim is an extremely ergonomic and powerful editor, and even though I use VSCode for most C/C++ projects now, I still use vim mode within VSCode to navigate and edit files. It has a very steep learning curve and can be intimidating to learn, but I highly recommend learning if you have the time.

To make things a little earlier, I recorded a video demonstrating how I use vim and wrote a cheat sheet with the most common features I use.

If you’re interested in using pure vim (instead of vim in VSCode), I highly recommend using some extra plugins to make your life easier. Here are some additional tips for getting more out of vim:

Split screening

Being able to see multiple files at the same time is super, super useful in this class!

For easy split navigation, add this to your .vimrc:

nnoremap <C-J> <C-W><C-J>
nnoremap <C-K> <C-W><C-K>
nnoremap <C-L> <C-W><C-L>
nnoremap <C-H> <C-W><C-H>
set splitbelow
set splitright

Now, to split the screen horizontally (i.e. to have two panes, one above each other), in command mode, type :sp (for “split”). To split the screen vertically (i.e. to have two panes side by side), type :vsp. To move up (assuming you have the above nnoremaps in your config), press ctrl+k; to move down, press ctrl+j; to move left, press ctrl+h; and to move right, press ctrl+l.

Once you are in a split pane, you can load a file using :e filename, or using FZF (see below).

More on splits:

Vundle

The following tips involve vim plugins. Vundle is a plugin manager to make installing things easy. To install Vundle, run the following:

git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim

Then, add the following towards the top of your .vimrc (if any of the following lines are already in your vimrc, you can omit them):

set nocompatible              " be iMproved, required
filetype off                  " required

" set the runtime path to include Vundle and initialize
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()

" let Vundle manage Vundle, required
Plugin 'VundleVim/Vundle.vim'

" Keep Plugin commands between vundle#begin/end.

" ---------- ADD Plugin DECLARATIONS HERE ---------

" All of your Plugins must be added before the following line
call vundle#end()            " required
filetype plugin indent on    " required

My tips below mention Plugin declarations. Add those lines where indicated at the dashed line above. Every time you add a new plugin to your .vimrc, save the .vimrc file and then run :PluginInstall to install the new plugin.

Deoplete (autocomplete)

Deoplete provides autocomplete suggestions, making vim behave a bit more like an IDE.

To use it, you will need vim 8 and Python 3.6.1. All the myth machines are running vim 8, but if you are doing this on your personal computer, you may need to upgrade. (Try brew install vim on a Mac.)

In addition, you will also need to install pynvim:

# On myth, pip isn't installed, so you need to install it. You can probably
# skip this step on your personal machine:
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python3 get-pip.py --user

# Install pynvim:
python3 -mpip install --user pynvim

Then, you can add the following to your vim config:

Plugin 'Shougo/deoplete.nvim'
Plugin 'roxma/nvim-yarp'
Plugin 'roxma/vim-hug-neovim-rpc'
let g:deoplete#enable_at_startup = 1
" deoplete tab-complete
inoremap <expr><tab> pumvisible() ? "\<c-n>" : "\<tab>"

If you can’t use vim 8 for whatever reason, YouCompleteMe is another good autocomplete alternative.

fzf (Fuzzy file finder)

fzf is an excellent plugin for quickly navigating to files without closing vim. You may see me do this in lecture; I press my spacebar twice, enter a few characters that are part of the filename I want, then select the desired file, and voila, it’s open!

It has been a long time since I installed this, and I’m forgetting some details. Try these instructions and please let me know if you run into problems on myth.

Run this in your shell:

git clone --depth 1 https://github.com/junegunn/fzf.git ~/.fzf
~/.fzf/install

Then add this to your .vimrc (see Vundle instructions above):

set rtp+=~/.fzf
Plugin 'junegunn/fzf'
Plugin 'junegunn/fzf.vim'
" This is the default extra key bindings
let g:fzf_action = {
  \ 'ctrl-t': 'tab split',
  \ 'ctrl-x': 'split',
  \ 'ctrl-v': 'vsplit' }

" Default fzf layout
" - down / up / left / right
let g:fzf_layout = { 'down': '~40%' }

" space space to open fzf file finder
let mapleader = "\<Space>"
nnoremap <silent> <leader><space> :Files<CR>

" Enable hidden buffers (don't need to save when switching files)
set hidden

Press “space space” to open fzf. To open a file, hit enter. To split the screen vertically and open the file in the new split pane, press ctrl+v; to split horizontally, press ctrl+x.

ag (Silver Searcher)

ag lets you quickly find files containing a search term. Need to know where the inode struct is defined? Just type :Ag struct inode { and you can easily open ino.h from there.

Again, it has been years since I installed this, and the installation instructions have since changed, so let me know if you run into problems.

You’ll need to install ag first; on Mac, this is just brew install the_silver_searcher, but check the linked README for instructions on other platforms. Then, add this to your .vimrc:

Plugin 'mileszs/ack.vim'
let g:ackprg = 'ag --vimgrep --smart-case'
cnoreabbrev ag Ack
cnoreabbrev aG Ack
cnoreabbrev Ag Ack
cnoreabbrev AG Ack

Airline

This is a really great plugin for formatting the vim display. I have it showing my currently-open files at the top of the screen, and I can press “space 3” to switch to the 3rd “tab.”

Plugin 'vim-airline/vim-airline'
let g:airline#extensions#tabline#enabled = 1
"set airline 
let g:airline#extensions#tabline#tab_nr_type = 1 " tab number
let g:airline#extensions#tabline#show_tab_nr = 1
let g:airline#extensions#tabline#formatter = 'default'
let g:airline#extensions#tabline#buffer_nr_show = 1
let g:airline#extensions#tabline#fnametruncate = 20
let g:airline#extensions#tabline#fnamecollapse = 2
let g:airline#extensions#tabline#buffer_idx_mode = 1

nmap <leader>1 <Plug>AirlineSelectTab1
nmap <leader>2 <Plug>AirlineSelectTab2
nmap <leader>3 <Plug>AirlineSelectTab3
nmap <leader>4 <Plug>AirlineSelectTab4
nmap <leader>5 <Plug>AirlineSelectTab5
nmap <leader>6 <Plug>AirlineSelectTab6
nmap <leader>7 <Plug>AirlineSelectTab7
nmap <leader>8 <Plug>AirlineSelectTab8
nmap <leader>9 <Plug>AirlineSelectTab9

Emacs

Sadly, I know so little about emacs that I’m not sure I even know how to save a file. Please send me recommendations, and I’ll post them here!

IntelliJ

If you’ve never used IntelliJ before, you should check it out; it’s a very powerful and capable editor, and the professional edition is free for students! Do note, however, that it is a pretty big memory hog and tends to slaughter my Macbook Air.

Other fun things