Record Vim macro after running commands - vim

In my normal workflow in Vim, I sometimes realize after the fact that I could have recorded a macro after making a bunch of edits to a line. I then record the macro and type out the actions again for another line, before using the macro.
I would like to record all of the actions run on a single line to a macro, after the fact. Is this possible?
I have not tried anything yet, because I do not know if this command exists.

No, you can't record something after it happened.

Related

How to save buffer (preferably if changed) and then close the buffer but not VIM?

So, I just realized I could use marks with capital letters to go to different files. That's great! However, I'm trying to find a way to close the buffer and return to the previous one. Say I go to my header file to change or add the declaration of the function I'm writing, and then I'd like to save the file, but only if there's changes to it, to go back to working on the contents of the function. How can I do this?
There's :x, but it also quits VIM
There's :bd!, but it doesnt save the changes
There's :bw, but that's even worse (unfortunately that's w[ipeout], not w[rite]...)
There's ctrl+O, but it doesnt seem to work when I edit the file (also, it doesnt actually close the buffer)
There's :up followed by :bd, but that's two commands and VIM's about efficiency, so I'd prefer a single command if it exists
There's a few other options and variants, but none that do what I wanted, afaik
It feels like this should be simple enough to do with one command, preferably without macros/plugins/functions/snippets/etc; as close to vanilla as possible. I get the feeling I'm missing something obvious.
You could concatenate commands like so:
:w|bd
I'd like to save the file, but only if there's changes to it
:up[date]
to go back to working on the contents of the function
Press Ctrl^, or enter the command :e[dit] #
I'd prefer a single command if it exists
Set an option :set autowrite and then Vim will save the current buffer on pressing Ctrl^ automatically.

Undo ESC-keystroke in vim

I am using vim for quite some time now, but several times per day I accidentally encounter an inconvenience by hitting ESC too early.
Assume I am editing the following file:
I want to change 'house' in this line.
'house' should stay like this.
This 'house' should become 'home'
The other 'house' should also change.
I want to change house to home in all lines except the second one. (In this simple example it would easy to write a :s command and perform the task, but usually the task is more specific and manual replacements are quicker and less error prone.) I navigate to the first occurrence of house, press cw, type hone, and hit ESC. After hitting ESC I realize that by accident I typed the letter n instead of m.
I could navigate to the next occurrence of house and hit . to repeat the faulty replacement. In this case I have to fix all hones and replace the n by m afterwards.
I could fix the mistake immediately, but then I can not repeat the same house->home replacement, since . would repeat the n->m replacement.
All of this would be no problem, if I had spotted the mistake before hitting ESC. My question is, if there is a way to undo leaving the insert mode, such that . will repeat both actions? Or similarly, is there a way to tell . to repeat the last to operations?
(Of course this sounds like recording a macro, but since I end up in this situation by mistake, I have not started a macro recording.)
As far as I am concerned, there's no command history of vim in that you could repeat the last two operations.
However, there's a plugin that could help you accomplish that. It's called the RepeatLast.vim plugin to address this exact requirement. It provides a 2\. key binding. The cost for using that plugin and how the plugin actually works is that... it actually enables macro recording all the time. But if you could live with that, it should deal with this sort of situation pretty fine.

vim macro for substitution with confirmation

I want to write macro for substitution with confirmation, something like :%s/old/new/gc, where old and new are constantly changing (they are dynamically constructed by the same macro). but i cant stop recording the macro while typing colon command, and after entering that command, i have confirmation prompt replace with new (y/n/a/q/l/^E/^Y)? As you see, typing q now will quit the substitute command, and not stop the macro.
So, what i want:
i want macro, which will execute :%s/old/new/gc as i need to confirm each substitution.
Or some other automated solution for that problem. Maybe there is a way to stop recording macro while entering a colon command?
You could try regular expressions for the search replace. The following should do what's described above.
%s/a:\(\w\{-}-\{-}fact\)/a:sld-\1/gc
See:
http://vimregex.com/
http://regexone.com/

Vim Repeat dot (".") command buffer?

I really like the behavior of YankRing, which lets me access the last several things I've yanked or deleted or changed without thinking.
However a complementary feature like this is completely missing for the . repeat command, most often when I type something I really want to repeat, then accidentally overwriting that edit by pressing x to clean something up.
Often it's possible to get back some time still by visual-mode yanking what I just typed, but this is not ideal.
It should be really easy to remember the past few commands.
The question is how possible is it to extract from Vim the representation of the last command contained in whatever stores what . will do before it gets blown away?
If it is as I fear, the only way is to get a plugin to bind to every single command that could edit something, and instrument it in such a way as to store our own repeat-buffer. This is really not practical because I can already imagine how many other plugins that will break. But, I would still really really want this feature if it is possible in any way.
Unfortunately, there's no way to get and replay the command behind the . command. The only workaround is to be perpetually in macro recording mode, and use the macro register as a replacement for the . command.
This tactic is employed by the RepeatLast plugin, which might offer what you want.
Keyword completion and/or ctrl-a in insert mode should cover your needs.
A more yankring-like solution should be possible but, as you say, probably a little too intrusive. Did you look on vim.org by yourself before asking others to do it for you?

trouble creating vim macro using search history

My problem is when I create a vim macro, I used some search and replace in the search and replace history. So I use the arrow key to go up in the history to find it. But the trouble is the macro only record my arrow key activity not the command I find. So when I execute the macro again, the search history is changed and the result are messed up. Is there any way to solve this?
Yes, you can solve this problem by understanding that macros record keystrokes, not the result of commands, and act accordingly.
For example, when you use the previous search with <C-r>/ or //, the actual content of the last search register is not recorded. The next time you play that macro, the last search pattern will probably be different and your whole macro will be busted if you expect otherwise. You must actually type the search pattern or perform the search as part of the recording if you want your macro to be reliable.
To get a better grasp of how recording works in Vim, you can paste the macro you just recorded (macros are saved in registers, just like the stuff you yank) and study what's there.
qq
(do stuff)
q
"qp
Remember this fact and don't use such history recall commands when you record a macro. It may be not as convenient, but you'll probably amortize the effort over the repeated macro applications, anyway.
Actually, the shrewd practitioner can use this behavior of macros as a feature. By e.g. referring to the last search pattern (e.g. :s//...) or recalling a partial command (e.g. :w foo<Up>), one can record macros that are applicable to a wide variety of situations.
Finally, you can "salvage" a macro after you realize it's broken; as its contents are stored in a register, you can just re-edit, e.g. via:
:let #a = <C-r><C-r>=string(#a)<CR>

Resources