Vim Cheatsheet & Basic .vimrc Setup


File under: Tech

A list of helpful everyday Vim commands. I have also included my current .vimrc file at time of writing (note, I primarily work in JavaScript, SCSS/PostCSS, Python, and PHP building web applications).

Vim Keyboard Commands

Navigation

Up
k
Down
j
Left
h
Right
h
Right one word length
w
Left one word length
b
Multi-word navigation

Ex. right 5 words

5w
Multi-line navigation

Ex. up 10 lines

10k
Navigate to a specific line number

Ex. Go to line 8

8G
Go to end of current line
$
Go to start of current line
^
Skip to first occurence of character in a line

Ex. Skip to first “(“

f(
Go to top of file
gg
Go to bottom of file
G
Tab Current Line Forward
>>
Tab Current Line Backward
<<
Page Down
Ctrl + d
Page Up
Ctrl + b

Visual Mode and Selecting Text

Enter Visual Mode
v

Multi-line changes in Visual Mode

Prepend multiple lines with text
  • Enter visual mode: v
  • Select lines to alter
  • Enter Insert Mode: shift + i
  • Insert desired characters
  • Press Esc Esc

Copying & Pasting, Deleting, Undo & Redo

Copy current line
yy
Delete current line
dd
Paste
p
Undo
u
Redo
Ctrl + r

Find & Replace (regex)

Current line:
:s/{pattern}/replacement/
Entire file:
:%s/{pattern]/{replacement}/g

Find & Replace (literal)

Current line:
:sno/{pattern}/replacement
Entire file:
:%sno/{pattern]/{replacement}/g

Folding Commands

Folding mimics the accordion-style functionality of hiding or showing code blocks between brackets in modern IDEs.

Open All Folds
zR
Close All Folds
zM
Open All Folds from Cursor
zO
Open Current Fold at Cursor
zo
Move Cursor to Next Fold
zj
Move Cursor to Previous Fold
zk

.vimrc

set number
set clipboard=unnamed
set autoindent expandtab tabstop=2 shiftwidth=2
set wrap
set linebreak
set nocompatible
set laststatus=2
set foldmethod=syntax
syntax on
set re=0
inoremap jj <ESC>

call plug#begin()
Plug 'sheerun/vim-polyglot'
Plug 'maxmellon/vim-jsx-pretty'
call plug#end()

Non-Vim Helpers

These are not Vim commands, but are other terminal tools I find helpful and use frequently when editing code with Vim.

Find and Replace Across Multiple Files of a Certain Type

Ex. Searching for the term “test” and replacing it with “testReplace” in JavaScript files, recursively.

find . -type f -name "*.js" -exec sed -i " -r"s/test/testReplace/g" {} \;
Searching Across Multiple Files

There are many ways to do this, but this is a command I find myself using frequently that lists all files containing the search string, and returning their path and file name:

Ex. searching for “test” in all files/folders from the current directory down.

grep -rli "test" . 
Tagged: , ,