Hot answers tagged

56 votes
Accepted

How can I view and edit my recording (i.e. recorded macro)?

Note: I'm assuming you're using the a register for the entirety of this answer, but you can use any register Note2: <1b> is Esc; you can insert this with Ctrl+v and then Esc. It may also show ...
user avatar
36 votes
Accepted

After pressing qq, the status line says "recording". How do I get out of this mode?

q is the command to begin the recording of a macro. By pressing qq you have told Vim to record a macro and store it in the register q. To stop Vim to record the macro, and thus to make the message ...
user avatar
  • 4,502
31 votes

Swap function arguments

Without plugins | indicates cursor position func(foo[0], bar(), |baz.spam) Steps: dt) will delete baz.spam Ff will move the cursor to the character f in foo[0] vt,p will delete foo[0] and ...
user avatar
  • 5,886
24 votes
Accepted

How do I stop recording while in command line?

Another 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 ...
user avatar
  • 356
22 votes

How can I run a function or macro across a folder of files?

If you are already in Vim, you can use the :argdo or :bufdo commands to execute a command on every item in the argument list or buffer list, respectively. e.g. to run a : command on every file in the ...
user avatar
  • 28.1k
21 votes
Accepted

Insert an incrementing number to each line in either a selection or a match

Similar to the answer at https://vi.stackexchange.com/a/818/227, you can use the global command. With it you can instruct vim to search for lines matching a pattern, and then perform commands on it. ...
user avatar
  • 8,072
21 votes
Accepted

How do I apply a set of keystrokes to every occurrence of a word?

You can use the :global and :normal commands for this. The :global command has the following syntax: :global/{pattern}/{command} This will run "command" on every occurrence of "pattern". You can ...
user avatar
20 votes

How do I stop recording while in command line?

One way to do it: start your macro as usual (e.g. qq ... other chars) when you reach the cmdline-mode, write all the characters you want you want to end the macro here. Hit ESC to get out of cmdline-...
user avatar
  • 9,330
19 votes
Accepted

Save a macro just created in vimrc

Yes, you can! There are a couple ways to do this. By default, all registers will be saved into your viminfo file, and loaded once you start vim. This is the easiest way. However, it's not foolproof. ...
user avatar
  • 16.8k
16 votes

Improve macro replay speed?

Setting the lazyredraw with :set lazyredraw will greatly improve macro execution speed by not redrawing the screen while a macro is executing or a command is run other than those you type, see :help '...
user avatar
16 votes

How can I view and edit my recording (i.e. recorded macro)?

To get something to work with... qajjjq Will start record a macro into the a register. You can see many of your current registers (used for macros, yanking, deleting, etc.) with the :reg command, ...
user avatar
15 votes
Accepted

Delete all consecutive duplicates

I think the following command should work : :%s/^\(.*\)\(\n\1\)\+$/\1/ Explanation : We use the substitution command on the whole file to change pattern into string : :%s/pattern/string/ Here ...
user avatar
  • 6,466
14 votes
Accepted

How can I run a function or macro across a folder of files?

You can use the -c argument to run a command on startup, from vim(1): -c {command} {command} will be executed after the first file has been read. {command} is ...
user avatar
14 votes
Accepted

How do I check if Vim is currently recording a macro?

Since Vim 8.1-0020, there is a reg_recording() function that'll return the name of the current register being recorded. An empty string is returned if we are not recording.
user avatar
  • 16.2k
14 votes
Accepted

Visual Block Mode edit with sequential number

Depending on your usecase the following might be useful: Create the entries all with the number "1": - "1" - "1" - "1" - "1" Then go to the second "1" and press V to start line-wise visual. Then ...
user avatar
  • 8,655
13 votes
Accepted

Continue an ex mode command after "norm"?

If you have a sequence of keystrokes that you want to execute in normal mode from the command line, you can use the :normal command. However, by default the :normal command can't be followed by ...
user avatar
  • 6,466
12 votes

How can I record a nested macro?

AFAIK, you cannot record two macros by qx...q in one shot. For your requirement, you have to create macro b by qb....q, then qa...@bq. If you don't want to "record" b you can do let @b='whatever' ...
user avatar
  • 1,240
12 votes
Accepted

How do I stop a recursive macro at the end of the line?

There is probably a simpler method but maybe you could try the following. Let's say you will use register q to record your recursive macro. At the very beginning of the recording, type: :let a = ...
user avatar
  • 6,466
11 votes

Delete all consecutive duplicates

Try the following: :%s;\v^(.*)(\n\1)+$;\1; As with saginaw's answer, this uses Vim's :substitute command. However, it takes advantage of a couple of extra features to improve readability: Vim lets ...
user avatar
  • 211
11 votes

How do I stop a recursive macro at the end of the line?

Use l A recursive macro will stop as soon as it encounters a command that fails. Therefore, to stop at the end of a line, you need a command that will fail at the end of the line. By default*, the l ...
user avatar
  • 28.1k
11 votes

Can Vim write incremental lines? i.e. on each line A becomes B, and 1 becomes 2

While it can be done with scripting, if you have a very recent version of Vim (e.g. the one in Arch Linux), you can use g Ctrl-a to increment a visual block. In your case: Write the original line: A =...
user avatar
  • 9,330
11 votes
Accepted

How can I do calculations with vim Macros?

Yes, you can perform arithmetic expressions in Vim by using the expression register, which you can access from insert mode using CTRL-R. Another alternative is to use expressions in the replacement ...
user avatar
  • 26.2k
10 votes
Accepted

Can I repeat a macro with the "dot operator"?

Give this a try. It remaps @ so that g@ (plus a dummy motion l) is used afterwards, thus becoming the last operator and subject to repeating with .. " When . repeats g@, repeat the last macro. fun! ...
user avatar
  • 2,510
10 votes
Accepted

How to force macro keep running even pattern is not found?

You can use :try to do that (:h :try). Here is an example (I didn't use your macro because you posted it as an image and it's not easy to copy :) ) let @z=':try|s/foo/bar/|catch||endtry^M' (Note ...
user avatar
  • 46.5k
9 votes
Accepted

Apply macro to every `n`-th line

How about writing a 2nd macro that moves down n lines and then executes @a? Using a mark you can easily jump back to your original position. E.g., having defined your macro in register a, press qb to ...
user avatar
  • 1,215
9 votes
Accepted

How do I ignore regex failure while running a macro?

Use the /e flag. From :help s_flag [e] When the search pattern fails, do not issue an error message and, in particular, continue in maps as if no error occurred. This is most useful to ...
user avatar
  • 16.8k
9 votes

Can I repeat a macro with the "dot operator"?

To repeat your last macro you can use @@ so 3@@ would essentially run @q 3 times. However the @ keystrokes can be unwieldy, so in my .vimrc I have the lines: "- Lazy macro repeat nmap <leader&...
user avatar
  • 248
9 votes
Accepted

Insert code snippets?

The :r command can read a file containing a code snippet and insert it to your active buffer. Unless I'm missing something, I believe this would address your request.
user avatar
  • 1,028
9 votes
Accepted

Weird behaviour when repeating macro

This is very odd behavior, but it does make sense. To understand why, first we need to back up a little bit and explain how registers and macros really work. Try this. qqiHello World<esc>q ...
user avatar
  • 16.8k
9 votes

How to create a vimrc macro using :sort?

You've already (rightly) accepted an answer that provides a much simpler method of achieving your goal, but I thought I'd address a few of the other issues raised by your question. The problems with ...
user avatar
  • 28.1k

Only top scored, non community-wiki answers of a minimum length are eligible