Advanced Usage of Ranges with Vim Keymappings

岁酱吖の 提交于 2019-12-02 07:45:27

The behavior you requested is normally done with :h map-operator mapping. With this commenting 3 lines down will turn into comc2j though, but 3 lines up is now just as easy: comc2k.

You can also use visual mode without changing your mapping at all: V2kcomc. You will have to add xnoremap with the identical lhs and rhs because nnoremap works only for normal mode. (And do not use nmap.)

Third option is mapping - to something that moves {count} lines up and puts count back:

nnoremap <expr> - (v:count ? ":\<C-u>\n" . (v:count-1) . 'k' . v:count : '')

. This assumes you are writing 6-comc, not -6comc.

// By the way, I would suggest The NERD Commenter if it comes to the plugin.

While it's commendable to go as far as possible without any plugins, sometimes they're just the best option. What will you do when you start working in a language that has comments with # or (*...*)? Add new mappings for these comment characters?

I recommend commentary.vim which does filetype-aware commenting.

The default commenting operator in commentary.vim is gc. You can combine it with motions, and use it in Visual mode too.

Your use cases:

  • Comment downwards N lines (say, 3): :.,.+3normal gcc, or gc3j or 4gcc.

  • Comment upwards 5 lines: :.,.-5normal gcc, or simply gc5k.

  • Comment until int main: :.,/int main/-1normal gcc, or simply gc/int main followed by Enter.

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