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, 2016 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. Aug 9, 2016 at 13:01

2 Answers 2


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 Aug 4, 2016 at 11:52

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 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 Aug 4, 2016 at 11:51
  • This is likely what I will use, but the other answer is more concise and answers the question directly. Aug 4, 2016 at 13:56

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.