Questions tagged [vim-macros]

Recording a macro is a great way to perform a one-time task, or to get things done quickly when you don't want to mess with Vim script or mappings, or if you do not yet know how to do it more elegantly.

In Vim, the word macro may refer to:

  • A sequence of commands recorded to a register (this tip).
  • A mapping to expand a sequence of typed keys to a longer sequence (see tutorial).
  • A script written in the Vim script language (stored in a file with extension .vim – see :help script).

Recording a macro

Each register is identified by a letter a to z.

To enter a macro, type:

  • q<letter><commands>q

To execute the macro <number> times (once by default), type:

  • <number>@<letter>

So, the complete process looks like:

  • qq: start recording to register q
  • ... : your complex series of commands
  • q : stop recording
  • @q: execute your macro
  • @@: execute your macro again

References

34 questions
485
votes
15 answers

Vim and Ctags tips and tricks

I have just installed Ctags (to help with C++ development) with my Vim (or rather gVim), and would like to find out your favorite commands, macros, shortcuts, tips that go along with it... Share your best arsenal. What other Vim add-ons you would…
Sasha
289
votes
4 answers

In Vim, how do I apply a macro to a set of lines?

I have a file with a bunch of lines. I have recorded a macro that performs an operation on a single line. I want to repeat that macro on all of the remaining lines in the file. Is there a quick way to do this? I tried Ctrl+Q, highlighted a set of…
Jordan Parmer
  • 36,042
  • 30
  • 97
  • 119
61
votes
2 answers

How can I repeat the last VIM macro?

I know how to repeat the last command in Vim. I use .. But how can I repeat the last macro? It's a little non-comfortable to press @q everytime I want to repeat it. I tried with . but it just repeats the last command from the macro. Is there a…
Ionică Bizău
  • 109,027
  • 88
  • 289
  • 474
48
votes
2 answers

How can I save evil-mode (vim style) macros to my init.el?

I'm using evil-mode, after moving over from Vim about a year ago. I've made a number of customisations, but not yet worked out how to save vim-style keyboard macros. I can define and run them in evil-mode, using the exact same keys as in Vim. qa…
wooger
  • 500
  • 4
  • 5
7
votes
3 answers

How can I minimize keystrokes while inserting similar lines in vim?

Sometimes I need to insert some similar lines in a file which differ only in a sequence number. For example, print "func 1"; print "func 2"; print "func 3"; print "func 4"; print "func 5"; Using vim, I end up copy pasting the first line using…
Lazer
  • 90,700
  • 113
  • 281
  • 364
7
votes
1 answer

How to execute vim macro step by step (like debugging)?

Sometimes when my vim macros become too long, there are small mistakes. But without being able to execute the macro step by step, it's really hard to find the problem. Is there a possibility to step through vim macros or debug them in any way?
miu
  • 1,234
  • 2
  • 18
  • 34
7
votes
2 answers

Conditional action in a macro

In a VIM macro, how are the conditional actions handled? if condition is true do this else do something else Basic example File content: _ abcdefg The macro will do: G^ if letter is one of the following letters: a e i o u …
Ionică Bizău
  • 109,027
  • 88
  • 289
  • 474
5
votes
2 answers

One-key vim macro / search-and-run-macro interactively

I know that I can record macros with q and run them with @. I also know that I can run the last-used macro with @@. But @@ is two whole keystrokes! That's one too many! Especially if I'm running that macro a lot of times. I know about…
Samizdis
  • 1,591
  • 1
  • 17
  • 33
3
votes
1 answer

VIM Mapping doesn't work properly when I create macros

I started studying VIM around 3 days ago. And now I'm stuck creating macros. On the beginning of learning VIM I created mapping: jk -> ESC for convenience (inoremap jk ). Now my macros works correctly only if I pressed ESC; with jk they don't…
woojiq
  • 71
  • 1
  • 9
3
votes
2 answers

Vim Macro which accepts user input

Lets say I have the following text file name: John Doe description: My name is John Doe and I'm really good at vim! name: John Doe description: My name is John Doe and I'm really good at vim! name: John Doe description: My name is John Doe and I'm…
Daniel Cooke
  • 1,426
  • 12
  • 22
3
votes
1 answer

vim to generate code template dynamically

I have to write test cases which contains repetive code. The name of the method should be the ClassName delimitted with _ ex: class_name_test The object name should be classNameObj and the mock method should take ClassName.class The genericObj.call…
zilcuanu
  • 3,451
  • 8
  • 52
  • 105
3
votes
1 answer

How to unmap a insert-key on vim?

On vim, command-mode keys can be mapped through the ex command :map and insert-mode keys can be mapped through :map! . After mapped, the commands to remove the mapping from the command-mode keys and insert-mode keys are…
IanC
  • 1,968
  • 14
  • 23
3
votes
1 answer

incrementing a register n times skips values

I've been playing with 500 individual files using VIM to make one of the values unique. I won't paste that as it's a total kludge :) After making one element Unique I have a single file with 500 records separated by a blank line. Recorded a macro to…
Steve
  • 388
  • 1
  • 3
  • 14
3
votes
2 answers

VIM Smart quit macro

From years of "finger memory" my hands know that F2 is save and F3 is quit (leftovers from years of IBM editors). Consequently my first vim key mappings were to get F2 and F3 doing what they're supposed to do. In particular for F3: :map
Wes Miller
  • 2,191
  • 2
  • 38
  • 64
2
votes
1 answer

Vim macro not working because of the shortcuts overlapping with Quick Fix command

So, I was trying to write a Vim macro and the macro @p I registered below includes a cc shortcuts which deletes a line. The macro is as follows. " macro @p qp cc{j@eA,j@ejcc}, q When I run the macro @p itself, it works just fine. "…
1
2 3