How do I highlight the current line and the cursor in .vimrc?

38,017

Solution 1

You have to change the color of your cursor line to a color other than the color of your cursor. If you're in a terminal emulator like st or rxvt, Vim cannot change the color of your cursor; it will always be the color your terminal application decides to make it. Only the graphical version of Vim is able to change the color of your cursor.

You can change your cursor color through your terminal configuration though.

Some ~/.Xdefaults / ~/.Xresources examples:

XTerm*cursorColor: #FFFFFF
URxvt.cursorColor: white

You could also use the Vim command :set cursorcolumn to put your cursor in crosshairs.

Solution 2

If you do not want to enter the command each time you start Vim, you have to put the commands in your .vimrc file.

whereis vim    
*#type in your linux terminal* 

(you'll have a different location, but your color scheme will be here /usr/share/vim/vim74/colors/.

You can list the existing color schemes with

ls /usr/share/vim/vim74/colors/    

try different color schemes from the listed

:colorscheme desert
:colorscheme delek

The following command in Vim activates a vertical line at the cursor's location.

set cursorcolumn 
hi CursorColumn ctermbg=8

to toggle (with the exclamation mark, works with all set-command)

set cursorcolumn!

for example:

set cursorline
set cursorline!

hi CursorLine ctermbg=235
*#defines a gray colour for the horizontal line*

Here is a table with xterm colours: link

Use a number by defining ctermbg=... (... = color number from the table) You can also try ctermfg=..., but it's not worth using it.

Share:
38,017

Related videos on Youtube

DevWouter
Author by

DevWouter

Updated on September 18, 2022

Comments

  • DevWouter
    DevWouter over 1 year

    I am trying to highlight the current line as well as the cursor position in Vim. Here's my .vimrc:

    set cursorline
    hi CursorLine ctermbg=8 ctermfg=15 "8 = dark gray, 15 = white
    hi Cursor ctermbg=15 ctermfg=8
    

    The problem I'm experiencing is that the current line background color covers up the cursor background color, so it looks like this:

    The current line is highlighted but the cursor is not.

    I can obviously tell where the cursor is because the foreground color is almost black, but when the cursor is on a space or at the beginning/end of a line I have no clue where it is unless I move it.

    The cursor is at the end of the line, though you'd never know it.

    What am I doing wrong here?

  • Stéphane Chazelas
    Stéphane Chazelas about 11 years
    vim could change the cursor colour, on those terminals like xterm that allow you to change it dynamically (printf '\033]12;#f50\7' for instance). You can also change it to a blinking block or underline (\e[1 q or \e[3 q)
  • Admin
    Admin about 11 years
    That's interesting. I didn't know those escape sequences existed. There's information regarding their use with vim here.