Skip to content

Vim pro tips

Copy (yank)#

  • yy - Yank the current line, including the newline character.
  • 3yy - Yank three lines, starting from the line where the cursor is positioned.
  • y$ - Yank everything from the cursor to the end of the line.
  • y^ - Yank everything from the cursor to the start of the line.
  • yw - Yank to the start of the next word.
  • yiw - Yank the current word.
  • y% - Yank to the matching character. By default supported pairs are (), {}, and []. Useful to copy text between matching brackets.

Cut (Delete)#

  • dd - Delete the current line, including the newline character.
  • 3dd - Delete three lines, starting from the line where the cursor is positioned,
  • d$ - Delete everything from the cursor to the end of the line.

Paste#

To put the yanked or deleted text, move the cursor to the desired location and press p to put (paste) the text after the cursor or P to put (paste) before the cursor.

Find and Replace - https://linuxize.com/post/vim-find-replace/#

1
:[range]s/{pattern}/{string}/[flags] [count]

Replace first occurrence of foo with bar in the current line

1
:s/foo/bar/

Replace all occurrences of foo with bar in the current line

1
:s/foo/bar/g

Replace all occurrences of foo with bar in the file

1
:%s/foo/bar/g

You can also use any non-alphanumeric single byte character as a delimiter, which is helpful if you need to look for /

1
:s|foo|bar|

If you want to confirm every replacement, use c as a flag. The following command will ask for each replacement on the current line

1
:s/foo/bar/gc

By default it is case sensitive. Use i as a flag to make it insensitive. The following will replace all occurences of Foo with bar, but including foo, FOO, fOo, etc.

1
:s/Foo/bar/gi

Last update: 2023-05-28 13:02:55