State variables in VIM
Posted
by
dotancohen
on Super User
See other posts from Super User
or by dotancohen
Published on 2012-07-01T09:41:12Z
Indexed on
2012/07/01
15:18 UTC
Read the original article
Hit count: 223
vim
Is there any way to have a state variable in VIM? Consider:
" Relative line numbers in Normal mode, absolute in Insert mode. Ctrl-N to toggle
set relativenumber
autocmd InsertEnter * :set number
autocmd InsertLeave * :set relativenumber
inoremap <C-n> <C-o>:call NumberToggle()<cr>
nnoremap <C-n> :call NumberToggle()<cr>
function! NumberToggle()
if(useRelNums != 1)
let useRelNums=1
set number
else
let useRelNums=0
set relativenumber
endif
endfunc
Of course, useRelNums
is undefined. I would like to keep this variable defined between calls to the NumberToogle()
function, i.e. a state variable. How is this done? I'm sure that I could repurpose some other VIM variable as a workaround (such as the state of paste
for a bad example) but I would prefer a real solution.
Thanks.
© Super User or respective owner