Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 260
Justin Avatar asked Jan 30 '17 00:01

Justin


People also ask

How do I highlight a character in Vim?

Press the v key to enter visual character mode. The word VISUAL will appear at the bottom of the screen. Use the Arrow keys to highlight the desired text. You can use other navigation commands, such as w to highlight to the beginning of the next word or $ to include the rest of the line.

How do I highlight a line in Vim?

If you want to select the entire line in a file, press V. Now when you press k or j to go up and down, vim will select the entire line above and below your cursor. Finally, you can select text in columns by pressing ctrl+v and moving up or down the block.


1 Answers

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.

like image 77
Dan Lowe Avatar answered Oct 05 '22 08:10

Dan Lowe