Skip to main content
  1. Notes/
  2. Vim/

Vim tips

230 words
Table of Contents

Put a ‘c’ after a substitute command
#

Not always needed but does help keeping control of your changes. If in stead of this:

s/foo/bar/g

You write this:

s/foo/bar/gc

Then vim will ask for confirmation on every change. This can help prevent you mutilating a document entirely.

vim-airline
#

If like me you mostly use vim through a terminal that by default does not use the xterm-256color terminal type using the vim-airline plugins may not be what you want.

My solution:

Non greedy match
#

If you need the shortest match between quotes or whatever adding .\\{-} to the pattern turns the match from greedy to non greedy.

Take the following line:

item1="1" item="2"

A normal substitute like this:

s/\".*\"/foo/

Will result in:

item1=foo

Changing the substitute with .\{-}:

s/\".\{-}\"/foo/

Will result in:

item1=foo item="2"

See: nongreedy for more information.

Move matching lines to bottom
#

  • v [command]: inverse match
  • normal [action]: Execute Normal mode commands
  • dd [command]: delete (cut) command
  • G [command]: go to the end of the file
  • p [command]: paste line in buffer

Match a pattern and uppercase the match
#

  • s [command]: substitute
  • \(<pattern>\) [pattern]: store the patterns results for use in the replacement.
  • .\{-} [pattern]: non greedy match
  • \> [pattern]: end of a word
  • \U [action]: uppercase
  • \1 : result of the first \(<pattern>\)
  • \E : end of \u, \U, \l and \L.

See: substitute