Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I want to be able to type something like 'Ncom' in normal mode that will comment out the current and then next N-1 lines. A key-mapping that I made to do so is, :nmap com ^i#<ESC>j. This does in fact comment out the current line, but it does not work for N lines in a manner similar to built-in vim commands. What do I need to know about vim that will explain the gap between what I want to happen and what is happening? Also, how might I correctly implement what I want?

share|improve this question

2 Answers 2

:nnoremap com :normal! I#<CR>

Explanation…

  • I is the same as ^i but better. Generally, you should do the following instead of ^i#<Esc>:

    I#<Esc>
    

    Same for $a, by the way.

  • :normal is an Ex command that allows you to run a normal mode command from the command line. It is used like that, followed by <CR>:

    :normal! I#
    
  • When a normal mode command is prepended with a count: 6w, the command is executed 6 times. However, the count is not propagated to subsequent commands: 6wifoo doesn't add foo before each of the 6 next words. Depending on the command, the rules may vary: consult the documentation if you are not sure.

  • When an Ex command is prepended with a count: 6:command, Vim automatically adds a range starting from the current line and ending with the count-1th line below. If you do 6:normal! I#, the command becomes:

    :.,.+5normal! I#
    

    which means "insert a # before the first printable character of this line and the 5 lines below".

  • When Vim comes across an Ex command in a mapping, with a :, it always adds the relevent range. That's why it's best to use :normal in mappings that are meant to be used over multiple lines or with a count.

See :help range and :help :normal.

share|improve this answer
    
Thank you very much. –  seewalker May 12 '13 at 21:08

I think this plugin does the job : https://github.com/tpope/vim-repeat

example:

silent! call repeat#set("\<Plug>MyWonderfulMap", v:count)
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.