Vim

See also

Vin 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 –

Vim commands

General

:q                  # quit
:w                  # write
:x                  # quit and write
:e <file>           # edit <file>

Edition

y                   # copy
d                   # cut

Note

You can compose commands. Example with y:

yy                # copy current cursor line
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>

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

Warning

If you only have vim-tiny, try to disable compatible mode.

Split screen

<Ctrl+wn>            # open new file
<Ctrl+ws>            # horizontal split
<Ctrl+wv>            # vertical split
<Ctrl+ww>            # switch between split screens

Vimrc file

Note

All changes are made in ~/.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 [1]

# 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

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

Screenshot

../_images/vim.png

Vim with NERDTree menu

Footnotes

[1]See modeline wiki page.