Searching

I assume you’re aware of using / and ? for searching forward and backwards. Let’s look into some maybe less known things.

Search for word under cursor

In normal mode: * to search forward

and # to search backward

Using last search as a pattern

Say you have searched for something and now you want to replace it. Isn’t it annoying to have to either copy or rewrite the regex you just wrote? You don’t have to, if you leave the pattern empty it will use the last used pattern.

Here I do * to trigger a search for the thing under my cursor and then do a replacement.

Global commands

Ever wanted to do actions on rows matching a pattern?

“Positive” global command

:g/regex/action optionally you can also supply a range or make a visual line selection. In this example :g/bleh/d deleted all lines containing bleh.

“Negative” global command

:v/regex/action same thing but the match is inverted.

Combined with norm

norm allows you to write sequences like you would in normal mode. Here, append a ; to non-empty lines that don’t currently end in one.

:v/;$\|^$/norm A;

…and combined with macros

Fixing up some nested tuples. I record the macro I[<esc>$i]<esc> into register q use a global command to run on lines matching a regex.

:g/[0-9],$/norm @q