Justin
Justin

Reputation: 151

Highlighting the 70th character of a current line in Vim

I like to keep a strict 70 character margin whenever possible. To help with this, I want to configure vim so that the 70th character of the current line is highlighted. I understand that

set cursorline

can be used to highlight the current line. I, however, would like just the very end of the line (the 70th character) to be highlighted. How would I go about accomplishing this?

Edit: cursorcolumn isn't what I'm looking for. I just want a single character (the 70th one on the current line).

Edit 2: perhaps a picture will help.enter image description here

Upvotes: 5

Views: 548

Answers (4)

Tjaart
Tjaart

Reputation: 496

:autocmd CursorMoved * exe 'match IncSearch /\%70v\%' . line(".") . 'l./'

The highlight color will be determined by your color scheme.

You can change IncSearch to any of the highlight groups, which can be found by typing:

:hi

Upvotes: 2

Dan Lowe
Dan Lowe

Reputation: 56518

You can use colorcolumn to set a "right margin" bar.

This did not exist before Vim 7.3, so it is wisest to only enable it if the feature is available.

if exists('&colorcolumn')
    set colorcolumn=70
endif

I prefer that this only be shown in insert mode, so I use this:

if exists('&colorcolumn')
    autocmd InsertEnter * set colorcolumn=80
    autocmd InsertLeave * set colorcolumn=""
endif

That will set the option when you switch to insert mode, and turn it off when you leave insert mode.

Upvotes: 5

Fabius Wiesner
Fabius Wiesner

Reputation: 926

:call matchadd('Todo', '\%70c')

and if you don't want to count one tab as a single character, but you want to take into account all the spaces it takes:

:call matchadd('Todo', '\%70v')

You can use any other highlight group (for example to change color) listed by :hi instead of Todo.

Upvotes: 1

Jeremy Walker
Jeremy Walker

Reputation: 85

If you are using VIM 7.3 you can set the color of a certain column with:

set colorcolumn=70

Upvotes: 0

Related Questions