vimtricks.wiki Concise Vim tricks, one at a time.

How do I list all lines in the current file matching the word under my cursor?

Answer

[I

Explanation

How it works

The [I command searches the current file (and included files) for the word under the cursor and displays a list of all matching lines with their line numbers. It is like a quick inline grep without leaving Vim.

Related commands:

  • [I lists all lines matching the word under the cursor
  • ]I lists matches from the cursor position to end of file only
  • [i shows just the first match
  • ]i shows the first match after the cursor

After seeing the list, you can jump to a specific match by typing the line number shown and pressing Enter, or use [<Tab> to jump to the first match and ]<Tab> for subsequent ones.

You can also use the Ex command version for any pattern:

  • :ilist /pattern/ lists all lines matching the pattern

Example

Suppose you have a Python file and your cursor is on the word validate:

def validate(data):        " line 5
    if not validate_schema: " line 6
    return validate(input)  " line 20
    validate(user_data)     " line 45

Press [I and Vim displays:

  1:   5 def validate(data):
  2:   6     if not validate_schema:
  3:  20     return validate(input)
  4:  45     validate(user_data)

This gives you an instant overview of everywhere the word appears. It is faster than running a full search and pressing n repeatedly, especially when you just want to see all occurrences at a glance.

Note that [I also searches included files (based on the include option), which can show matches from imported modules in some languages.

Next

How do you yank a single word into a named register?