Vim¶
1. Getting started 🧰¶
- Vim motions for beginners (by Dispatch) (video)
- Intermediate Vim motions (by Dispatch) (video)
- Vim graphical cheat sheet & tutorial (highly recommended):
- PDF version (here)
- PDF version (glump.net)
- Web version (viemu.com)
- Writing at the speed of thought (by No Boilerplate) (video)
- Untold story of Vim (by CodeSource) (video)
Cheat sheets¶
2. Configuration¶
To configure Vim, you need to modify the file ~/.vimrc. If it does exist, you
need to create it. A good place to start is the example vimrc that comes with
Vim (example on Matrix):
vim /usr/share/vim/vim91/vimrc_example.vim
These is my recommended setup for ~/.vimrc:
" get the defaults that most users want
source $VIMRUNTIME/defaults.vim
"
" Colors
"
" set colorscheme (some good ones: habamax, retrobox, slate)
if &t_Co > 2 || has("gui_running")
colorscheme habamax
endif
" switch on highlighting the last used search pattern
if &t_Co > 2 || has("gui_running")
set hlsearch
endif
"
" Search
"
" ignore case when the search pattern contains lowercase letters only
set ignorecase
set smartcase
" search while typing (when it's possible to timeout)
if has("extra_search") && has('reltime')
set incsearch
endif
"
" Buffers
"
" allow switching buffers without writing to disk
set hidden
" set key mappings to traverse buffer list
nnoremap <silent> [b :bprevious<CR>
nnoremap <silent> ]b :bnext<CR>
nnoremap <silent> [B :bfirst<CR>
nnoremap <silent> ]B :blast<CR>
"
" Line Numbers
"
" show hybrid (relative+absolute) line numbers
if v:version >= 740
set number relativenumber
endif
"
" Indentation
"
" interpret blanks as tabs
set smarttab
" set default indentation: 2 spaces
" (ts=tabstop, sts=softtabstop, sw=shiftwidth)
set ts=2 sts=2 sw=2 expandtab
" override indentation for Go
autocmd FileType go setlocal ts=4 sts=4 sw=4 noexpandtab
" override indentation for Makefile
autocmd FileType make setlocal ts=4 sts=4 sw=4 noexpandtab
" override indentation for Python
autocmd FileType python setlocal ts=4 sts=4 sw=4 expandtab
Colorschemes¶
Show all installed colorschemes, inside Vim:
enter :colorscheme, then Space, then Tab.
3. Vim essentials¶
Vim has three primary modes. They are like different layers of the keyboard.
Vim modes:
- Normal mode: navigation & operations with the keyboard
- Insert mode: enter text with the keyboard
- Visual mode: select text
After starting Vim we find ourselves in Normal mode.
Going back to Normal mode:
Press the Esc key!
Exiting Vim¶
Go back to Normal mode if you are in Insert mode, and then type :q (this
will show in Vim's bottom line).
If the file was changed then :q will fail, because Vim does not want you to
loose the changes. Your options to exit are:
- save changes and exit with
:wq - discard changes and exit with
:q!
4. Vim - fundamental commands¶
Vim commands are used in Normal mode.
Cursor movement¶
Basic movement:
h # move cursor left
j # move cursor down
k # move cursor up
l # move cursor right
w # jump forwards to the start of a word
b # jump backwards to the start of a word
Additional movement:
0 # jump to the start of the line
^ # jump to the first non-blank character of the line
$ # jump to the end of the line
gg # go to the first line of the document
G # go to the last line of the document
5gg # go to line 5
5G # go to line 5 (same as above)
Ctrl + b # move back one full screen
Ctrl + f # move forward one full screen
Ctrl + d # move forward 1/2 a screen
Ctrl + u # move back 1/2 a screen
Insert mode - inserting/appending text¶
The commands below will switch to Insert mode.
i # insert before the cursor
I # insert at the beginning of the line
a # insert (append) after the cursor
A # insert (append) at the end of the line
o # append (open) a new line below the current line
O # append (open) a new line above the current line
Esc # exit insert mode
Editing¶
The commands below will stay in Normal mode.
dw # delete to the start of the next word
de # delete to the end of the next word
dd # delete the entire line, beginning to end
D # delete the from the cursor to the end of the line
x # delete one character
The commands below will switch to Insert mode.
cw # change (replace) to the start of the next word
ce # change (replace) to the end of the next word
cb # change (replace) to the start of the previous word
c0 # change (replace) to the start of the line
c$ # change (replace) to the end of the line
cc # change (replace) the entire line, beginning to end
C # change (replace) the from the cursor to the end of the line
s # delete single character and substitute text
Repeat and undo edits/commands¶
These make the use of Vim efficient, because we often need to repeat the same operation multiple times.
. # repeat last command
u # undo
Ctrl + r # redo
Search and replace¶
/pattern # search for "pattern"
n # repeat search in same direction
N # repeat search in opposite direction
Next¶
Manage code with Git version control.