4

When I search a text file for a word from within Vim, it will highlight all matches for the word by changing their background color -- to olive green on my terminal; your color may vary. I am looking for a way to highlight the background color behind the current cursor position in this way. (This would make the cursor easier for me to see than it is by default.)

By reading the Vim documentation and searching the web, I found plenty of information on highlighting the line the cursor is currently on. But that is not what I'm looking for right now. I am specifically looking for a way to highlight the character at the current cursor position, specifically by changing its background color in the way Vim already does with matches to text searches

How can I do that?

5
  • 1
    Welcome to Vi and Vim! To be honest, I'm not sure why you wouldn't just change your terminal's cursor color at this point...
    – D. Ben Knoble
    Commented Apr 3, 2020 at 18:55
  • 1
    For GUI it's :hi Cursor guibg=#rrggbb. For TUI you really should setup your terminal instead.
    – Matt
    Commented Apr 3, 2020 at 19:10
  • I did make my terminal's cursor as dark as I could. It still wasn't visible enough for me. I need a broader area highlighted. Commented Apr 4, 2020 at 8:49
  • Does this answer your question? Can I use a different color for the selected match than for other matches
    – Tae
    Commented Apr 24, 2020 at 8:06
  • Thanks for the suggestion, Tae, and sorry for the late reply, but no. My problem is that I don't really want to match anything. I just want to highlight the current cursor position. Commented May 1, 2020 at 14:57

2 Answers 2

1

You can turn on search match highlights like this.

:set hlsearch

To turn it off until the next time you search, use this.

:noh

To change highlight to a specific color:

:hi Cursor guibg=#rrggbb
or
:hi Search guibg=<colorname>
0

Here's a quotation from the vim's built-in help that will probably answer your question, if I actually got it right:

When Vim enters Insert mode the 't_SI' escape sequence is sent.
When Vim enters Replace mode the 't_SR' escape sequence is sent
if it is set, otherwise 't_SI' is sent.  When leaving Insert
mode or Replace mode 't_EI' is used. This can be used to change
the shape or color of the cursor in Insert or Replace mode. 
These are not standard termcap/terminfo entries, you need to
set them yourself.

Example for an xterm, this changes the color of the cursor:  

    if &term =~ "xterm"
        let &t_SI = "\<Esc>]12;purple\x7"
        let &t_SR = "\<Esc>]12;red\x7"
        let &t_EI = "\<Esc>]12;blue\x7"
    endif

NOTE: When Vim exits the shape for Normal mode will remain. 
The shape from before Vim started will not be restored.

{not available when compiled without the |+cursorshape| feature}

I already tested this and the only issue I see is that the original color of the cursor isn't restored upon exiting vim, as already noted above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.