Vim view

Updated: 01 July 2023

Write a Vim script that restores the contents of the current window

:mkview

Directory which stores files for :mkview

:set viewdir?

Load the view for the current file

:loadview

Vim global

Updated: 31 May 2023

Execute [cmd] (default :p) on lines within [range] where {pattern} matches

:[range]g[lobal]/{pattern}/[cmd]

Delete blank lines

:g/^$/d

Vim key mappings

Updated: 21 May 2023

Remove all text from line, remain in normal mode

nmap <Leader>S S<Esc>
nmap <Leader>cc cc<Esc>

Add blank line above or below current line

nmap <Leader>O O<Esc>j
nmap <Leader>o o<Esc>k

Scroll left and right without moving cursor

map <C-L> 5zl
map <C-H> 5zh

Vim motions

Updated: 15 May 2023

) Sentence forwards.

( Sentence backwards.

} Paragraph forwards.

{ Paragraph backwards.

S Delete contents of a line, go to start, stay in Insert mode.

C Delete to end of line and enter INSERT mode.

0 Start of line, stay in NORMAL mode.

$ End of line, stay in NORMAL mode.

#cc Change # lines.

ciw Change in word.

Vim substitute

Updated: 16 March 2024

Capture groups

Given the following

one foo one
one bar one

this substitution :s/one\(.*\)one/two\1two/ yields

two foo two
two bar two

Replace text with a new line

Given the following

phpdoc_separation, blank_line_before_statement, ordered_imports
class_attributes_separation, phpdoc_summary, phpdoc_separation

this substitution :s/, /\r/g yields

phpdoc_separation
blank_line_before_statement
ordered_imports
class_attributes_separation
phpdoc_summary
phpdoc_separation

Case

Use I. The following will replace stays but not Stays

:%s/stays/country/gI