I want to comment out a piece of code. For instance this snippet:foobarfooShould become this:<!--foobarfoo-->So I type the following sequence:vap:norm '<i<!--<C-v><Esc>'>A-->This give me the following result, which is unexpected for me:<!--<!--<!--foobarfoo-->-->-->I understand that a Normal mode command on a range is executed on each line of this range, right?My question is: is it possible to execute the Normal mode command only on the first/last line of a range and if so, how can I do this?
Is it possible to execute a Normal mode command only on the first/last line of a range?
command line;normal mode;range
As I said in the comments I strongly recommend a plugin like nerdcommenter or vim-commentary to do stuff like this because they are well tested and handle a lot of corner cases.Now if you want to do it by yourself a solution could be the following. First create a function which takes two numbers as arguments and uses these numbers as the lines on which it should make the substitutions:function! HTMLCommentFunction(line1, line2)    execute normal!  . a:line1 . GI<!--    execute normal!  . a:line2 . GA-->endfunctionThen create a command accepting a range and which will send the lines of this range to your function:command! -range HTMLComment call HTMLCommentFunction(<line1>, <line2>)And you're done you can then use :'<,'>HTMLComment to get your result.And to make all of it easier to use you can create the following mapping:vnoremap <key> :HTMLComment<CR>So you can just press you key in visual mode to get your comment.