When I am recording a macro with q
and I am done, I can type:
q
to stop recording if I am in normal or visual mode.<c-o>q
to stop recording if I am in insert mode...
How do I stop recording if my macro ends in command mode?
Vi and Vim Stack Exchange is a question and answer site for people using the vi and Vim families of text editors. It only takes a minute to sign up.
Sign up to join this communityAnother solution is when in command line mode, use Ctrl-f
, and press q
to stop recording.
Ctrl-f
allows you to open the command line window, in which you can edit the command in normal mode. To validate the command, simply use enter.
Therefore, after having added something like:
cnoremap <c-q> <c-f>i<c-o>q
in your .vimrc
, you can use <c-q>
to stop recording a macro while in command mode.
Florent
^F
) from the end of the macro, right? At least that's what I see.
qqSTARTMACRO:STARTCOMMANDLINEPART<c-f>i<c-o>q
and so the recording has actually stopped while your were typing the command part. To use the macro: @qFINISHYOURCOMMAND<cr>
and you're done: no need to edit it. Unfortunately, this does not seem to work from visual mode (or it looses the selection).. or have I missed something?
Sep 6, 2015 at 16:17
@q
and the range was added at command start '<,'>
- as long as the macro started with :
....
'<,'>
) still matches the selection when validating the command.
One way to do it:
qq
... other chars)ESC
to get out of cmdline-mode, q
to end your macro.ESC
from your q
register (*): you can paste it in a buffer, remove the ^[
character, select the whole stuff again (character-wise) and yank it back to register q
.@q
.Basically, you "post-process" your macro a little, since it is stored in a register :)
(*) A quick way to remove the last character from a macro stored in register q
would be:
:let @q = @q[:-2]
:let @q = @q[:-2]
please? Especially how does [:-2]
work? :-)
Having read your comment about the use case, it looks like you could use a key mapping instead of a macro.
I have this line in my .vimrc (don't remember where I found it):
nnoremap <Leader>s :%s/\<<C-r><C-w>\>//g<Left><Left>
It has a similar effect to the use case you described, putting you on the command line so the only thing you have to do is type the replacement and press enter, but in this case, replacing all occurrences of the word that was under your cursor.
To just replace within the current block, I think something like the following should work:
nnoremap <Leader>s m'va{<ESC>``:'<,'>s/\<<C-r><C-w>\>//g<Left><Left>
Of course, you can map it to whatever key you want.
What the above does: mark the current position using the unnamed marker, visually select the current block, leave visual mode and return the cursor to the saved position (the visual selection markers are still set), enter command mode and type out the substitute command, inserting the word that was under the cursor with <C-r><C-w>
, and positioning your cursor to type out the replacement.
toBeReplaced
pattern is not as simple as "the word under cursor", which is why I need a small, specific macro that I could create on the go. And then again: too much specific for a map :\ Thank you anyway :)
Sep 4, 2015 at 16:42
toBeReplaced
part (instead of current word under cursor, which is more limiting).
va{:s/<C-r>0//g<Left><Left>
With this you can yank whatever pattern you need before-hand, and then just press the mapped key to replace that pattern with something in the current block.
Sep 4, 2015 at 20:13
:'<,'>s/toBeReplaced//g
then put the cursor between the two//
so that all I have to do is to typenewString<cr>
and I'm done. :)