Vim Cheat Sheet


Basic Commands

i = insert mode

h,j,k,l = move left, up, down, left
w = move from word to word
b = move back word to word
W = jumps the whole word
B = jusmps back a word to word

$ = go to end of the line
^ = go to the begining of the line
0 = goes to the very beginning

gg = goes to the begginig of the file
G = goes to the bottom of the file
{ } = jumps paragraphs

f = finds the first occurence of a string in the file
F = fins the first occurance of a string backward

number followed by comman (4dd) – deletes four lines

3gg = goes 3 lines from the top
:13 = goes to line 13
3 G = goes three lines from the bottom


Advanced Movement

ctrl d – moves half screen down
ctrl u – moves half screen up
ctrl f – full screen forward
ctrl b – full screen backward

M – goes to middle of the window
H – goes to top of window
L – goes to last line of window
3L – goes three lines above bottom of window

zt – moves the line our cursor is on to the top
zb – moves the line to the botton
zz – moves the line to the middle

ma – saves cursor position to a register
‘a – goes directly to register a
’ – move between markers


Basic Editing

X – deletes behind the cursor
x – delete whats under the cursor
dd – delete the line
dw – delete the word
db – delete previous word

cc – delete line and enter insert mode
cw – delete work and enter insert mode
cd – delete previous word and enter insert mode

ci” – changes whats inbetween two “
ca” – deletes everything inside the quotes


Cut, Copy and Paste

Every deletion is a cut command, puts in a special register for pasting later

p – pastes last thing in deletion cut register P – pastes before the cursor y – yank/copy something copy can be used with any familiar command
yw – copy word
yy – copy line


Search

/search = starts searching for word search. it is also case sensitive
?search = looks backwards for the word search
set incsearch = sets incremental search on
set ignorecase = turns off case sensitive search
set hlsearch = turns on highlight search
nohlsearch / noh = turns off highlight search
n = find next occurence. vim stores the last search
c/find = will delete from the cursor to find and then enter insert mode

vim search can use regexs


Search and Replace

:s/pattern/replace/flags
:s – perfoms a substitutions replaces on first occurance by default
:s/vim/Vim – replaces vim for Vim
:%s/vim/Vim – replaces whole file with vim for Vim
:%s/vim/Vim/g – replaces whole file with vim for Vim globally

vim will use previous serach pattern so can do :s//pattern
when using regex can use \ze or \zs to stop selection before or after

Visual mode – v
visual line mode – V

gv – reslects last selection

go to a bracket, enter visual mode hit % and that will find the corresponding bracket

FLAGS

c = confirm, asks for confirmation for oeach occurence
i = insensitive
g = finds all occurances in a line

HELP

:help s – opens the help for a command


Macros and Register

ctrl g – get file info

macros store a command pipeline that can be used over and over

qa – start recording a macro in register a
q – stops recroding
:registers / reg – shows the recorded registers
@a – runs macro in a register
@@ – runts the last macro ran again


Command Line in Vim

:! – initalises comman from the shell then run command
:read !date – puts the oupt of shell into the file under cursor
:r !curl —silent URL – outputs the file into the buffer
you can take the selected text buffer and feed it into standard in and replace it with output


Navigating Between Buffers

:ls – lists all active buffers for files in memory
:bn / bnext – next buffer
:bp / bprev – previous buffer
:b# – last buffer we visited
:bf – first buffer

:bd 14 – deletes buffer 14


Windows and Tabs

Split windows

:vsplit / vs file_path – splits the buffer vertically ctrl+w h/l = move between left right
ctrl+w H / L = move to the left / right most part
ctrl + w J/K = move to the bottom / top
ctrl + w +/– = increase/decrease window hight 1 row
ctrl + w = increase/decrease width by 1 column
ctrl + w = = auto resize windows equally
:q = clost a window buffer

:sb 2 = split buffer in memory
:vert sb = vetical split buffer in memory

Tabs

:tab e / tabedit file_path
:gt – move oen tab forward
:gT – move one tab backward


Graphical Vim

set gfn – sets current font


Indents and Folds

:set list – prnts lines/tabs/space
>> – indent of one tab
8>> – indents following 8 lines
:set expandtab – sets soft tabs
:set noexpandtab – sets tabs normal
:set shiftwidth=3 – 3 spaces for one indentation
:set smartindent – sets smart indentation
:set softtabstop / sts 3

in visual mode –

gv – reselects last selection
>/< – indents

Auto indent = on select texted auto indent
=3j auto indent next 2 lines
=G indents the whole file

In inset mode tab = inserts
ctrl + t = indent forward
ctrl + d = indent back

Folding

manual folds zf5j – fold the next 5 lines
zo – open fold
zO – open from cursor fown
zc – close everything from position up fold
zd – delete fold
zf% – folds the matching bracket
zi – inverts folding on/off

auto folding

set foldmethod/fdm – numberous options see help foldmethod
set fdm=sytax = fold baed on syntax
set fdm=marker = set a custom folder maker } for instace


.vimrc File

:so % – source our current file

set nocompatible – sets vims way of doing things

allows file types to be detected than plugins and indentation set up based on those files

filetype on
filetype indent on
filetype plugin on

let VARIAABLE – “.”
syntax enable

set ignorecase
set hlsearch
set autoindent
set fileencoding=utf-8
set encoding=utf-8
set backspace=indent.eol.start

set smartcase – perform smart searchs
gdefault – perform global replacments
set incsearch
set showmatch – highlight all matching parentheses

deals with how windows are auto sized
set winwidth=84
set winheight=5
set winminheight=5
set winheight=999

set no/list – view all tabs and line endings
set guifont= – sets the font
set guioptions= add options
set guioptions-= remove options
set number – set line numbers
set visualbell – flash the screen over the bell
set cursoline – highlight line your in

set cc=80 – sets line at 80th column
set background=dark
colorschmeme solarized

au BufRead,BufNewFil *.ru setfiletype ruby


Mappings

sequence of characters that triggers sequence of commands