📘 Vim#
Overview
vi is a screen-oriented text editor originally created for the Unix operating system.
Vim (Vi IMproved) is a clone, with additions, of vi text editor program for Unix.

Links
Vim modes#
Name |
Cmd |
Description |
|
|---|---|---|---|
normal
|
<Esc>
<Ctrl-c>
|
back to normal mode
idem
|
Text manipulation mode:
searching, copying, saving commands
are available.
|
insert
|
i
a
o
I
A
O
|
insert before cursor
append after cursor
open new line above
insert at beginning of line
append at end of line
open new line below
|
Text edition mode:
edit your text as usal.
– INSERT –
– INSERT (paste) –
|
visual
|
v
V
<Ctrl-v>
|
visual mode
visual mode line
visual mode block
|
– VISUAL –
– VISUAL LINE –
– VISUAL BLOCK –
|
replace
|
R
|
replace mode
|
– REPLACE –
|
Tip
Access manual page with help command :
:help vim-modes
:help registers
:help operator
:help tabstop
Vim commands#
General#
:q # quit
:w # write
:x # quit and write
:e <file> # edit <file>
Search#
/pattern # search pattern
n # next search pattern
N # previous search pattern
# # search word under cursor
:noh # clear last search highlighting
Edition#
c # change
d # delete
y # yank into register
Note
You can compose commands. Example with y:
yy # copy current cursor line
yl # copy current cursor character
y$ # copy current cursor to end of line
y0 # copy current cursor to beginning of line
y<number>y # copy <number> lines from current line
yG # copy current cursor to end of file
p # paste below cursor
P # paste above cursor
. # repeat previous command
x # cut current character
r <char> # replace current character by <char>
u # undo
<Ctrl+r>,U # redo
:%s/pat/rep/gc # replace pat by rep globally with confirmation request
:10,20s/pat/rep/gc # idem between line 10 and 20
:10,20w <newfile> # write line 10 through line 20 as <newfile>
:10,20w>> <newfile> # append line 10 through line 20 to <newfile>
:read <file> # insert <file> content under cursor
:read !<cmd> # insert <cmd> output under cursor
:<num>read ... # insert to line <num>
Configuration#
:se nu # show line numbers
:set number # idem
:se paste # enable insert paste mode
:se mouse=a # enable mouse visual mode
:se mouse-=a # disable mouse visual mode
:se nocp # disable compatible mode
:se ai # enable auto-indent
:se ts=4 # <Tab> Number of spaces
Warning
If you only have vim-tiny, try to disable compatible mode.
Split screen#
<Ctrl+wn> # open new file
<Ctrl+ws> # horizontal split
:split # idem
<Ctrl+wv> # vertical split
:vsplit # idem
<Ctrl+ww> # switch between split screens
Registers#
+- The unnamed register “” (default)
+- 10 numbered registers “0 to “9
+- 26 named registers “a to “z or “A to “Z
"ayy # copy current line in register "a
"ap # paste register "a
"bd2d # delete two lines registered in "b
"bp # paste register "b
Configuration file, .vimrc#
Base#
Enable syntax highlight, modeline, disable mouse visual
syntax on
set tabstop=4
set modeline
set modelines=5
set mouse-=a
Note
Modeline usage
# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
# vim: ft=jinja
# vim:syntax=apache filetype=apache
# vim: set expandtab ts=4 sw=4:
Withespace#
Highligt trailing whitespace
" highlight trailing whitespace in red
" have this highlighting not appear whilst you are typing in insert mode
" have the highlighting of whitespace apply when you open new buffers
highlight ExtraWhitespace ctermbg=red guibg=red
match ExtraWhitespace /\s\+$/
autocmd BufWinEnter * match ExtraWhitespace /\s\+$/
autocmd InsertEnter * match ExtraWhitespace /\s\+\%#\@<!$/
autocmd InsertLeave * match ExtraWhitespace /\s\+$/
autocmd BufWinLeave * call clearmatches()
Pathogen#
Execute pathogen
execute pathogen#infect()
NERDTree#
Ctrl-N to toggle left menu, open left menu if no file or directory
"" open automatically NERDTree
" autocmd vimenter * NERDTree
"" open automatically NERDTree if no file specified
autocmd StdinReadPre * let s:std_in=1
autocmd VimEnter * if argc() == 0 && !exists("s:std_in") | NERDTree | endif
"" open automatically NERDTree if directory
autocmd StdinReadPre * let s:std_in=1
autocmd VimEnter * if argc() == 1 && isdirectory(argv()[0]) && !exists("s:std_in") | exe 'NERDTree' argv()[0] | wincmd p | ene | endif
"" <Crtl-n> to open NERDTree
map <C-n> :NERDTreeToggle<CR>
Vim plugins#
Pathogen#
Install pathogen
mkdir -p ~/.vim/autoload ~/.vim/bundle && \
curl -LSso ~/.vim/autoload/pathogen.vim https://tpo.pe/pathogen.vim
Vim-sensible#
Install sensible
git clone https://github.com/tpope/vim-sensible.git ~/.vim/bundle/vim-sensible
Vim-airline#
Install airline
cd ~/.vim/bundle
git clone https://github.com/vim-airline/vim-airline
NERDtree#
Installation#
Install nerdtree
git clone https://github.com/preservim/nerdtree ~/.vim/bundle/nerdtree
Nerdtree commands#
? #toggle help
Files
o # open in prev window
go # preview
t # open in new tab
T # open in new tab silently
i # open split
gi # preview split
s # open vsplit
gs # preview vsplit
Directories
o # open & close
O # open recursively
x # close parent
X # close all children recursively
e # explore selected dir
Bookmarks
o # open bookmark
t # open in new tab
T # open in new tab silently
D # delete bookmark
Filesystem
C # change tree root to selected dir
u # move tree root up a dir
U # move tree root up a dir but leave old root open
r # refresh cursor dir
R # refresh current root
m # Show menu
cd # change the CWD to the selected dir
CD # change tree root to CWD
Tree Navigation
p # go to parent
P # go to root
K # go to first child
J # go to last child
<C-k> # go to prev sibling
<C-j> # go to next sibling
Tree filtering
I # hidden files (off)
f # file filters (on)
F # files (on)
B # bookmarks (off)
Vim-jinja#
Install vim-jinja
git clone https://github.com/lepture/vim-jinja.git ~/.vim/bundle/vim-jinja
