문제

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.

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top