8

I don't have much use for line numbers while in insert mode so I would like to turn them off. However they are great for normal mode.

Constantly using set nu! is tiresome. Is there a way to tie set nu! to exiting and entering normal mode?

Any solutions using the above or any other method?

  • Regarding your comment to @Carpetsmoker's answer, why would you keep the margin but not the line numbers? What was your original intent to get rid of the numbers? – Rolf Aug 9 '16 at 6:31
  • It acts as a way to differentiate between insert mode and normal mode. Also removing the line numbers serves to remove irrelevant information from the insert mode. If every time entering insert mode you remove the margin you are likely to get a headache from all the text shifting three spaces each time. Some might prefer this, however it is not my preference. – Jason Basanese Aug 9 '16 at 13:01
13

You can do this with autocommands.

au InsertEnter * set nonumber
au InsertLeave * set number

Not much explanation is needed. This does exactly what you asked for. It ties "entering and exiting insert mode" to "turning line numbers on and off".

  • 1
    Note however, that InsertLeave is not triggered, when using Ctrl-C to exit insert mode – Christian Brabandt Aug 4 '16 at 11:52
5

Here's an alternative solution which will only hide the numbers, but won't actually change the margin on the left so you text doesn't jump around so much when entering/leaving insert mode:

set number

augroup number
    autocmd!
    autocmd InsertEnter * highlight LineNr ctermfg=white guifg=white
    autocmd InsertLeave * highlight LineNr ctermfg=130 guifg=Brown
augroup end

When entering insert mode this will set the text colour to the background colour. In my case this is white, but this may be different in your case. When we leave insert mode it resets it to the default for my colour scheme.

You can find out the default for your colour scheme with:

:highlight LineNr
LineNr         xxx term=underline ctermfg=130 guifg=Brown
  • 3
    Even better: au InsertEnter * hi LineNr ctermbg=NONE ctermfg=bg guifg=bg – Christian Brabandt Aug 4 '16 at 11:51
  • This is likely what I will use, but the other answer is more concise and answers the question directly. – Jason Basanese Aug 4 '16 at 13:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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