views:

82

answers:

1

I'm making a shortcut that places a # at the front of each line, in the next x lines. x is a number I type before entering the shortcut, like typing 11dd deletes the next eleven lines.

The command is .,+10 s/^/#/g. Here the number ten should really be whatever was typed before the shortcut. How do I make the shortcut change according to the number that was typed before it?

Added after question was answered:

So now I have the following in the .vimrc:

nmap c1 :s/^/#/g<esc>``
nmap c0 :s/^#//g<esc>``

Which allows me to type 13ac, to add # at the front of the next 13 lines, and 13dc to delete any # at the front of the next 13 lines.

It's better than =pod and =cut for they cause errors when nested.
c1=comment add,
c0=comment delete.
# is used in Perl.

+3  A: 

In ex mode, you can use the following command:

s/^/#/count

where count is the number of lines you want to change. You can't put the number before the command, because that is used to select the starting line (current line if omitted). Thus:

5s/^/#/3

will add a '#' before lines 5, 6 and 7.

Edit

In ex mode you can use the map command to create a shortcut to a colon command, which you can then use with a prefix number:

map CC :s/^/#/g

Now you can use 'xCC' in vi mode to prepend '#' to the next x lines.

schot
but i mean, isn't there some sort of ex/vimscript variable or something that gets filled whenever a number gets typed?somehow vim knows what got typed before dd when one presses 11dd.
Hermann Ingjaldsson
@Hermann Now I know what you mean by shortcut, see edit.
schot
that totally solved my problem. thanks a lot.
Hermann Ingjaldsson
@Hermann Ingjaldsson there is a v:count variable, but it is for more complex cases.
ZyX