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. ...
17
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 ...
15
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.
12
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! ...
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 ...
10
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&...
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 ...
10
votes
Accepted
Weird behaviour when repeating macro
TL;DR if you're going to make a macro, choose a letter to record to instead of any symbol.
This is very odd behavior, but it does make sense. To understand why, first we need to back up a little bit ...
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.
9
votes
How to force macro keep running even pattern is not found?
Since you're using the s command, you could simply use the e flag. From :help :s_flags:
When the search pattern fails, do not issue an error message and, in particular, continue in maps as if no ...
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 ...
9
votes
Accepted
How to put quotation marks (') in the content of a vim macro?
you add another single quote to escape your single quote inside your vim macro
let @r=':%s/''/"/g'
from https://vim.fandom.com/wiki/Macros
if your register must itself contain single quote ...
8
votes
Accepted
How to execute a macro in command mode?
Use :h execute combined with :h normal, like so:
:for i in range(1,10) | if i > 5 | exe "normal @w" | endif | endfor
Short explanation: execute/exe runs it's argument as an Ex command and normal ...
8
votes
Accepted
How to turn several commands into a macro without recording?
Well, if you want turn them into a macro specifically, then this is pretty easy to do. The thing you need to know about macro registers is that they are exactly the same as text registers that you cut/...
8
votes
Accepted
How to play recorder macro in SpaceVim?
SpaceVim uses leaderqr instead of q because it remaps q to another function.
As far as I can tell from the documentation, @ is not used for another function: so you play back macros in the usual way: ...
8
votes
Accepted
How to create a vimrc macro using :sort?
As simple as that:
nnoremap <Leader>s Vip:sort<CR>
ip is an "inner paragraph" object, i.e. a piece of text between two empty lines.
7
votes
Accepted
Run macro between buffers
Just for completeness's sake (and so that this doesn't get bumped in six months by Community):
You can put a buffer command (:b) in macros. For your case:
qqggD:b#^Mo^R":b#^Mq
Where "#" is the ...
7
votes
Accepted
Go forward to first alphabetic character
Based on DLMcMMayhem's hint, I found that following search will do what you need:
/\A*\zs\a
\A* searches for zero or more non-alphabetic characters (equivalent to [^A-Za-z])
\a searches for any ...
7
votes
Accepted
Remapping Alt key in terminal: problem with macros
The problem is caused by the way that terminals have historically handled the Alt/Meta key.
Back in the mists of history, some computers/terminals had a Meta key. When this was held down while ...
7
votes
Yank both sides of equation
This can be done with either regex+substitute or macros
Substitute. This is the same as your regex except the important parts are surrounded by \( . \) to create capture groups. These are referred ...
7
votes
Accepted
How to prevent stopping a macro on unsuccessful substitution?
I've figured it out thanks to this. One needs to put an e flag to the substitution so the substitution looks like this: :%s/ \(kb\d\+\),/ \1.0,/ge. e suppresses the error message and lets the macro ...
7
votes
How can I do calculations with vim Macros?
When I see the not so short vim solution :s/\(.*\),\(.*\)/\=submatch(0).','.(submatch(1)+submatch(2))/ following statement from https://vimways.org/2019/vim-and-the-shell/ comes to my mind:
“Vim is ...
7
votes
Accepted
How do I get my signature with a date inserted in my changelog?
Here's a simple, somewhat dumb solution, that doesn't require any plug-ins or anything.
Just add this snippet to your vimrc:
iabbrev <expr> debsig
\ ' -- Alexis Wilke <alexis@example....
7
votes
Accepted
Can macros be edited?
I want to end up in insert mode after a macro. Is there a way to do that?
Yes! You can stop recording a macro using Ctrl+O, q (Ctrl+O is a sequence to enter a single Normal-mode command from Insert ...
6
votes
Can I repeat a macro with the "dot operator"?
You can record the use of a macro in another macro, and repeat that.
qqdwq
qr2@qq
Now 3@r is like running @q six times.
6
votes
Insert code snippets?
StandardEyre's answer is the most direct answer for the body of your question, but you may also be interested in checking out one (or several) of the many snippet plugins for Vim, such as Ultisnips, ...
6
votes
How to apply a macro for every single matching pattern
Another way that also works across files is to use :h :vimgrep with g flag:
Without the 'g' flag each line is added only once.
With 'g' every match is added.
The workflow is:
...
6
votes
Accepted
Replay a vim macro until specified line number
I think you can use this for specific line
:10,15 norm! @d
If you want something like until line, you can use this instead
:.,15 norm! @d
Or a more visual way, by selecting the lines you want to ...
6
votes
Inserting the content of a register where a macro is recorded results in control characters becoming unicode characters
The ^[<80><fd>a sequence is a <Nop>, which means Vim recognizes this key sequence as valid, but it produces no action at all.
The reason why Vim will insert a <Nop> right after ...
5
votes
Accepted
Repeating multi-lines and increment numbers in specific sections?
A macro is certainly capable of doing this. Starting with the first three lines as a template
entry 100/1/1
fields value 1
other section 1 is valid
Place your cursor on the first line and ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
macro × 206register × 22
key-bindings × 20
repeated-commands × 16
vimrc × 15
vimscript × 15
regular-expression × 11
substitute × 10
replace × 9
neovim × 7
command-line × 7
functions × 7
search × 6
buffers × 6
cut-copy-paste × 6
normal-mode × 6
visual-mode × 6
terminal × 4
cursor-movement × 4
ex-mode × 4
text-generation × 4
vscode × 4
syntax-highlighting × 3
cursor-motions × 3
global-command × 3