Vim: remap key to toggle line numbering

余生颓废 提交于 2019-12-03 11:17:32

问题


I added:

set number
nnoremap <F2> :set nonumber!

to my vimrc file. Basically what it's supposed to do is let me press F2 to toggle line numbering but it's not working. What have I done wrong?


回答1:


In your .vimrc, add this:

set number
nnoremap <F2> :set nonumber!<CR>

Then pressing F2 will turn on line numbering if it is off, and turn it off if it is on.




回答2:


This is what I use (with a different key binding):

nmap <f2> :set number! number?<cr>

The "number!" toggles the setting and "number?" reports the state.




回答3:


nmap <silent> <F11> :exec &nu==&rnu? "se nu!" : "se rnu!"<CR>

In new vim you can set both relative number and number at once, this way:

set nu rnu




回答4:


This is one method:

map <silent> <F2> :if &number <Bar>
    \set nonumber <Bar>
        \else <Bar>
    \set number <Bar>
        \endif<cr>

(this one is nice 'cause I usually put foldcolumn in there as well)

This is another:

map <silent> <F2> :set invnumber<cr>

(direct method)




回答5:


I use this to toggle between relativenumber ( with current absolute line number) and no line numbering

nnoremap <silent> <leader>l :set relativenumber! <bar> set nu!<CR>


来源:https://stackoverflow.com/questions/762515/vim-remap-key-to-toggle-line-numbering

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!