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

How do I highlight the column just past my textwidth to visually mark line length limits?

Answer

set colorcolumn=+1

Explanation

The colorcolumn option highlights one or more vertical columns to help keep lines within a length limit. Values prefixed with + or - are relative to textwidth, so the guide column automatically follows if you ever change your line length setting.

How it works

  • set colorcolumn=80 — absolute: always highlights column 80
  • set colorcolumn=+1 — relative: highlights the column one past textwidth
  • set colorcolumn=-0 — highlights exactly at textwidth

With set textwidth=79, set colorcolumn=+1 highlights column 80. If you later do set textwidth=119, the highlight automatically moves to column 120 without touching colorcolumn.

Example

set textwidth=79
set colorcolumn=+1

Column 80 is now highlighted in a different color. Any text reaching that column is clearly past the limit. After set textwidth=99, column 100 becomes highlighted instead.

Tips

  • You can highlight multiple columns: set colorcolumn=+1,+2,+3 to show a gradient zone
  • Customize the highlight color with :hi ColorColumn guibg=#333333
  • Use set colorcolumn= (empty) to disable the highlight entirely
  • The relative form only works when textwidth is non-zero; otherwise it has no effect
  • Combine with set wrap and set linebreak for a visual guide that complements soft wrapping

Next

How do I programmatically read and write Vim register contents including their type from Vimscript?