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
2
votes
1 answer

vim edited macro adding a ^J at the end

I was trying out "continuing macros despite error" as mentioned here. My test code looks like this this is line number 1 this is line 2 this is line 3 this is line 4 this is line 5 this is line 6 this is line 7 this is word 8 this is line 9 this is…
indojin
  • 139
  • 7
2
votes
2 answers

vim, duplicate whole line to the end of each line?

How to copy the current line content to the end of each line for the whole file ? For example: From hello world ! To hello hello world world ! !
2
votes
1 answer

vim macro mapping

In Vim, how would I set up a macro for Ctrl+Shift+another key? I did noremap and the mapping practically ignored the shift. I could press and the macro would go through.
Hien
  • 1,769
  • 3
  • 18
  • 17
2
votes
1 answer

edit a Vim macro with a range operation that refers to a tag

I created a macro with a range (http://vim.wikia.com/wiki/Ranges) operation like: :.,'bs/ .*$\n/ /ge^M I then wanted to edit it, which I would normally do using let (http://vim.wikia.com/wiki/Macros#Editing_a_macro): :let @b=':.,'bs/ .*$\n/…
user2242865
  • 567
  • 5
  • 18
1
vote
1 answer

Difference between @a="0" and @a=@0

I've the following text copied to the "0 register test test If I want to copy the content of the "0 register to the "a register I do :let @a=@0 Then, if I paste the content of "a register I obtain test test Now, to paste the content of the "0…
user515933
  • 111
  • 2
1
vote
1 answer

How to copy a Vim macro to another Vim instance without clipboard support (i.e., no `+` register)?

Have a macro where a visually selected text will be made a markdown link and once the macro executes it leaves Vim in INSERT mode: | - VISUAL selection i - cursor position in INSERT mode @l - invoke macro in "l" register Some |text| saying stuff.…
toraritte
  • 6,300
  • 3
  • 46
  • 67
1
vote
1 answer

Vim macro to Camel_Case_With_Underscores

I want a quick macro to allow me to convert text like qty_on_hand to Qty_On_Hand. Words that have no underscores should be capitalised so description would become Description. Case in the source text is not going to be consistent, so it might be…
Greg Reynolds
  • 9,736
  • 13
  • 49
  • 60
1
vote
0 answers

Reliable solution to copy/paste Vim macros

Line number 7 shows the paste of a macro I recorded. When I copy/paste it, the following 4 lines is what I get. I know we can replace ^[ character using , but thats not just one, there is ^M & that too in many places. It doesn't seem to be a…
Jikku Jose
  • 18,306
  • 11
  • 41
  • 61
1
vote
1 answer

What does ccw do in Vim macro

In the following vim macro from this article, the author gives an explanation. I understand some but not everything. :qccwcommand:wjjq This macro (which includes the record start / stop, if you’re wondering) will change the…
shin
  • 31,901
  • 69
  • 184
  • 271
0
votes
0 answers

How do I copy contents of the dot operator into a register?

I have a last command in vim which can be replicated by the 'dot' operator. However, I now wish to execute another operation, and then go back and execute the previous command. When I execute my new command, the 'dot' operator will get overriden to…
piedpiper
  • 1,222
  • 3
  • 14
  • 27
0
votes
2 answers

How can you create a vim macro that reorders lines?

Let's start off with the text 1 The 2 Quick 3 Brown 4 Fox 5 Jumps 6 Over 7 The 8 Lazy 9 Dog Let's then say that you want to make the first line the last line repeatedly with a macro. That is, this is the goal state after 1 run 1 Quick 2 Brown 3…
Jessica Pennell
  • 578
  • 4
  • 14
0
votes
1 answer

Macro to map keyword arguments to google doc strings

I am trying to write my first vim macro. The goal is to map a line containing a keyword to google doc string format. So the input is: 'keyword' : value, and desired output would be: keyword (value) : So I assume the easiest way would be to define…
kipstar
  • 23
  • 3
0
votes
1 answer

vim: playing a macro in all selected lines fail

I have a block of text like this: var a = [ {"id": 1, "name": "Name 1", "description": "Description for item #1"}, {"id": 2, "name": "Name 2", "description": "Description for item #2"}, {"id": 4, "name": "Name 4", "description": "Description…
Adriano_Pinaffo
  • 1,429
  • 4
  • 23
  • 46
0
votes
1 answer

Vim method to jump to number under the cursor

In vim, is there a more elegant way to jump to a line number under the cursor than map gn "nyw@nG "gn" or some other key combo? This necessitates ensuring the cursor is at the beginning of the string of numbers. Also, this macro COULD be a bit…
wmmso
  • 55
  • 5
0
votes
1 answer

How do you apply a macro x amount of times per line where x depends on the line

Say I have something like a & 1234567890 b & 1234567890 c & 1234567890 d & 1234567890 e & 1234567890 f & 1234567890 Is there a way to use a vim macro such that I can run a macro/command x amount of times per line, where x depends on the line? In…
andyzg
  • 1