Skip to content

Vim

Write to read-only file

:w !sudo tee "%"

Delete any number of lines

Shift+V from point of cursor and press D key to delete.

Search and replace

:%s/search/replace/g

Disable search highlight

:noh

Prepend multiple lines with a character

  1. Shift+V from point of cursor to enter Visual Block mode
  2. Move up/down to select multiple lines
  3. : to enter command mode
  4. s/^/#/g to replace first character on the lines with a #

The prompt will look something like this when all done:

:'<,'>s/^/#/g

Format to certain column width

  1. Set text width to desired width:

    :set textwidth=80
  2. Move to start of file:

    gg
  3. Format text from start to the end:

    gqG

Measure start-up speed

Terminal window
$ hyperfine "nvim --headless +qa" --warmup 5
Benchmark 1: nvim --headless +qa
Time (mean ± σ): 40.3 ms ± 1.8 ms [User: 16.1 ms, System: 8.0 ms]
Range (min … max): 37.2 ms … 46.3 ms 58 runs

Re-indent according to file type

  • A single line: ==
  • A range of lines: <range>==
  • Entire file: gg=G
    • Probably won’t be as intelligent as a language-specific formatter

Reverse lines

:g/^/m0

More information here.

Use visual mode to selectively reverse ((:g|:global) command is still required).

Useful for re-ordering commits with git rebase -i if you know you made the most relevant commit first and subsequent minor changes afterwards.