Coffee and Contemplation A developer blog

Vimming in 2020

If you use vim, you can use h,j,k,l keys to navigate instead of the arrow keys. You won’t have to move your hands away from the home row, and you’ll end up saving a lot of time.

One of my seniors in college told me this about vim. It sounded so incredibly ridiculous at that time, that I decided to give it a shot.

For the first couple of years, my .vimrc looked like this.

set nu
set cindent
syntax on

This enabled line numbers, indentation and added some syntax highligting to the code.

My real journey into vim started with this post titled “How I boosted my vim” by Vincent Driessen. It introduced me to vim’s plugin ecosystem and I found out there is more to vim than h,j,k,l.

10 years later, my (neo)vim looks like this: neovim screenshot

Basic setup

Depending on if you are using vim or neovim, startup file is different. For vim, it is ~/.vimrc. For neovim, it is ~/.config/nvim/init.vim. Add the following block into your startup file.

filetype plugin indent on
syntax on

set modeline
set encoding=utf-8
set termencoding=utf-8
set tabstop=4                   " a tab is four spaces
set expandtab                   " Use spaces to insert a <TAB>
set backspace=indent,eol,start  " allow backspacing over everything
                                " in insert mode
set autoindent                  " always set autoindenting on
set copyindent                  " copy the previous indentation
                                " on autoindenting
set number                      " always show line numbers
set shiftwidth=4                " number of spaces to use
                                " for autoindenting
set shiftround                  " use multiple of shiftwidth when
                                " indenting with '<' and '>'
set smarttab                    " insert tabs on the start
                                " of a line according to
                                " shiftwidth, not tabstop
set hlsearch                    " highlight search terms
set incsearch                   " show search matches as you type
set ignorecase                  " ignore case when searching
set smartcase                   " ignore case if search pattern is all
                                " lowercase, case-sensitive otherwise
set scrolloff=8                 " Start scrolling at the last 8 lines
set history=1000                " remember more commands and search history
set undolevels=1000             " 1000 levels of undo
set title                       " change the terminal's title
set noerrorbells                " don't beep
set wildmenu                    " Better command line completion
set wildmode=list:longest,full
set nobackup                    " no backup files
set nowritebackup
set noswapfile                  " no swap files
set nolazyredraw
set hidden
set cmdheight=2                 " Better display for messages
set updatetime=300
set inccommand=nosplit          " Preview for search and replace
                                " (neovim only)

set pastetoggle=<F2> " F2 Toggles set paste
au InsertLeave * set nopaste " Exit paste mode when leaving insert mode

" No shift for :
nnoremap ; :

" Navigate wrapped lines easily
nnoremap j gj
nnoremap k gk

" Remove highlight on ,/
nmap <silent> ,/ :nohlsearch<CR>

" Correct typos
aug FixTypos
    :command! WQ wq
    :command! Wq wq
    :command! QA qa
    :command! Qa qa
    :command! W w
    :command! Q q
aug end

If you would like to know more details about any of these settings, you can do a :h <command>. For eg:- :h autoindent

Plugins

vim-plug by Junegunn Choi is a good candidate for a plugin manager.

Install vim-plug.

For vim in osx, linux

curl -fLo ~/.vim/autoload/plug.vim --create-dirs \
    https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim

For neovim in osx, linux

sh -c 'curl -fLo "${XDG_DATA_HOME:-$HOME/.local/share}"/nvim/site/autoload/plug.vim --create-dirs \
       https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim'

For others, see here. When you start vim next, the vim-plug will be installed.

Now, let us install a plugin called lightline. The URL for lightline on github is https://github.com/itchyny/lightline.vim. You need to use the url slug for the plugin to install it; like shown below.

call plug#begin('~/.vim/plugged')

Plug 'itchyny/lightline.vim'

call plug#end()

To install the plugin, quit vim and run vim +PlugInstall. When you restart vim, you can see a better status line(the line the bottom of vim).

vim-plug provides some commands to manage plugins. Here are some I use often.

  1. PlugInstall - Installs all the listed plugins in vimrc.
  2. PlugUpdate - Updates all the listed plugins in vimrc.
  3. PlugClean - Removes unlisted plugins.