Skip to content

Instantly share code, notes, and snippets.

@patriques82
Last active June 4, 2023 08:04
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save patriques82/5774853 to your computer and use it in GitHub Desktop.
Save patriques82/5774853 to your computer and use it in GitHub Desktop.
Vim cheat sheet
Vim commands helpsheet
Contents
My .vimrc commands
Basics
Movement
Search and replacement
Buffers and Windows
Buffers
Windows
Tabs
Edit text
Recording
Argslist
Yanking
Indentation
Information about tabstops
Help
Installing Plugins
Special
Create a command on the fly
Mappings
Plugins
My .vimrc commands:
<leader>bn = previous buffer
<leader>bp = previous buffer
<Leader>c = toggle colorscheme
<C-w>h (jkl) = window nav
<C-t> = number toggle
<leader>tg = generate tag file
<leader>tt = tagbar toggle
<leader>n = nerdtree
fw = fixwhitespace
<Leader>p = ctrlp (files)
<leader>pt = ctrlp (tags)
<leader>pb = ctrlp (buffers)
<leader>pc = crtlp (clear caches)
<leader>gs = git status
<leader>gg = git grep
<leader>gl = extradite
<leader>gd = git diff
<leader>gb = git blame
<Leader>l = easymotion lineforward
<Leader>h = easymotion linebackward
<Leader><Leader>w = easymotion wordsforward
<Leader><Leader>ge = easymotion wordsbackward
Haskell:
tw = insert type
tq = see type of expr
te = clear type of expr
Basics:
VIM has three modes:
i = insert mode- stuff you type is added to the buffer
<esc> = normal mode- keys you hit are interpreted as commands
v = visual mode- allows you to select continuos text
Other
V = visual mode and select whole line
<ctrl-V> = to enter visual mode for blockselections
:set number = to set linenumbers
:q! = force quit
:wq = save and quit
:!<bash command> = execute command
:set guifont=Monaco:h20 = to set bigger fontsize
:cd ~/path/to/working/directory = changes working directory
:pwd = to see working directory
:!mv % /new/path/to/file = move current file to new dir
:e /path/to/file = open file in new buffer (see below "Buffers")
:!rm % = delete current file
Movement:
gg = go to top of file
G = go to end of file
:12 = go to line 12
zz = make current cursor position center of window
ctrl-u = move up half window
ctrl-d = move down half window
H = move to Top (High) of screen
M = move to Middle of screen
L = move to Bottom (Low) of screen
0 = move to beginning of line
^ = move to first non-blank character
$ = move to the end of line
% = move between () {} <>, if cursor is on one
'' = move to last cursor position
3b = 3 words backwards
2w = 2 words forward
ma = will insert a mark 'a' at the current location of the cursor
mz = will inser another mark at another given location
'a = will jump to mark 'a'
'z = will jump to mark 'z'
Search and replacement
gd = will take you to the local declaration.
gD = will take you to the global declaration.
g* = search for the word under the cursor (like *, but g* on 'rain' will find words like 'rainbow').
f# = find next character # on line
3f# = find 3:rd next hashtag on line
; = next #
, = previous #
:/pattern = searches whole file for occurences of pattern
* = searches whole file for word under cursor (forward)
# = searches whole file for word under cursor (backward)
g* = searches whole file for word (inclusive those where it is only part of too).
n = find next occurence of searched pattern
N = find previous instance of searched pattern
:noh = stop highlighting pattern after search
:%s/old\ pattern\./new pattern./gc = replacing all occurences of old pattern with new
1. g = global search for all occurences
2. c = ask for confirmation on search
:s/old/new/g = replaces all occurences of old to new on current line
:5,12s/old/new/g = replaces all occurences between line 5 and 12
Buffers and Windows:
Buffers:
:help buffer = for a review of vim buffers and windows
:sb file = opens a new split window for the buffer
:e file = opens file in new buffer
:badd file = adding buffer for file without opening it
:ls = list buffers (all open documents with unique nr)
:b (part of the filename)<Tab> = autocompletes the buffer name and opens it
:b# = (or <Ctrl-^>) go to alternate buffer (# most recent buffer, % is the current buffer)
:bufdo {cmd} = execute command in all buffers
:bd = delete current buffer and return to previous
:bd 3 5 = delete buffer 3 and 5
:3,5bd = delete buffer 3, 4, and 5
:bd *.php<Ctrl-a> = deletes all php file buffers for example
:ba = buffer all opens a window for all buffers
Windows:
Windows are not Buffers but views onto buffers
:help ctrl-w = to see all useful commands
ctrl-w s = Split window horisontally
ctrl-w v = Split window vertically
:sp file = splits widow horisontally and opens the file in the current window
:vsp file = does the same but vertically
:ba = buffer all opens a window for all buffers
[ctrl-w =] = (without brackets) equalizes all windows to same size
ctrl-w w = cycles through windows
ctrl-w h = Move focus right
ctrl-w j = Move focus down
ctrl-w k = Move focus up
ctrl-w l = Move focus left
ctrl-w r = shifts the windows to the right (all windows must be vertically split)
ctrl-w R = shifts the windows to the left
ctrl-w x = shift the two closest windows
:q = close current window
:only = close all windows but current
Tabs:
:mvim -p first.txt second.txt = open first.txt and second.txt in different tabs
:mvim -p *.txt = same as above but for all .txt files
:tabedit {file} = edit specified file in a new tab
:tabfind {file} = open a new tab with filename given, searching the 'path' to find it
:tabclose = close current tab
:tabclose {i} = close i-th tab
:tabonly = close all other tabs (show only the current tab)
:tab split = copy the current window to a new tab of its own
:tabm 0 = move current tab to first
:tabm = move current tab to last
:tabm {i} = move current tab to position i+1
:tabn = go to next tab
:tabp = go to previous tab
:tabfirst = go to first tab
:tablast = go to last tab
gt = go to next tab
gT = go to previous tab
{i}gt = go to tab in position i
Edit text:
I = insert text at beginning of line
i = insert text at cursor position
a = append after cursor position
A = append after the end of the line
o = open new line in insert mode under cursor
O = open new line in insert mode above cursor
x = deletes character after cursor
X = deletes character before cursor
d2w = delete 2 words
d2e = delete 2 words and put cursor before next word
:-2,+2d = deletes from 2 lines above to 2 lines under cursor position
c2w = change 2 words (same as above but goes into insert mode after)
C = change entire line from the cursor position to end
r9 = replace characters under cursor position with 9
R = start replacing all characters under cursor
:set tw=80 = textwidth of the number of columns of text you want to wrap your text around
(visually mark text) + gq = to make the marked text all wrapped around 80 now.
Recording
qa = to start the recording and save it in register [a].
(...) = do some stuff from command mode (if you want to use tab type it with <ctrl>)
q = to quit the recording
@a = play up the recording
23@a = play up the recording 23 times
Argslist
(from terminal) g $(command): g $(grep *.c | sort) = take all .c files sort them and put in args list in vim
(g is an alisas for vim --remote-silent)
ga = record and make some changes to a file
:n = open up next file in argslist
q = stop recording
25@a = make the same change to all files
:rewind = goes back to the beginning of the argslist
:n = opens next file in the argslist to control that all changes have been made
:p = opens previous file in the argslist
:wall to save the changes in all the files in the argslist
Yanking
v = go into visual mode to select text to yank or delete (cut)
y = yank selected lines from visual mode
y2 = yank two lines
yyp = yank current line and paste it below
:%y+ = yank whole file
P = paste above
:5,7m 21 = move lines 5, 6 and 7 to after line 21
:m+3 = move current line 3 lines down
ctrl + n = autocomplete word if written before, may require you to write it many times.
u = undo
ctrl + r = redo
c = fold codeblock
di" = delete inside quotes
:305,309 s/^/# / = comment out the rows between 305 and 309 with '#' sign
To format text of file to only 32 columns:
:set tw=32
gggqG
Indentation
>> = indent line in normal mode
5>> = indent 5 lines in normal mode
If you for example want to move just the comments of a couple of lines but not the whole line
you could use visual blockselection and move only that (like Sublimes multiple cursors):
<ctrl-V> = mark the block
>> = indent
[==] = (without brackets) autoindents current line in normal mode
[6==] = autoindents 6 lines from current in normal mode
When using autoindentation Vim looks for indentation rules in $VIMRUNTIME/indent directory
for the current filetype (.rb, .py or .php). If it does not find one it will use c-style
indentation rules.
1. Flattening a whole file
ggVG = to highlight all text in visual mode
<... = to move all the selected far left
2. Autoindent a whole file
gg = to move to the top of the file
=G = to autoindent with a motion
> = indent highlighted text in visual mode
Because the vim editor exits visual mode after it has indented you will need enter visual
mode to mark the text again and then redo the same command, therefore when indenting use:
. = repeat previous command
to repeat the same command and indent another step and:
u = to undo the indentation back a step.
You could also mark the same text all over again by
gv = reselects the last highlighted selection in visual mode
and so lets you manage the block in any given way you want.
vi} = to visually select all text with {} as delimiter (selecting text inside the {} )
Information about tabstops
set tabstop=8: defines the width of a tab to 8 columns
expandtab: enables use of spaces instead of tabcharacter when the tabkey is used
noexpandtab: disables it
softtabstop: [insert mode] finetunes of the amount of whitespace used in insert mode
shiftwidth: [normal mode] determines the amount of whitespace inserted or removed in
indentation (shifting a block of code in normal mode with < or >)
If you have expandtab enabled softtabstop and shiftwidth dictates the amount of whitespace
used, but if it is disabled, you´ll notice that the softtabstop and shiftwidth still
dictates the space but only to the point that the inserted space is under the limit of the
tabstop, if you add more than that Vim replaces the spaces with a tab. For not letting the
indentation of normal mode conflict with the tabs of insert mode when using spaces
let shiftwidth == softtabstop.
If you prefer using tabs instead of spaces and dont want to convolute the file
let tabstop == softtabstop
Help:
:h <search term> = search term (opens new buffer for search results)
Installing plugins
To install a Vimball plugin (with .vba extension) all you need to do is open the file in
Vim, and then type:
:source % = installs the current file in the proper directories
:q = quit
This is in the case of the .vba files. The % refers to the current file, so having a file
open in vim is telling it to run the file in the context of vim, therefore it knows where.
That being said, I would recommend instead of doing it this way to use:
Pathogen
Is a plugin manager which is highly recommended. This way you have a structure of where all
plugins reside. All plugins will be located in the subfolder "bundle" in your .vim folder.
Pathogen looks for a bundle folder in .vim and adds the subdirectories of it to the runtime
path. This essentially means each subdirectory of bundle looks like a .vim/ folder to vim.
All you have to do to install a plugin is to get the plugin from wherever, and put it in its
own folder in ..vim/bundle To remove the plugin, just delete the folder from .vim/bundle.
Special:
:options = gives all set options for configurations
:set autochdir = uses files current directory as working directory
:source = reads the contents of a file as a series of commands (:so)
gf = edit filename under cursor
:w !sudo tee % =
1. :w = for writing
2. ! = for executing a bash command
3. sudo tee = for superuser piping the output of command into another filesave
4. % = this current file
vi = in irb (ruby shell) opens vim and lets you write longer programs to test instead of
line-by-line editing (requires you have "gem installed interactive_editor" and created
a ~/.irbrc file with the two lines
"require 'rubygems'" and
"require 'interactive_editor'".
Create a command on the fly
1. Start by in insert mode writing a command on one line.
2. 0i:r = (to turn the current line into a valid :r command),
3. "cdd = (to delete the line into the "c" register) and
4. @c = execute that command.
Mappings (Basics):
To map keys to commands you can put commands in your .vimrc that you wish to map to a certain
keysequence. The general format for mapping is:
{cmd} {attr} {lhs} {rhs} where
{cmd} is one of ':map', ':map!', ':nmap', ':vmap', ':noremap', etc.
{attr} is optional and one or more of the following: <buffer>, <silent>, <expr> <script>,
<unique> and <special>.
{lhs} left hand side, is a sequence of one or more keys that are being mapped.
{rhs} right hand side, is the sequence of keys that the {lhs} keys are mapped to.
Example:
map j gg " Will map the key j to gg, i.e go to the top of the file
map Q j " Will map the key Q to j, i.e also to gg since maps without [nore] are recursive
noremap W j " Will map W to j, but not to anything else
For each of these sets of mappings, there is a mapping that works in all modes (:map and
:noremap), one that works in normal mode (:nmap and :nnoremap), one in visual mode (:vmap
and :vnoremap) and so on.
Plugins:
Surround:
VS<strong> = surround V(isual) selected content with S(urrounded) and indented <strong> tags
viwS<em> = surround v(isual) i(nner) w(ord) with <em> tags
viwS<a href="http://url.com"> = guess what that does?
ciwS<strong> = change surrounding with <strong> tags
dst = delete surrounding tags
vim-trailing-whitespace
:FixWhitespace = Duuh, fixes all whitespace
EasyMotion
<Leader><Leader>w = mark all words in document (below cursor) with triggerletters (goto)
<Leader><Leader>ge = mark all words in document (above cursor) with triggerletters
<Leader><Leader>k = mark beggining of all lines (above cursor) with triggerletters
<Leader><Leader>fo = mark all occurances of char 'o' (below cursor) with triggerletters
CtrlP (Command-T for Vim)
ctrl-p = start fuzzy search for files, buffers and tags.
NERDTree commands
<Leader>n = to open NERDTree (with "cmap nt NERDTree" use :nt instead!)
? = to see all commands, use as well for toggling inside of the NERDTree
<Leader>c<space> = comments out marked textblock (NerdCommenter ?)
vim-multiple-cursors
ctrl-n = marks word under cursor for editing, press ctrl-n again and next occurance of the word
gets highlighted for editing, and now you edit all the words at the same time.
snipMate
In insert mode, for example, you can type
div<tab> = if you are in a html file (.html extension) then this expands to:
<div id="name">
</div>
fn<tab> = if you are in a haskell file (.hs extension) the this expands to:
fn :: a -> a
fn = undefined
You can add snippets to your favorite language yourself by adding them to the snippet file that
exist in your .vim directory in ~/.
Align
In command mode type
:Align -- = and all comments in you haskell file will be aligned.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment