gws
gws

Reputation: 777

Using inline if operator in .vimrc

I am trying to implement a toggle between absolute and variable line numbering in vim and I'd like to do as a one liner instead of writing an if function.

My current code is:

nnoremap <F4> :( &rnu == 1 ? "set nu" : "set rnu" )<CR>

which does not work; anybody knows how can I make it work?

Upvotes: 3

Views: 1969

Answers (1)

Randy Morris
Randy Morris

Reputation: 40947

This is how I would accomplish this:

:nnoremap <f4> :setl <c-r>=&nu ? "rnu" : "nu"<cr><cr>

The longer version that is probably a little more clear for the future Googlers:

:nnoremap <f4> :setlocal <c-r>=&number ? "relativenumber" : "number"<cr><cr>

The <C-r>= tells vim to use the expression register to evaluate the rest of the line as vim commands. The trailing double <cr> is required because the first one evaluates the expression and the second one executes the :setlocal command.

Upvotes: 7

Related Questions