42

I know

:set number

and

:set nonumber

I want to know whether there is any command in vi/vim

:set togglenumber

to display line number if the line numbers is not showing or hiding line numbers if line numbers is showing.

Mohammed H
  • 6,880
  • 16
  • 81
  • 127
  • 4
    either `set number!` or `invnumber`. See also [this table](http://stackoverflow.com/questions/9306914/how-can-i-get-all-the-abbreviations-of-vims-options/9312070#9312070) I made some time ago. – Benoit Feb 20 '13 at 09:35

4 Answers4

77

You can use (on VIM at least):

:set invnumber

More Info:

:set number      Turn line numbers on
:set nonumber    Turn line numbers off
:set invnumber   Toggle line numbers
:set number!     Toggle line numbers
:set number&     Set option to default value
:set number?     Show value of option 

source: http://vim.wikia.com/wiki/Managing_set_options#Boolean_options

Mohammed H
  • 6,880
  • 16
  • 81
  • 127
MechanTOurS
  • 1,485
  • 2
  • 15
  • 18
  • nice! modifiers like "inv..." or "...!" etc. work with many/all(?) options. Type `:set inv` to see possible values – MacMartin Mar 12 '20 at 08:19
13

In vim, many options support this set pattern, (for example, foo):

"enable the option
set foo

"disable it
set nofoo

"toggle the option
set foo!

"get option's current value
set foo?

for number, map a key to :set nu! would be ok.

Kent
  • 189,393
  • 32
  • 233
  • 301
13

You can toggle any (boolean) option using the ! suffix, i.e. :set number! would toggle displaying line numbers.

The on-board help for this command is a bit hidden; while one would think :help toggle would get you there, it doesn't quite. Using :help set-! is the magic word.

Jens
  • 69,818
  • 15
  • 125
  • 179
8

I use relative line numbers, for all lines above and below the current line, together with absolute line numbering for the current line. But this is only working since Vim 7.4.

This is sometimes named “Hybrid” line numbering (e.g. in the article https://jeffkreeftmeijer.com/vim-number/)

To toggle from "no numbers at all" to "hybrid" you can use the following config:

set number relativenumber " Turn line numbering on at startup
" Toggle line numbers from none at all
" to relative numbering with current line number
noremap <F3> :set invnumber invrelativenumber<CR>
Simon Schürg
  • 2,134
  • 2
  • 20
  • 31