52

Is there a way to highlight only the current line number (in the left hand coloumn) in vim, without highlighting the background of the current line? Ideally, I would Like to make the current line number bold.

4
  • I don't know about that, but you do realize it's at the bottom of the window, yes?
    – Andrew
    Nov 23, 2011 at 18:27
  • It's not at the bottom of my window. The bottom of my window only contains the text --INSERT--. How do you get the current document position to show at the bottom of the window?
    – cg433n
    Nov 23, 2011 at 18:49
  • I didn't realize it was not on by default, but apparently set ruler could be a command for that: linuxquestions.org/questions/linux-desktop-74/…
    – Andrew
    Nov 23, 2011 at 18:57
  • Yep. set ruler works for my setup (MacVim on Lion), and it's not on by default. Thanks for your suggestion.
    – cg433n
    Nov 24, 2011 at 2:12

6 Answers 6

49

There are two groups that determine highlighting of line displayed when &cursorline option is active: CursorLine and CursorLineNR. First is used to highlight the whole line, second for the line number. So to achieve what you want you must

  1. Clear the highlighting of CursorLine: just hi clear CursorLine after any :colorscheme and set background= call.

    hi clear CursorLine
    augroup CLClear
        autocmd! ColorScheme * hi clear CursorLine
    augroup END
    
  2. Set the highlighting of CursorLineNR if it is not set in your colorscheme:

    hi CursorLineNR cterm=bold
    augroup CLNRSet
        autocmd! ColorScheme * hi CursorLineNR cterm=bold
    augroup END
    

    (better to check whether it is already set in the colorscheme, maybe it will look better in that case).

You can join both autocommands in one of course.

CursorLineNR has been added relatively recently around version 7.3.488.

7
  • Clever solution. What is the asterisk for (after the autocmd! ColorScheme)? I'm not well versed in vimscript.
    – cg433n
    Nov 8, 2012 at 3:49
  • @ChristopherGreen :h :autocmd: it would show you that it can only be pattern. A single asterisk means that this autocommand will be executed for every colorscheme.
    – ZyX
    Nov 8, 2012 at 19:31
  • 1
    @ZyX Thanks for your great answer, but CursorLineNR work for all Vim versions? I'm trying to follow your answer but I can't highlight the current line number using Vim 7.3.
    – Yang
    Jul 20, 2013 at 19:06
  • 1
    @Yang Generally the best way to answer this question is using hg annotate on the file containing relevant documentation (in this case it is runtime/doc/syntax.txt). hg annotate runtime/doc/syntax.txt | grep CursorLineNr shows that this tag was first defined in revision bitbucket.org/ZyX_I/vim/changeset/… which is just after 7.3.487. In this case CursorLineNr is also shown in log, revisions corresponds to vim 7.3.479.
    – ZyX
    Jul 20, 2013 at 21:57
  • @ZyX Thanks for your explanation to Mercurial. Updating to 7.4a solved my problem. Do you mind helping me with another question? superuser.com/questions/622171/… Thanks a lot.
    – Yang
    Jul 21, 2013 at 0:16
21

You want to look at

:se cursorline

and perhaps even/also

:se cursorcolumn
2
  • 1
    How to turn that off? Feb 20, 2016 at 7:46
  • 4
    @Pétur as with any other option, so :se cul! or e.g. :se nocursorline
    – sehe
    Feb 20, 2016 at 9:58
19

This is what worked for me:

highlight CursorLine cterm=NONE ctermbg=NONE ctermfg=NONE guibg=NONE guifg=NONE
set cursorline

I'm just using this in my .vimrc after having set the color scheme. Of course you could also set a specific background color but setting all of them to NONE just highlights the line number (i.e. makes it brighter).

I guess you could just use :hi CursorLine cterm=NONE but I just wanted to make sure I'm making everything transparent (gvim included).

With CursorLineNR I was then able to set the foreground and background colors of the highlighted number.

I'm only writing this because for me it worked without any autocommands and it may be what most people require.

2
  • This is a nice low-impact way to highlight the line. Love it!
    – f1lt3r
    Jan 9, 2016 at 16:54
  • Straight forward and elegant.
    – Hritik
    May 7, 2017 at 11:25
15

You can set cursorline to highlight only the numbers

'cursorlineopt' 'culopt' string (default: "number,line")                        
                        local to window                                         
                        {not available when compiled without the +syntax        
                        feature}                                                
        Comma separated list of settings for how 'cursorline' is displayed.     
        Valid values:                                                           
        "line"          Highlight the text line of the cursor with              
                        CursorLine hl-CursorLine.                               
        "screenline"    Highlight only the screen line of the cursor with       
                        CursorLine hl-CursorLine.                               
        "number"        Highlight the line number of the cursor with            
                        CursorLineNr hl-CursorLineNr.             

And to override your colorscheme use autocmd

So, the following works:

set cursorline
set cursorlineopt=number
autocmd ColorScheme * highlight CursorLineNr cterm=bold term=bold gui=bold
1
  • Thanks. The new underlined numbers was driving me crazy.
    – trinaldi
    Jun 18, 2020 at 4:23
5

This worked for me to highlight the line number and not the rest of the line:

highlight CursorLineNr cterm=NONE ctermbg=15 ctermfg=8 gui=NONE guibg=#ffffff guifg=#d70000

0

The help for highlight-groups does not mention an exclusive "number of current line" syntax group, so the official answer might be no.

You may want to take a look in the cursorline option which highlights the entire line, if it helps.

0

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.