How do I repeat the last n changes in Vim? - vim

Doing . repeats the last change. Doing 2. repeats the last change two times.
But imagine I want to repeat the change before the last one. How do I do it in Vim?

Don't think you can, see :help . However, what you can do is to record a macro for your edits, you have a lot of registers to choose from {0-9a-zA-Z"} (uppercase to append).
Then use e.g. #u for edit 1, #t for edit 2 and so on.
Great tips about recording from Best of VIM Tips
" Recording (BEST TIP of ALL)
qq # record to q
your complex series of commands
q # end recording
#q to execute
## to Repeat
5## to Repeat 5 times
qQ#qq : Make an existing recording q recursive *N*
" editing a register/recording
"qp :display contents of register q (normal mode)
<ctrl-R>q :display contents of register q (insert mode)
" you can now see recording contents, edit as required
"qdd :put changed contacts back into q
#q :execute recording/register q
Have a look at these for more hints for repeating:
:& last substitute
:%& last substitute every line
:%&gic last substitute every line confirm
g% normal mode repeat last substitute
g& last substitute on all lines
## last recording
#: last command-mode command
:!! last :! command
:~ last substitute
:help repeating

I wrote the RepeatLast.vim plugin to address this exact requirement. It provides a 5\. key binding to repeat the last 5 changes (including movements) and 2\D to drop/forget the last 2 actions.
It works by enabling macro recording all the time, which may not be desirable for everyone. But if you can live with that, it works in 99% of use cases.
Latest version: https://github.com/joeytwiddle/RepeatLast.vim (Please feedback!)
Caveats:
Please :set ch=2 so that the first line of output won't be hidden by the "recording" message.
The 1% of times it fails to work as intended are usually due to:
Difficulties triggering the CursorHold event slowly without losing
fast-repeated keystrokes
Undesirable recording of [Space] and
[Enter] keys when the user is responding to a prompt.
Training your q muscle to pre-emptively record macros might be a better approach in the long term. ;-)

Based on Fredrick Phil's answer, here is an example:
Recording your macro
The following shows how to record a macro to delete everything in and including a quoted string and store in register d. The command to delete a string is da". So to store this command in macro register d we can simply do this:
qdda"q
Notice it starts and ends with a q. The second character is the register, in this case d for delete. But we could have given it any letter or number. The remaining characters da" is our command.
Using our macro
Now that our macro is recorded we can invoke it by using the # symbol followed by the register:
#d
Repeating the last macro command
To use the most recently invoked macro command again:
##
Unrelated info:
In this example, we used da" which stands for delete a quoted string. (If you instead wanted to delete everything inside the quoted string, but not the quotation marks themselves you can instead use di" instead.).

Record Your "Edits"
yes! you can do this in vim! 😎
One of Vim's most useful features is its ability to record what you type for later playback. This is most useful for repeated jobs that cannot easily be done with .
To start recording
press q in normal mode followed by a letter (a to z)
That starts recording keystrokes to the specified register. Vim displays recording in the status line
Type any normal mode commands, or enter insert mode and type text
To stop recording
ending in normal mode, come to normal mode if you are not, and press q
ending in insert mode, press Ctrl+O, this will temporarily get you into normal mode, and then press q
To playback your keystrokes/recording
press # followed by the letter previously chosen
Typing ## repeats the last playback
References
Vim Fandom: Recording keys for repeated jobs
Vim Fandom: Macros
Quora - How do you stop recording a Vim macro when in insert mode?

Related

How to repeat a navigation command in Vim

The . key can be used to repeat the last insert command. However, we might do some navigation that is not part of the insert, but we want it repeated.
Imagine commenting out lines like so:
// line of text
// line of text
line of text
line of text
The insert command is to put the two forward slashes and a space. That can be repeated using the . key. The navigation would be to navigate down one line and then left some number of characters. That part is not captured by the . key command.
How can we achieve this functionality? I read that it was not available in Vi some years ago, but I'm wondering if it exists now in the latest version of Vim.
Press qX, where X is any of the writable registers (typically: pick any lowercase letter).
Do whatever actions you want to record.
Press q again to stop recording.
Press #X (where X is the same register) to play it back (count times, if used with a count).
Press ## to replay the most recently used macro (count times).
I read that it was not available in Vi some years ago, but I'm wondering if it exists now in the latest version of Vim.
If the Vim docs are to be believed, Vi did not support recording (steps 1-3), but did support #. Then you would have to manually yank the characters into the target register with "Xy<motion> or some other register-writing command. That also works under Vim, but I can't recommend it because it is much more error prone.
Another approach would be "block select then edit" approach:
ctrl + v - block select
then go down j or down-arrow
shift + i will put you in insert mode. Make the change here where you want it to be reflected on all the other lines you've selected.
esc twice will show/repeat that change you made on the line one.
If you have a big range of similar lines and want to put // at the beginning of it, you can do something like:
:15,25norm! I//<space>
You can also use visual area (vip selects an entire paragraph)
:'<,'>norm! I//<space>
using a pattern
:g/TODO/norm! I//<space>

How to efficiently paste over multiple lines again and again with the same text in Vim?

Objective
Yank a line and use it to overwrite some of the lines following it.
Assumption
It is preferable in this case to manually select the lines to apply the substitution to. In other words, automated find and replace is not desired.
Analogy
Think of this process as creating a “stamp” from a line of text and going through a list of items—each item being a line of text following the “stamp” line—and deciding whether that line should be overridden using the contents of the “stamp” or not (in the former case, replacing the line with the “stamp”, of course).
This last step of triggering the replacement of the line under the cursor with the contents of the stamp, should be as easy as possible; preferably, as easy as pressing . (repeat last change) or ## (execute the contents of macro register #).
Issue
The straightforward workflow is, of course, as follows.
Position the cursor on the line to be copied (using movement commands).
Enter line-wise Visual mode (via the V command).
Copy selected text (using the y command).
Manually position the cursor onto the line to be replaced (using movement commands).
Enter Visual mode again to select the text to be replaced (using the V command).
Paste over the selection (using the p command).
However, this approach does not work when the replacement has to be done multiple times. Specifically, replacing the text on step 6 overrides the (unnamed) register containing the line initially copied and intended to be used as a “stamp”.
What I have tried
I have tried using "_y to either yank or delete into the _ register, avoiding the loss of the contents of the stamp, but I am looking for something that ends up being quick and comfortable to type as I manually go through the list and apply replacements where I see fit.
What I would prefer not to use
I would rather not use macros or “remaps” for this, if I can help it.
Illustrative sample file
See the sample starting file below, followed by the desired final stage, for further clarity.
Sample file, starting condition
At this stage, I select the blueberry and make it my “stamp”.
blueberry
apple
banana
coconut
apple
banana
coconut
apple
banana
coconut
Sample file, desired final state
After having moved through the list, I have applied some replacements, “stamping” over some lines, making them the same as the “stamp” blueberry line.
blueberry
apple
banana
blueberry
apple
banana
coconut
apple
banana
blueberry
To make your workflow work as expected, you need to paste from the previous yank register "0, rather than the default register.
So use Vy (or yy, which is the same) to yank the first line as before, then position the cursor over the line you want to replace, and do
V"0p
this replaces the current line with the previously yanked text, but doesn't overwrite the yanked text. I hope I understood you correctly!
EDIT 1: repeating using a macro
I was surprised that this operation isn't repeatable using ., but this is presumably due to the use of visual mode. To repeat the operation using a macro, do this:
qqV"0pq
The macro can then be repeated by pressing #q or ##.
EDIT 2: repeating using .
Here's an attempt at making it repeatable using . by not using visual mode. After yanking the stamp line and moving the cursor, do this:
"_S<c-r>0<delete>
which uses the insert mode <c-r> command to insert the contents of register 0. Note that the <delete> is necessary because the stamp line contained a carriage return. If it did not (i.e. yanking using y$ rather than yy) the <delete> could be omitted.
I don't think you are going to reach your goal without at least a little bit of "remapping".
I've been using this one for a "long" time:
vnoremap <leader>p "_dP
p and P still work as usual and I simply hit ,p over a visual selection when I want to repeat the same paste later. You could also map a single function key to make the whole thing quicker.
Also, do you know about the c flag for substitutions?
:%s/coconut/blueberry/c
will ask for your confirmation for each match.
Many answers here outline the general keys or commands. I've turned them into my ReplaceWithRegister plugin, which also handles many corner cases, and allows quick repeat via the . command. I also use your described create stamp and replace technique often, and found my script indispensable. Should you not like it, the plugin page also has links to alternative plugins.
A really easy solution: just put this script in your .vimrc, then toggle off the "buffer-overwriting" side-effect behavior of the delete key by typing ,, (two commas) to enter "no side effects" mode.
In this mode your workflow now works exactly as you described: yank whatever you like, then select, paste, and delete freely and repeatedly -- your buffer always remains intact. Then type ,, again if you wish to restore vim's normal buffer-altering behavior.
The script is the accepted answer here:
vim toggling buffer overwrite behavior when deleting
One can resort to Ex commands to achieve the said workflow.
For a single substitution, yank the “stamp” line (with yy, Vy,
:y, or otherwise), then repeatedly use the combination of the
:put and :delete commands:
:pu|-d_
Like any other Ex command, this one can be easily repeated with the
#: shortcut (see :help #:) — unless another Ex command was issued
in the meantime (in which case that command would be repeated instead).
Of course, you can also record the above Ex command as a macro and
invoke it that way, too.
Starting with your cursor at the start of the line to be duplicated:
y$ to yank the whole line (excluding the linefeed).
j and k to advance to the next line to be replaced (repeating as needed)
Replace the line with your yanked text
C<c-r>0<esc>0 (first time)
. (subsequent times)
If there are more lines to be replaced, goto 2.
The cursor will remain in column zero after each step.

How do I get fine-grained undo in Vim

I find Vim's undo to be a bit too coarse. E.g. if I type something like this:
a // to go into edit mode
to be or not to ve
<esc> // to exit insert mode
Oops! I made a typo. I want to start undoing so I press u, but then it clears the whole line. Is there a way to undo word-by-word or character-by-character?
You can break undos via :help i_ctrl-g_u. You can map this if you want for every character, but that might a little bit complicated. Mapping this to space button is a way.
:inoremap <Space> <Space><C-g>u
After that every word can be undo via u
So as you see from the others what you are asking for doesn't exist in Vi (AFAIK).
Undo undoes what your last action was. If your last action was to enter insert mode and then add a line and then exit insert mode. That will be undone, however if from the default mode you hit the "x" key then you will delete 1 character or if in visual mode with text selected the text will be deleted. If you hit undo then you will restore that one character or the text that was selected.
...You should think of this as an action, and actions can be atomically undone or restored
As mentioned previously if you wish to delete the previous word then you should be able to hit Ctrl + w and delete the previous word while remaining in insert mode.
If you exit insert mode you can navigate (motion) back a word with "b" forward a word with "w" to the end of a word with "e", and can cut (which leaves you in insert mode) with "c" or delete with "d". Both actions cut and delete can accept a motion following them so you can delete the current word / up to the next word with "dw" or cut the previous word with "cb"
This concept becomes more useful when you remember to use the "." command (in normal mode). This command is to repeat the last action. I have used this many times to find and replace a small group of words in a file (It is especially useful if you are paranoid about changing too much). The scenario would be the following:
File:
This is my post
really it is a test
this is the middle
This is the end
if I wanted to replace "is" with "was" I could write:
%s/\<is\>/was/g
however if I wanted to change the first line and the third line "is" to "was" (and I didn't know their line numbers, and I wanted to see if there were any other places I wanted to change is to was I could type
"/is"
hit "n" until I reach the place I want substituted, and then hit "cw" and type "was"
I can now hit "n" until I reach another place I want substituted and hit ".", and that will replace "is" with "was" (Note: my search string didn't limit to the word "is", just the two characters "is" so "This" & "this" will match in this case)
No, it is not possible and is actually not necessary either. Vim has a million ways of dealing with that. Try cb for example. Or bC. Or brb. Or Tspace to jump back instead of b. Or ciw.
You can, of course use most of these solutions in insert mode (by pressing CTRLo first), or bind one to your favorite key combination (:help map and :help imap).
On Linux, using control-w while in input mode deletes the last 'word'.

How do I use vim registers?

I only know of one instance using registers is via CtrlR* whereby I paste text from a clipboard.
What are other uses of registers? How to use them?
Everything you know about VI registers (let's focus on vi 7.2) -- share with us.
Registers in Vim let you run actions or commands on text stored within them. To access a register, you type "a before a command, where a is the name of a register. If you want to copy the current line into register k, you can type
"kyy
Or you can append to a register by using a capital letter
"Kyy
You can then move through the document and paste it elsewhere using
"kp
To paste from system clipboard on Linux
"+p
To paste from system clipboard on Windows (or from "mouse highlight" clipboard on Linux)
"*p
To access all currently defined registers type
:reg
I was pleased when I discovered the 0 register. If you yank text without assigning it to a particular register, then it will be assigned to the 0 register, as well as being saved in the default " register. The difference between the 0 and " registers is that 0 is only populated with yanked text, whereas the default register is also populated with text deleted using d/D/x/X/c/C/s/S commands.
I find this useful when I want to copy some text, delete something and replace it with the copied text. The following steps illustrate an example:
Yank the text you want to copy with y[motion] - this text is saved in " and 0 registers
Delete the text you want to replace with d[motion] - this text is saved in " register
Paste the yanked text with "0p
where " is the command to use a register for the next command.
On the final step, if you were to paste from the default register (with p), it would use the text that you had just deleted (probably not what you intended).
Note that p or P pastes from the default register. The longhand equivalent would be ""p (or ""P) and "0 holds the last yank, "1holds the last delete or change.
For more info see :help registers.
One of my favorite parts about registers is using them as macros!
Let's say you are dealing with a tab-delimited value file as such:
ID Df %Dev Lambda
1 0 0.000000 0.313682
2 1 0.023113 0.304332
3 1 0.044869 0.295261
4 1 0.065347 0.286460
5 1 0.084623 0.277922
6 1 0.102767 0.269638
7 1 0.119845 0.261601
Now you decide that you need to add a percentage sign at the end of the %Dev field (starting from 2nd line). We'll make a simple macro in the (arbitrarily selected) m register as follows:
Press: qm: To start recording macro under m register.
EE: Go to the end of the 3rd column.
a: Insert mode to append to the end of this column.
%: Type the percent sign we want to add.
<ESC>: Get back into command mode.
j0: Go to beginning of next line.
q: Stop recording macro
We can now just type #m to run this macro on the current line. Furthermore, we can type ## to repeat, or 100#m to do this 100 times! Life's looking pretty good.
At this point you should be saying, "But what does this have to do with registers?"
Excellent point. Let's investigate what is in the contents of the m register by typing "mp. We then get the following:
EEa%<ESC>j0
At first this looks like you accidentally opened a binary file in notepad, but upon second glance, it's the exact sequence of characters in our macro!
You are a curious person, so let's do something interesting and edit this line of text to insert a ! instead of boring old %.
EEa!<ESC>j0
Then let's yank this into the n register by typing B"nyE. Then, just for kicks, let's run the n macro on a line of our data using #n....
It added a !.
Essentially, running a macro is like pressing the exact sequence of keys in that macro's register. If that isn't a cool register trick, I'll eat my hat.
Other useful registers:
"* or "+ - the contents of the system clipboard
"/ - last search command
": - last command-line command.
Note with vim macros, you can edit them, since they are just a list of the keystrokes used when recording the macro. So you can write to a text file the macro (using "ap to write macro a) and edit them, and load them into a register with "ay$. Nice way of storing useful macros.
The black hole register _ is the /dev/null of registers.
I use it in my vimrc to allow deleting single characters without updating the default register:
noremap x "_x
and to paste in visual mode without updating the default register:
vnoremap p "_dP
If you ever want to paste the contents of the register in an ex-mode command, hit <C-r><registerletter>.
Why would you use this? I wanted to do a search and replace for a longish string, so I selected it in visual mode, started typing out the search/replace expression :%s/[PASTE YANKED PHRASE]//g and went on my day.
If you only want to paste a single word in ex mode, can make sure the cursor is on it before entering ex mode, and then hit <C-r><C-w> when in ex mode to paste the word.
To make it more convenient :
cnoremap <c-g> <c-r>"
cnoremap <C-Right> <c-r><c-w>
A cool trick is to use "1p to paste the last delete/change (, and then use . to repeatedly to paste the subsequent deletes. In other words, "1p... is basically equivalent to "1p"2p"3p"4p.
You can use this to reverse-order a handful of lines:
dddddddddd"1p....
I think the secret guru register is the expression = register. It can be used for creative mappings.
:inoremap \d The current date <c-r>=system("date")<cr>
You can use it in conjunction with your system as above or get responses from custom VimL functions etc.
or just ad hoc stuff like
<c-r>=35+7<cr>
q5 records edits into register 5 (next q stops recording)
:reg show all registers and any contents in them
#5 execute register 5 macro (recorded edits)
From vim's help page:
CTRL-R {0-9a-z"%#:-=.} *c_CTRL-R* *c_<C-R>*
Insert the contents of a numbered or named register. Between
typing CTRL-R and the second character '"' will be displayed
<...snip...>
Special registers:
'"' the unnamed register, containing the text of
the last delete or yank
'%' the current file name
'#' the alternate file name
'*' the clipboard contents (X11: primary selection)
'+' the clipboard contents
'/' the last search pattern
':' the last command-line
'-' the last small (less than a line) delete
'.' the last inserted text
*c_CTRL-R_=*
'=' the expression register: you are prompted to
enter an expression (see |expression|)
(doesn't work at the expression prompt; some
things such as changing the buffer or current
window are not allowed to avoid side effects)
When the result is a |List| the items are used
as lines. They can have line breaks inside
too.
When the result is a Float it's automatically
converted to a String.
See |registers| about registers. {not in Vi}
<...snip...>
I use the default register to grep for text in my vim window without having to reach for the mouse.
yank text
:!grep "<CTRL-R>0"<CR>
Use registers in commands with #. E.g.:
echo #a
echo #0
echo #+
Set them in command:
let #a = 'abc'
Now "ap will paste abc.
One overlooked register is the '.' dot register which contains the last inserted text no matter how it was inserted eg ct] (change till ]). Then you realise you need to insert it elsewhere but can't use the dot repeat method.
:reg .
:%s/fred/<C-R>./
A big source of confusion is the default register ". It is important to know the way it works. It is much better if the default register is avoided most of the times. The explanation from the Vim documentation:
Vim fills this register with text deleted with the "d", "c", "s", "x" commands
or copied with the yank "y" command, regardless of whether or not a specific
register was used (e.g. "xdd). This is like the unnamed register is pointing
to the last used register.
So the default register is actually a pointer to the last used register. When you delete, or yank something this register is going to point to other registers. You can test that by checking the registers. There is always another register that is exactly the same as the default register: the yank register ("0) , the first delete register("1) , small delete register("-) or any other register that was used to delete or yank.
The only exception is the black hole register. Vim doc says:
An exception is the '_' register: "_dd does not store the deleted text in any
register.
Usually you are much better off by using directly: "0, "- and "1-"9 default registers or named registers.
My favorite register is the ':' register. Running #: in Normal mode allows me to repeat the previously ran ex command.
So we can verify some commands in with :MY_commandXXXXX then put MY_commandXXXXX in vimrc
My friend Brian wrote a comprehensive article on this. I think it is a great intro to how to use topics. https://www.brianstorti.com/vim-registers/
My favorite feature is the ability to append into registers by using capital letters. For example, say you want to move a subset of imports from buffer X to buffer Y.
Go to line x1 in buffer X.
Type "ayy to replace register a with the content of line x1.
Go to line x5.
Type "Ayy (capital A) to append line x5 at the end of register a.
Go to buffer Y and type "ap to paste
<content of line x1>
<content of line x5>

How to repeat a command with substitution in Vim?

In Unix the ^ allows you to repeat a command with some text substituted for new text. For example:
csh% grep "stuff" file1 >> Results
grep "stuff" file1
csh% ^file1^file2^
grep "stuff" file2
csh%
Is there a Vim equivalent? There are a lot of times I find myself editing minor things on the command line over and over again.
Specifically for subsitutions: use & to repeat your last substitution on the current line from normal mode.
To repeat for all lines, type :%&
q: to enter the command-line window (:help cmdwin).
You can edit and reuse previously entered ex-style commands in this window.
Once you hit :, you can type a couple characters and up-arrow, and it will character-match what you typed. e.g. type :set and it will climb back through your "sets". This also works for search - just type / and up-arrow. And /abc up-arrow will feed you matching search strings counterchronologically.
There are 2 ways.
You simply hit the . key to perform an exact replay of the very last command (other than movement). For example, I type cw then hello to change a word to "hello". After moving my cursor to a different word, I hit . to do it again.
For more advanced commands like a replace, after you have performed the substition, simply hit the : key then the ↑ up arrow key, and it fills your command line with the same command.
To repeat the previous substition on all lines with all of the same flags you can use the mapping g&.
If you have made a substitution in either normal mode :s/A/B/g (the current line) or visual mode :'<,>'s/A/B/g (lines included in the current selection) and you want to repeat that last substitution, you can:
Move to another line (normal mode) and simply press &, or if you like, :-&-<CR> (looks like :&), to affect the current line without highlighting, or
Highlight a range (visual mode) and press :-&-<CR> (looks like :'<,'>&) to affect the range of lines in the selection.
With my limited knowledge of Vim, this solves several problems. For one, the last visual substitution :'<,'>s/A/B/g is available as the last command (:-<UP>) from both normal and visual mode, but always produces an error from normal mode. (It still refers to the last selection from visual mode - not to the empty selection at the cursor like I assumed - and my example substitution exhausts every match in one pass.) Meanwhile, the last normal mode substitution starts with :s, not :'<,'>s, so you would need to modify it to use in visual mode. Finally, & is available directly from normal mode and so it accepts repetitions and other alternatives to selections, like 2& for the next two lines, and as user ruohola said, g& for the entire file.
In both versions, pressing : then & works as if you had pressed : and then retyped s/A/B/, so the mode you were in last time is irrelevant and only the current cursor line or selection determines the line(s) to be affected. (Note that the trailing flags like g are cleared too, but come next in this syntax too, as in :&g/: '<,'>&g. This is a mixed blessing in my opinion, as you can/must re-specify flags here, and standalone & doesn't seem to take flags at all. I must be missing something.)
I welcome suggestions and corrections. Most of this comes from experimentation just now so I'm sure there's a lot more to it, but hopefully it helps anyway.
Take a look at this: http://vim.wikia.com/wiki/Using_command-line_history for explanation.

Resources