21
votes
Accepted
How do I apply a set of keystrokes to every occurrence of a word?
You can use the :global and :normal commands for this. The :global command has the following syntax:
:global/{pattern}/{command}
This will run "command" on every occurrence of "pattern". You can ...
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. ...
16
votes
Accepted
Delete all consecutive duplicates
I think the following command should work :
:%s/^\(.*\)\(\n\1\)\+$/\1/
Explanation :
We use the substitution command on the whole file to change pattern into string :
:%s/pattern/string/
Here ...
16
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 ...
14
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.
13
votes
Accepted
How do I stop a recursive macro at the end of the line?
There is probably a simpler method but maybe you could try the following.
Let's say you will use register q to record your recursive macro.
At the very beginning of the recording, type:
:let a = ...
13
votes
Accepted
Continue an ex mode command after "norm"?
If you have a sequence of keystrokes that you want to execute in normal mode from the command line, you can use the :normal command.
However, by default the :normal command can't be followed by ...
12
votes
How do I stop a recursive macro at the end of the line?
Use l
A recursive macro will stop as soon as it encounters a command that fails. Therefore, to stop at the end of a line, you need a command that will fail at the end of the line.
By default*, the l ...
12
votes
Delete all consecutive duplicates
Try the following:
:%s;\v^(.*)(\n\1)+$;\1;
As with saginaw's answer, this uses Vim's :substitute command. However, it takes advantage of a couple of extra features to improve readability:
Vim lets ...
12
votes
Can Vim write incremental lines? i.e. on each line A becomes B, and 1 becomes 2
While it can be done with scripting, if you have a very recent version of Vim (e.g. the one in Arch Linux), you can use g Ctrl-a to increment a visual block. In your case:
Write the original line: A =...
11
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
Apply macro to every `n`-th line
How about writing a 2nd macro that moves down n lines and then executes @a? Using a mark you can easily jump back to your original position.
E.g., having defined your macro in register a, press qb to ...
9
votes
Accepted
How do I ignore regex failure while running a macro?
Use the /e flag. From :help s_flag
[e] When the search pattern fails, do not issue an error message and, in
particular, continue in maps as if no error occurred. This is most
useful to ...
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 ...
8
votes
Delete all consecutive duplicates
If you want to remove ALL adjacent identical lines, not just Hold, you can do it extremely easily with an external filter from within vim:
:%!uniq (in a Unix environment).
If you want to do it ...
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.
8
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 ...
7
votes
How to move to the next matching string and then hit delete twice in a macro?
One way of deleting two new lines after every > is as follows
:%s/>\n\n/> /
To explain, this is a global search and replace (:%s) finding > followed by two newline characters (>\n\n) ...
7
votes
Accepted
Macro won't exit insert mode with Esc
It looks like your ^[ is not a literal <Esc>. More likely a ^ followed by a [ (two characters);
You are supposed to insert a literal escape character (a single character displayed as ^[). This ...
7
votes
How to insert repetitive text with minor difference in vim?
You can use the following function:
function! GenerateLines()
let firstpart="similar text part 1"
let secondpart="similar text part 2"
let words=["wordA","wordB","wordC","wordD","wordE"]
...
7
votes
Leader versus macros
I can see some confusion if you have been reading the help docs because they use some similar names even though these two things are pretty different.
The @ command will "play back" the contents of ...
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 ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
macro × 181register × 18
key-bindings × 15
repeated-commands × 14
vimscript × 13
vimrc × 11
regular-expression × 11
substitute × 9
replace × 8
neovim × 7
search × 6
command-line × 6
buffers × 6
visual-mode × 6
functions × 6
cut-copy-paste × 5
normal-mode × 5
cursor-movement × 4
ex-mode × 4
text-generation × 4
vscode × 4
terminal × 3
cursor-motions × 3
count × 3
syntax-highlighting × 2