Vim Tip of the Day: Search

While words, lines and paragraphs give us a way to refer to text in general, often, these “linguistic” nouns aren’t fine-grained enough to do the thing we want. You may want to go not just to the next word, but to the next occurrence of a specific word, like “Oliver.” You may not want to delete the entire line, you may just want to delete everything until the hyphen. In such cases, you are leveraging particular features of the text you are working with rather than features applicable to text in general. These cases all roughly fit under the rubric of search-based motions, the most general of which are:

/ ? — go to next/previous occurrence of <whatever you type>
n N — go to next/previous occurrence of your last search term

These search for regexes and not only literal strings, making them very expressive and useful. For instance, if you search for the regex ^$ (meaning “empty line”) by typing /^$, then n and N now behave a lot like { and } — in essence, you’ve just defined your own nouns! Although, as n and N change each time you do a search, these nouns are ephemeral. Here are some other search-based motions:

* # — go to next/previous occurrence of word under cursor [handy!]
% — go to matching paren, tag, or delimiter
f F t T — go to, i.e. (f)ind next/previous or find un(t)il next/previous occurrence of <character> in the current line, forwards or Backwards
; , — go to next/previous occurrence of last in-line search (e.g. f or t)

The f and t searches may seem obscure, but they are very useful in cases where they capture the way in which you actually think about the editing action. “I want to change everything here until the hyphen.” As with all things in Vim, expressing things conceptually instead of manually pays dividends (we’ll see why later when we talk about “living the high life”). Also note that ; and , here play the role of new “ephemeral” nouns just like n and N do with regex search.

Finally, remember that since any of these motions implies a range in the same way that the basic nouns do, these can be combined with verbs in the usual ways, e.g. ct; (Exercise: what does this Vim sentence mean?).

2 comments

  1. srea

    I don’t know whether you were planning on going into this, but one of my most used operators in vim related to searches is “gn”, which visually selects the search results and applies any pending operator – meaning I always use it like “dgn” or “cgn” to delete or change what I searched for, which is then also repeatable with “.” ! My brain is just way faster with “dgn” / “cgn” compared to using substitute commands, plus I can see the result and skip one deletion / replacement with “n”.

    1. sid

      I usually do this by doing the action on the first result and then repeating n ., but by using dgn / cgn it looks like that reduces it to a single action, just . or n to skip. I like it! Also interesting that gn works like a text object here and not a motion, i.e. we are saying “delete the next match” not “delete from here to the next match.” And we can do different actions on each of the matches once they are all selected. dgn (delete this one), cgn (change this one), n (leave this one alone), . (do the same thing as last time) … which could be especially useful on regex-based searches for interactive editing of a selection of content where it may not even be a simple edit that we are repeating (e.g. dw wouldn’t even work since it’s a regex we are manipulating!). Really cool stuff, thanks for sharing!

Leave a Reply

Your email address will not be published. Required fields are marked *