Skip to main content
Share Your Experience: Take the 2024 Developer Survey

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

Why doesn't this macro work? (expression register)

According to this: nnoremap Q @='n.'<CR>: The @ key tells Vim to execute a macro. Rather than using a named register we use the expression register, which lets us specify the contents in ...
3 votes
2 answers
269 views

Is there a way to insert documentation comments into a Vim macro definition?

I have found Vi / Vim macros extremely obtuse to understand even a week after I've written them. The syntax of a .vimrc file supports comments on lines beginning with a double-quote character (") ...
1 vote
1 answer
101 views

Can I map the number of search matches to a key?

I'd like to map 0 to whatever number of matches I currently have. For example: This here has 5 matches. If I wanted to perform a macro on every instance, i'd like to be able to simply type: 0Q (Q is ...
0 votes
2 answers
159 views

g[lobal] with a s[ubstitution]

I would like to add a dot after the first number in a line. I tried using the command below. :g/\v^\d/exe "norm!:s/\v(\d)/\1\./^M" The command above does not work. I recorded the macro @q=':...
1 vote
1 answer
1k views

Replay a vim macro until specified line number

Suppose caret is on the line 10 and I've recorded macros @d. I need to replay macros until line 25. If I know that macros move caret one line per execution, I can calculate that it should be executed ...
6 votes
1 answer
429 views

Understand addition on numbers in Vim

I have a file with the following content: 001 Now if I start a macro (qq), yank this line (Y), paste it (p), increase the number by four (4Ctrlaa), end the macro (q), and execute this macro nine ...
0 votes
1 answer
133 views

Make autocmd fire in a macro

These are the two lines of code responsible for inserting matching characters in my matching plugin let s:cur_split = [s:cur_split[0], s:match[press] . s:cur_split[1]] cal setline(".", join(s:...
1 vote
1 answer
277 views

Vim go into insert mode when as a macro starts

When doing <ESC>qq you can record a macro into the q register (still in command mode though). How can I make <ESC>qq go into insert mode right away (without having to type i)? map qq qqi ...
-1 votes
1 answer
41 views

Put the highest count into a register and run macro that number of times, plus a contigency

I read books. A lot of books :) When a mate asked how many books I read a week he couldn't believe it was in the 3+ range so I started recording them by author and Month read since 2013. I wrote a ...
17 votes
4 answers
4k views

How can I convert words to camelCase in a macro?

I have a list of words, like this: these are some words that I want to convert to camel case I need a way to turn, for example, camel case into camelCase within a macro (or a way to turn every line ...
4 votes
2 answers
215 views

Will macro-ing the space bar mess up its command navigation?

I am considering attaching a macro to the space bar to insert a space. My question is whether this will change its normal mode behavior. For example, if I type 23<space> will it execute the ...
1 vote
3 answers
353 views

Repeating a macro to create lines of text

I am pretty new to vim and hence trying to learn some of its powerful features and ofcourse top of the list is macros. So i am trying to create a pattern like this. <tag>var 1</tag> <...
0 votes
1 answer
431 views

vim-surround not working properly in macro (ideavim)

vim-surround not working properly in macro: I want to change all li's <li><a href="/">home</a></li> <li><a href="/stuff">...
1 vote
1 answer
42 views

Update the display when :normal is busy

The [n]vim manual on :normal says "The display is not updated while ":normal" is busy". Is there a way to have it do so? If not, is there a plugin that defines its own version of :...
0 votes
2 answers
113 views

How to use a variable from register in command mode?

I have a list of cities London Madrid Paris Brasil I want to write a macro, that will add a ul tag and wrap each city in to an li. I think, i need to create a variable like: let i = 4 Than to write ...
3 votes
1 answer
810 views

Use treesitter to jump to next argument without going into nested function calls

I'm hoping to write a macro that involves jumping to a second argument of a function call and deleting it. The function arguments will sometimes include nested function calls, sometimes not, will ...
2 votes
1 answer
77 views

Vim macro to copy paste text

I would like to copy paste everything after a comment into the [] place holder of the next line. E.g.: %This is a comment Target box: [] with some more text The desired output would be: %This is a ...
0 votes
3 answers
103 views

How to map q for saving and quitting?

I was reading the Vi documentation, by Joy and Horton and on section 6, 6.9 Macros they give a way to map q to save and quit: :map q :wq^V^VCR CR When trying that out in Vim it creates a new file ^V^V ...
0 votes
1 answer
52 views

How can I write a vanilla function or macro to align characters generally?

Often I want to align code like this // start with someVariable = someValue shorter = anotherValue longerVariable = anotherValue // turn into someVariable = someValue shorter = anotherValue ...
0 votes
1 answer
36 views

Macros on lines with different structures

There are occasions where I use macros to re-format multiple lines, where the lines can have a different format. For these tasks, I use appropriate macros, but due to the different formats this can ...
1 vote
0 answers
42 views

Run macros in background while doing something else

I have a macro that calls APIs to fetch data based on information in a buffer. Is it possible to have the macro continue execution while I move to another window and work on other buffers?
5 votes
2 answers
708 views

How do I play a macro, but *slowly* (for debugging a macro)

Sometimes I do a repetitive task via a macro, but it ends up too complicated and I make a mistake somewhere. Then when I try to execute the macro, everything happens at once and I can't see where I ...
0 votes
2 answers
66 views

Delay inside Macro - Is It Possible in Vim Macros to Create a Delay Between Commands?

Does something like this exist?: let @p = "0D:!ping -c1 8.8.8.8\<CR>"<SLEEP 1 SECOND>"\<CR>iJust pinged a DNS Server\<ESC>" And let me read the ping result....
1 vote
1 answer
94 views

Individual macros function but combined macros do not

I am trying to brute-force the Caesar shift cipher in vim. I have recorded three macros f, a, and d where f copies the line and a applies a Caesar shift to all elements on the line by calling macro d ...
0 votes
1 answer
44 views

Repeat many times without macro?

Is there a way to repeat a normal mode sequence n times without making a macro for it? I thought 5:norm would be able to do that, but that actually runs the normal mode sequence once for each line of ...
0 votes
0 answers
115 views

How to search for yanked text in a macro?

How do I search for yanked text in a macro properly? I tried the following: qq (start a macro) yi" (yank text inbetween ", let's say the yanked text is foo) / press ctrl + r " (I ...
4 votes
5 answers
7k views

How to run Vim on a Chromebook?

I am working on a Chromebook. Mostly I code in an online IDE, which supports Vim mode, but from time to time I need full Vim support. For example, I need it to be able to use macros and filter content ...
63 votes
2 answers
20k 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 ...
2 votes
3 answers
94 views

How to get floating result in expression register?

I want to write a macro to set line height in fraction. font-size: 18px; line-height: 22px; The problem is with expression register. I set 2 variables @f = font-size @h = line-height And =@h/@f - ...
1 vote
2 answers
103 views

in-place replacement of code in HTML documents with TOhtml?

I'm working on some old HTML documentation, where are multiple code snippets (several lines long). I need to replace this old HTML-ized code with new one using modern styling. What I have now: ... ... ...
0 votes
1 answer
46 views

How to prevent an extra newline from being appended to macros ending with a newline? [duplicate]

If I save a macro by typing: :let @s='/{<C-v><C-m>'<Enter> which looks like: :let @s='/{^M' the macro that is actually saved is /{^M^M, with an extra linebreak (that I did not type)...
4 votes
0 answers
69 views

How to Make Macros Stop with Custom Motion?

I am writing a custom Vim motion. The problem is that when used in a macro (especially at the end of a macro), the macro does not automatically stop when the motion no longer applies. For example, if ...
5 votes
1 answer
324 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. ...
0 votes
1 answer
66 views

how to move to the end of a string / beginning of string for a macro

i want to write a makro and need to know how i can move to the end and to the beginning of a string, respektively i want to visually select the link so that i can surround it with brackets. my line ...
3 votes
1 answer
181 views

How to save a macro (for quickly printing a variable)?

I'm trying to create a macro for an action that I very frequently use in Python: printing a variable in a bright color so it can easily be found in the traces while debugging. So lets say there's ...
4 votes
4 answers
728 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 $...
3 votes
4 answers
4k views

How to create a macro in .vimrc which automatically uses enter

I want to have a macro in my .vimrc which when activated will enter a command and execute it. I want something like let @r = ':e!' but instead of it just being entered in the command line I want ...
1 vote
2 answers
574 views

What is @q doing?

I accidentally typed @q instead of :q and the file closed. I checked if I have a macro defined by using :reg q. It shows a long string of jibberish. Is it possible to find out exactly what function @q ...
3 votes
2 answers
126 views

is there something like `%` but for matching double quotes

I'm working on some HTML files that are formatted weirdly. Each individual class is on a separate line which makes the file (imo) difficult to read. Something like: <div class=" classA ...
5 votes
5 answers
2k 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 receive the following result instead: apple apple apple apple banana banana peach ...
2 votes
1 answer
491 views

Break line after the n-th comma

Suppose I have the next line 1,0,10,10,9,7,7,5,41,3,57,7,1,9 I want to split it into several lines, each containing n commands for example, for n = 4 1,0,10,10, 9,7,7,5, 41,3,57,7, 1,9
2 votes
1 answer
56 views

How do you repeat the last ex command in a macro?

If I do qq:diffthis<CR><C-w><C-w>@:q, the macro @q does not work as expected, since the command line history is not updated when ex commands are performed in macros, and ...
1 vote
1 answer
44 views

How does recording a macro differ from defining a macro directly?

In my Vim configuration, I have mapped the <C-j> key to <cmd>bn<CR>, but because the C-j key corresponds to the null character in Vim, this causes some strange issues when defining ...
17 votes
2 answers
3k 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?
0 votes
1 answer
176 views

How to join multiple lines which doesn't include space at the end

I'm working on a subtitle document (.srt) which has thousand of lines. But this issue is not about the subtitle documents, I going to export as plain text to read after. To see the dialogs line by ...
1 vote
1 answer
98 views

How to add a certain word before the names of methods in Javascript?

I am working with react. First I had a component with state I created methods and all that. Now I want to convert it into stateless component. I have add the word function before each method name. ...
2 votes
0 answers
217 views

How to split view all buffers in vim with equal distribution vertical horizontal

How can I split view all buffers in vim with a distribution between vertical and horizontal? Is there a plugin? I know how to make all buffers appear in either horizontal or vertical splits: :ball :...
0 votes
2 answers
30 views

How do I create a macro (q<letter>) which goes to the end of a line and deletes one or two characters?

I'm refactoring the logger code in my software. The old log message ends with either ); or just ; at the end of a line and I want to remove those characters and add a new line with some additional ...
0 votes
1 answer
71 views

Run macro on visual selection only. [Evaluate an Iterator to consecutive numbers]

How can we run the macro on only the visual selection? The macro that we want to run is / id: 1,^Mf1s^R=i^M<C-c>:let i += 1^M (where (as given in this SO answer) ^M happens to be the way vim ...
3 votes
1 answer
179 views

Inserting the content of a register where a macro is recorded results in control characters becoming unicode characters

If I hit Ctrl+V and then Escape while in insert or ex mode, I get ^[ as a single character. If I hit qqEscapeq and then go see :registers, I see that q contains ^[<80><fd>a, which are ...