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

How do I quickly create an Ex command range spanning the next N lines using a count before the colon?

Answer

5:

Explanation

Typing a count before : in normal mode automatically fills in a line range in the command line. For example, pressing 5: opens the command line with :.,.+4 pre-filled, covering the current line and the next 4 lines. This is a fast way to apply Ex commands to a block of lines without calculating the range manually.

How it works

  • {N}: in normal mode opens the command line with :.,.+{N-1} pre-filled
  • . is the current line; .+{N-1} is N-1 lines below
  • The result is a range of exactly N lines starting from the cursor
  • You then type your command after the pre-filled range and press Enter

Example

With the cursor on line 10, pressing 5: gives:

:.,.+4

Now type s/foo/bar/g and press Enter to substitute on lines 10–14.

Or press 5:d<CR> to delete the current line plus the next 4 (5 lines total).

Tips

  • This is faster than visually selecting lines or typing 10,14 when you know the relative count
  • Works with any Ex command: 5:s/old/new/g, 5:norm @q, 5:d, etc.
  • The visual selection equivalent (: after V5j) gives :'<,'> instead
  • Use 2: to affect just the current line and the one below

Next

How do I configure Vim's command-line tab completion to show all matches and complete to the longest common prefix?