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

Vim surround

Updated: 17 May 2023

Surround a single character with square brackets, no additional space

ysl]

Surround a visual selection with backticks. Make the selection then

S`

For “Hello world!”, change double quotes to single quotes

cs"'

For <em>foo</em>, change tags to double quotes

cst"

Vim run bash command

Updated: 01 January 2023

Use !, for example to tidy text into tabulated columns

notes.txt 101 KB root
a-longer-filename.sh 2 KB chris
1.jpg 16.37 MB www-data

Type command

:!column -t

Result

notes.txt             101    KB  root
a-longer-filename.sh  2      KB  chris
1.jpg                 16.37  MB  www-data

Vim searching

Updated: 07 November 2023

:grep uses external grep command.

:vimgrep uses vim’s internal grep facility.

:lgrep Same as :grep except the location list for the current window is used instead of the quickfix list.

Word boundaries

/\<word\>

* search for word currently under cursor.
[I show lines with matching word under cursor.