Questions tagged [macro]

A macro is a recording of typed commands that can be executed later. Macros are stored in registers; recording a macro can be done with q, and you can run them with @.

Filter by
Sorted by
Tagged with
50
votes
2answers
12k views

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

I'm using complex repeats to record my operations (qq to record, some operations, q to stop) and I did a mistake. It is possible to see what I've recorded and correct the mistake, instead of doing it ...
30
votes
3answers
59k views

How do I stop recording while in command line?

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 ...
26
votes
2answers
4k views

Swap function arguments

When refactoring code it would be useful to be able to swap function arguments. Changing for eg: func(foo[0], bar(), baz.spam) into func(baz.spam, foo[0], bar()) Is there some way to swap ...
24
votes
1answer
3k views

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

I accidentally pressed qq, and now the status line says recording. What does that mean? How do I get out of this "recording" mode? Pressing Esc doesn't work.
18
votes
1answer
4k views

Save a macro just created in vimrc

Sometimes I create macros. Sometimes I create the same macro multiple times. Sometimes I get tired of creating the same macro over again. Can I store macros? Can I load a preset of macros? Can I make ...
17
votes
2answers
5k views

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

Let's say I have a function, named StripWhitespace. I can run it on a single file by opening the file and running :StripWhitespace. How can I automatically run that function against a folder of ...
17
votes
5answers
4k views

Can I repeat a macro with the “dot operator”?

Can I repeat a macro with the . command? For example, I'd like to be able to type: @q... and have it effectively call @q 4 times. With the default behavior, this works only if 'q' holds a single ...
14
votes
3answers
980 views

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

If I want to delete every word, that is easy, because I would do something like this: :s%/WORD//g But what if I want to do xyz to every occurrence of "WORD"? For example, let's say I want to delete ...
14
votes
2answers
2k views

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

How can I create a recursive macro so that it only runs until the end of the line? Or how to run a recursive macro until the end of the line only?
13
votes
6answers
3k views

Delete all consecutive duplicates

I have a file that looks like this. Move to 230.00 Hold Hold Hold Hold Hold Hold Move to 00.00 Hold Hold Hold Hold Hold FooBar Hold Spam Hold I would like it to look like this: Move to 230.00 ...
13
votes
1answer
2k views

Improve macro replay speed?

I recorded a simple macro and replayed it on following lines, but it seems replay speed is very slow (handle a very few lines per second). Is this expected and is there a way to improve such speed?
12
votes
3answers
11k views

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

I have a problem I can think of two general approaches for solving, but I don't know specifics for either approach. ... Level 1: cũng also Level 1: và and Level 1: như like; such ...
12
votes
2answers
1k views

How can I record a nested macro?

I want to record a macro within a macro, like so: qa [ first macro ... ] qb [ second macro ... ] q [ some more stuff, involving @b ... ] q However, the second q is ...
11
votes
3answers
799 views

How to insert repetitive text with minor difference in vim?

I want to insert text like this: similar text wordA similar text ... similar text wordB similar text ... similar text wordC similar text ... I know I can paste the text many times then use the ...
11
votes
2answers
468 views

Is there a way to detect if I'm recording in vimscript?

Is there anything like eval("is_recording()") that I can call in my vimscript to find out if I am currently recording a macro? I would have thought this works: eval("mode(1)") but that just ...
11
votes
2answers
828 views

Save last action (.) in a macro

Use Case I'm in normal mode. I've not explicitly thought about recording a macro, but I make a single command change. I can now repeat this action by using dot. I now realize it's a useful action to ...
11
votes
1answer
888 views

How can I use insert mode completion in a macro?

I'm trying to define a macro which quickly generates a list of random-ish words. I want the macro to turn this into a list like a b c d e into this arezzo bywords czars détente eyrie What I have ...
8
votes
5answers
1k views

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

I wanted to write this: A = mapping[0] B = mapping[1] C = mapping[2] D = mapping[3] E = mapping[4] F = mapping[5] G = mapping[6] H = mapping[7] I = mapping[8] J = mapping[9] K = mapping[10] L = ...
8
votes
3answers
1k views

Visual Block Mode edit with sequential number [duplicate]

Suppose I'd like to create a list - "1" - "2" - "3" what Is the smartest way to create such? My attempt would be to i- "1"ESCyypp to get - "1" - "1" - "1" and then jump to the second line's 1 ...
8
votes
2answers
2k views

Insert code snippets?

I'm creating a folder with common code snippets I use. I want to create a quick command in vim to insert them, for example something like: :snippet codeblock1 Ideally it would be a single command ...
8
votes
1answer
677 views

Continue an ex mode command after “norm”?

I often use extremely long macros when handling repetitive editing and refactoring. Whenever possible I write these as ex-mode commands rather than recording them as macros, because they are easier ...
7
votes
3answers
163 views

mapping to enclose symbol under cursor with an expression that contains the symbol

Have been reading vim scripting posts for an hour, but I'm afraid I don't have the skills to apply these posts to my problem. Editing math in latex documents, I'm frequently wanting to replace math ...
7
votes
1answer
351 views

Why does vim add ^J to the end of my macros?

I wrote short macro that helps me grep for the word under the cursor. Here it is in my .vimrc: let @a=":! git grep ^R^W^M" However, when I open vim and run :reg a, I see the following: "a :! git ...
6
votes
2answers
1k views

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

Is there a way to programmatically check if Vim is currently in the recording mode triggered by q?
6
votes
3answers
4k views

Multiple search-replace operations on a selection

I have a LaTeX file with a bunch of commands of the form \label{<word>} where the <word> represents a string that contains special characters, namely á é í ó ú ý ð þ æ ö. I'm looking for a ...
6
votes
1answer
799 views

How do I ignore regex failure while running a macro?

If I have a regex in a recorded macro and then run it over a line that the regex fails to match the macro stops. For example, I have a list of files. family-reunion-2015.webm road-festival-20150607....
6
votes
1answer
127 views

How to prevent stopping a macro on unsuccessful substitution?

I've been trying to record a rather simple macro with two substitutions. The whole macro is: dd"+pkdd:%s/\t/, /g :%s/ \(kb\d\+\),/ \1.0,/g "+y$ However, the second substitution is there to reformat ...
6
votes
2answers
112 views

Swap register key from @ to another more accessible key

I used many macros assigned to registers, for instance I have this line in my .vimrc: let @p='^[O</p>^[0jj' I just type @p and it closes the tag, very convenient. PROBLEM: On my new keyboard @...
6
votes
2answers
249 views

How to get the count given to a macro?

When executing an ex command I can use the variable v:count to get the count which was given to this command. From :h v:count, if I create this mapping: :map _x :<C-U>echo "the count is " . v:...
6
votes
1answer
137 views

Swap two keys in such a way that works with both `f` and macros

I have swapped my number keys with their symbols, because the symbols occur much more frequently than numbers in many programming languages, like C or Haskell. You can see my key-swap function in my ....
5
votes
3answers
270 views

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

I have a macro to swap text. For single line, it's running without problem. But if I run this macro many times and one line is empty or different pattern, it will print below error and stop. Is ...
5
votes
2answers
1k views

Go forward to first alphabetic character

Is there a way to go forward to the first letter, maybe using f other than specifying the letter itself? In other words I want to skip over whitespace, symbols and digits. The reason I ask is that I ...
5
votes
5answers
930 views

How to apply a register to a command?

I have the given file 4 apples 2 bananas 6 peaches 3 grapes 1 pineapple 5 pears 3 tomatoes What I want to do is to recieve the following result instead: apple apple apple apple banana banana peach ...
5
votes
1answer
196 views

Weird behaviour when repeating macro

I wanted to delete every 2 lines and then skip one, so I tried recording a macro. I did q1 then d2dj to delete 2 lines and go one down, then q and @1 to execute it. The first time it worked well. ...
5
votes
2answers
180 views

Efficient ruby line splitting

I never made any plugins but I would like to get some advice for a specific case. I'm writing a lot of ruby. Sometimes I have very long lines that I want to split. Example: MyModel.first_method....
4
votes
2answers
568 views

Apply macro to every `n`-th line

I would like to apply macro a to a set of lines. For consecutive lines, this can be done by :5,10norm! @a (https://stackoverflow.com/questions/390174/in-vim-how-do-i-apply-a-macro-to-a-set-of-lines)....
4
votes
3answers
309 views

Reverse a set of paragraphs

Is there a good way to reverse a set of paragraphs (navigable via cursor motion { and })? My practical use case is reversing the order of blocks of latex code (ie, reversing the order of jobs in a ...
4
votes
1answer
1k views

How to execute a macro in command mode?

The contents of register w are xx. When I try to run the following command: :for i in range(1,10) | if i > 5 | @w | endif | endfor It throws an error saying that E492: Not an editor command xx. ...
4
votes
1answer
83 views

Can the command-line window be entered without interrupting macro recording?

I'm having a problem using the command-line window (help cmdwin), which is extremely useful, but it doesn't play well with macros. cmdwin is invoked with q:, so if there is a macro being recorded, ...
4
votes
2answers
2k views

Is there a way that I can identify which window is a terminal after calling the `:term` command in neovim?

Is there way I can identify a window by a number in neovim and then jump to it with a macro ? Or better yet...Is there a way that I can identify which window is a terminal after calling the :term ...
4
votes
1answer
50 views

How to save a macro which does not shift cursor position

I have to do </^< a whole lot. So I created a macro: let @w = '</^<Enter' (where Enter is entered via Ctrl+V M). But when I run it with @w it shifts the cursor down one line - which is NO ...
4
votes
1answer
856 views

insert register contents on a macro

How can I include the contents of a register when recording a macro? If I use Ctrl+R x I get a "pointer" to register x, i.e., my macro will contain ^Rx instead of the register contents. Motivation: ...
4
votes
2answers
103 views

Make a frequent RSpec edit easy & repeatable

Writing unit tests in RSpec, I frequently change something like this, it 'eats pizza' do pizza = build(:pizza) expect(subject.eats?(pizza)).to be_true end to this, let(:pizza) { build(:pizza) } ...
3
votes
2answers
361 views

Copy a word in macro without using a register

I want to create a macro that will take a word logThis, which is selected in visual mode, and turn it into this: console.log 'printing logThis' console.log JSON.stringify(logThis, undefined, 2) I ...
3
votes
3answers
3k views

Run macro over a visual selection with line movements

I can find an answer to a similar problem here, however although I can get that example to function I can't get what I want to do to work. Here is the example text to be edited, a partial section of ...
3
votes
2answers
342 views

How to execute several macros with one command?

I have three macros that I have created separately over time. a performs a search and replace, b adds tags to every line and c appends text at the beginning and the end of the file. I execute them ...
3
votes
1answer
155 views

Is it safe to do other stuff on a system while a macro runs?

If I'm running a complex macro on a large file that I expect to take a few minutes, is it OK to do other stuff on the system (e.g. opening another application, typing in a different text editor, etc?) ...
3
votes
5answers
464 views

How do I get my signature with a date inserted in my changelog?

Whenever I make an update to the changelog of a Debian package, I have to update the signature with the correct date (well, it's not mandatory, but it's really nice to have). The signature looks like ...
3
votes
1answer
718 views

Remapping Alt key in terminal: problem with macros

I'm using xfce4-terminal. I remapped most Alt+key combinations in a file that is loaded in the .virmc. The file (called alt_bindings.vim, only loaded in vim 8 terminal mode) is like this: set <M-...
3
votes
4answers
447 views

How to get around this common multiple-cursors use case?

I have a lot of difficulty doing things like the following in Vim: From 1 'apple' 2 'orange' 3 'pear' 4 $fruit1 = 5 $fruit2 = 6 $fruit3 = To 1 2 3 4 $fruit1 = 'apple'; 5 $fruit2 = 'orange'; 6 $...