Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim, how to map two tasks under one shortcut key?

i like to have line numbers ON in Vim but sometimes line numbers are annoying. I have shortcuts set in .vimrc:

map <c-F12> :set nonumber<ENTER>
map <c-F11> :set number<ENTER>

for turning them off/on.

Is it possible to connect both tasks (turning off/on) under one key? I mean, when line numbers are ON shortcut should turn OFF them and turn ON when line numbers are OFF.

like image 721
jotbek Avatar asked Dec 08 '10 12:12

jotbek


2 Answers

From Vim help, you can use the following syntax :

:se[t] {option}! or
:se[t] inv{option} Toggle option: Invert value. {not in Vi}

See :help set-option

In your case, you could use: map <c-F11> :set number!<ENTER>

like image 83
Xavier T. Avatar answered Nov 13 '22 12:11

Xavier T.


In case you want also change between the relative line number, the line number, and no number you can use something like the following:

let g:relativenumber = 0
function! ToogleRelativeNumber()
  if g:relativenumber == 0
    let g:relativenumber = 1
    set norelativenumber
    set number
  elseif g:relativenumber == 1
    let g:relativenumber = 2
    set nonumber
    set relativenumber
  else
    let g:relativenumber = 0
    set nonumber
    set norelativenumber
  endif
endfunction

map <c-f11> :call ToogleRelativeNumber()<cr>
like image 31
skeept Avatar answered Nov 13 '22 12:11

skeept