← back to latest

Underline a heading with a row of equals signs exactly as long as the title

VISUALVr=Underline a heading with a row of equals signs exactly as long as the title

anatomy

V
Start linewise visual mode. The selection covers every character on the current line.
r
In visual mode, r replaces every selected character with the next character you type. Line breaks are left alone, so the line keeps its length and the lines stay separate.
=
The replacement character. Any single character works: =, -, *, a space.

In the buffer

Before:

Release notes for 2.4
^

Fixed the retry loop.

After typing yyp to duplicate the heading line, then Vr=:

Release notes for 2.4
=====================
^

Fixed the retry loop.

The copy of the title becomes a row of 21 equals signs, one for each character in the title. The cursor sits at the start of the new underline.

When you would reach for it

You are writing a README in reStructuredText or setext-style Markdown, and every heading needs an underline that matches its length. Holding down = and counting is how you end up one short, and RST complains when an underline is shorter than its title. Duplicating the title and overwriting every character of the copy gets the length right by construction. The same move works any time you need to blank out or mask a stretch of text without changing its width.

Gotchas

  • r replaces characters, not screen columns. A tab counts as one character, so Name<Tab>Value becomes ten = signs even though it looks thirteen columns wide. Run :retab first if the title contains tabs.
  • Wide characters such as CJK text take two columns on screen but count as one character, so the underline comes out visibly short. Pad it by hand with A= or 5A=<Esc>.
  • Linewise V selects the whole line, including leading indentation. If the heading is indented, the indentation turns into = too. Use ^v$ instead, which starts the selection at the first non-blank character.
  • Vr does not touch line breaks, so selecting several lines with Vj and pressing r- gives you several lines of dashes, not one long one. That is usually what you want, but it means you cannot use it to join lines.

Variants

password=hunter2, user=admin
         ^
password=*******, user=admin
         ^

vt,r*: select up to the comma and mask it. Characterwise selection lets you overwrite a value in place without changing the line’s length, handy before pasting a config into a bug report.

id  name
01  alpha
^
02  beta
03  gamma
id  name
XX  alpha
^
XX  beta
XX  gamma

<C-v>jjlrX: blockwise selection overwrites a rectangle, in this case the first two columns of three rows.

Release notes for 2.4
Release notes for 2.4
^
Release notes for 2.4
=====================
^

:s/./=/g: the Ex way to do the same overwrite on the current line. It works in vi, which has no visual mode, and accepts a range like :.,+2s/./=/g for several lines.

lineage

Visual mode did not exist in vi. Bram Moolenaar added it to Vim in the early 1990s, and with it gave the familiar operators a selection to act on. Single-character r was already in vi; Vim's visual version extends it from one character to every character in the selection, in characterwise, linewise, and blockwise modes alike.

-- INSERT --vim-hacks.com034-034-underline-a-heading-to-match-its-length.html
utf-81,1All