Learning Vim

Vim is something I’ve been wanting to learn for a while. I keep hearing great things about it and I work with Linux servers often where Nano is not always installed by default. Up until this point, all I have known how to do in Vim is to edit text and save the file.

Then the YouTube algorithm suggested the video series below where ThePrimeagen goes through the basics in an easy to follow manner. But first I followed his video on setting up the editor.

Setting up Vim

https://www.youtube.com/watch?v=n9k9scbTuvQ
  • rm -rf ~/.vim
  • vim ~/.vimrc
syntax on
set noerrorbells
set tabstop=4 softtabstop=4
set shiftwidth=4
set expandtab
set smartindent
set nu
set nowrap
set smartcase
set noswapfile
set nobackup
set undodir=~/.vim/undodir
set undofile
set incsearch

set colorcolumn=80
highlight ColorColumn ctermbg=0 guibg=lightgrey
python3.7-config
  • Control + shift + v to paste inside of Vim from clipboard
  • mkdir ~/.vim/undodir -p
  • ls ~/.vim
  • Install Vim-plug
  • vim ~/.vimrc
  • Add at the bottom
call plug#begin('~/.vim/plugged')

Plug 'gruvbox-community/gruvbox'
Plug 'jremmen/vim-ripgrep'
Plug 'tpope/vim-fugitive'
Plug 'leafgarland/typescript-vim'
Plug 'vim-utils/vim-man'
"Plug 'lyuts/vim-rtags'"
Plug 'git@github.com:kien/ctrlp.vim.git'
Plug 'git@github.com:Valloric/YouCompleteMe.git'
Plug 'mbbill/undotree'

call plug#end()
  • :source %
  • :PlugInstall
  • I was unable to install rtags on Ubuntu so it’s commented out
  • Edit vimrc again and add the following to the bottom
colorscheme gruvbox                       
set background=dark       
                                                          
if executable('rg')                       
    let g:rg_derive_root='true'                                             
endif                                                                       
                                                                            
let g:ctrlp_user_command = ['.git/', 'git --git-dir=%s/.git ls-files -oc --exclude-staged']
let mapleader = " "                                                         
let g:netrw_browse_split=2                                                  
let g:netrw_banner = 0  
let g:netrw_winsize = 25                                                     
                                                                            
" ag is fast enough that CtrlP doesn't need to cache "                      
let g:ctrlp_use_caching = 0                                                 
  • ~/.vim/plugged/YouCompleteMe/install.py --ts-completer
  • vim ~/.vimrc Add to the end
  • After run :source % again to execute the file
nnoremap <leader>h :wincmd h<CR>                                                
nnoremap <leader>j :wincmd j<CR>                                                
nnoremap <leader>k :wincmd k<CR>                                                                         
nnoremap <leader>l :wincmd l<CR>                                                                         
nnoremap <leader>u :UndotreeShow<CR>                                             
nnoremap <leader>pv :wincmd v<bar> :Ex <bar> :verticle resize 30<CR>                                     
nnoremap <leader>ps :Rg<SPACE>                                                                           
nnoremap <silent> <leader>+ :verticle resize +5<CR>                                                      
nnoremap <silent> <leader>- :verticle resize -5<CR>                                                      
                                                                                                         
nnoremap <silent> <leader>gd :YcmCompleter GoTo<CR>                                                      
nnoremap <silent> <leader>gf :YcmCompleter FixIt<CR>

VIMRC source – https://github.com/zakarybk/learning-code/blob/master/.vimrc

Commands (1/6)

Movement

  • j – line below
  • k – line above
  • h – back one character
  • l – forwards one character
  • w – forwards one word
  • b – back one word

Editing

  • yy – yank(copy) a line
  • y(movement) – yank based on movement (yj – yank below)
  • p – paste copied line onto line below
  • dd – delete(cut) the current line
  • d(movement) – delete based on movement (dw – delete word)
  • u – undo last command
  • i – insert mode
  • esc – leave insert mode

Visual Mode

  • V(shift) – highlight current line (movement to keep highlighting)
  • v – highlight current character (movement to keep highlighting)

File

  • :w – write file
  • :q – quit file
  • :w !sudo tee % – save with sudo access

Notes

Most Vim editors should display the current mode you are in (visual/edit etc) at the bottom.