Skip to content

Vim

Commands

Text processing

Replace the word in vim

<Esc>:%s/regular_expression/replacement/g<Enter>

Replace the word in vim from line 10 to line 15

<Esc>:10,15s/regular_expression/replacement/g<Enter>

Replace the word with case sensitive in vim

<Esc>:%s/\Cregular_expression/replacement/g<Enter>

Add a new character at the beginning of the line

<Esc>:%s/^/#<Enter>

Add a new character at the end of the line

<Esc>:%s/$/,<Enter>

Delete lines containing the pattern globally.

<Esc>:g/pattern/d<Enter>

Delete the '\n' at the end of lines

<Esc>:s/\n/ <Enter>

Add the '\r' at the end of lines

<Esc>:s/$/\r<Enter>

Delete the content from 'keyword' to the end of lines

<Esc>:s/keyword.*/<Enter>

Delete the content from 'keyword' in one line

<Esc>:s/keyword\s//g<Enter>

Remove the content from the beginning of the line up to the pattern.

[^.]: This is a character class that matches any single character except for the dot (.). Square brackets define a set of characters, and ^ (inside square brackets) signifies negation, so [^.] means "any character that is not a dot."

<Esc>:%s/^[^.]*/<Enter>
Back to top