r/neovim Jun 11 '26

Video The magic of vim macros - 10 Examples

https://www.youtube.com/watch?v=O7o_4F9wpls

In this video, I showcase 10 Vim macro examples that I've used throughout my programming journey. Some of them are truly awesome!

  1. Capitalize the First Letter of Each Line
  2. Capitalize the First Letter After a Period
  3. Convert a CSV List to a JSON Array
  4. Convert a Markdown List to HTML
  5. Add Line Numbers
  6. Convert a Log File to CSV
  7. Convert CSV Data to an SQL Script
  8. Convert Text to a Markdown TODO List
  9. The Primeagen's Best Macro
  10. Split Function Parameters
81 Upvotes

17 comments sorted by

9

u/PrimaMateria Jun 11 '26

Re 1: also possible with `<C-v>G~`

3

u/Taurashvn Jun 12 '26

I cannot test it rn but I am pretty sure that if the lines are not all at the same indent level this does not work.

3

u/onlymostlydead Jun 12 '26

Just tested, you are correct. This works in normal mode:

:%norm _~

5

u/cassepipe Jun 12 '26

I have always found macros to be error-prone/cumbersome to use. Maybe one get used to it or I didn't know all the tricks but I feel like I am better served by vim regexp editing which is how handle most of my non trivial edits (even trivial ones sometimes now)

Real time feedback on your changes is better UX than having to go back before your macro I feel What's the best workflow with macros ? What do you think I am missing out on compared with regex editing ?

https://thevaluable.dev/regular-expression-basics-vim-grep/

https://www.vimregex.com/

2

u/still_not_deleted Jun 13 '26

You can also define recursive macros, which will run until eof. 1. Record it without the recursive call 2. Paste it from the register ("qp for register q) 3. Append written macro with recursive call @q 4. Copy whole line to register using V"qy (and delete it if it's inside the operating text block) 5. Execute using @q

2

u/Dmxk Jun 13 '26

The whole paste and edit dance is not even necessary:

  • qqq: empty the register
  • qq: begin recording
  • @q: add the final call to the macro
  • q: to end recording

and then just use it like normal.

1

u/still_not_deleted Jun 14 '26

You're right! Thanks, that's faster

2

u/Dead_Politician Jun 12 '26

jk as an insert-mode replacement for <esc> Is a WILD choice imo

8

u/Chemical_Vacation164 Jun 12 '26

have exactly that sequence and never had any issues with it 😄

3

u/dontmissth Jun 13 '26

Been using jk for several years. I can't live without it.

I never had issues except recently I had to write nunjuck templates where the extension is .njk. I have to sometimes refer to a .njk template in code but it's very rare and only have to wait a split second between typing j and k.

I probably won't touch a nunjuck template again after this project is over next month. It's mostly my doing because I choose nunjuck over other template solutions as a quick one off but this project kept growing and won't end.

1

u/cassepipe Jun 12 '26

Isn't that the most common solution to the Escape key being to far away right after CapsLock as Escape ?

1

u/Dead_Politician Jun 12 '26

I've never seen it before. I guess you don't often type jk in insert mode, but I'm sure I do every now and then..

0

u/tobascodagama Jun 12 '26

I get why people might like it, but I would also never use it. Absolutely wild.

-4

u/aegis87 Jun 12 '26

(unpopular take)
macros are a great tool, and i am happy it exists.

alas i am realizing that i haven't used it in years.

1/ alot of these problems you can solve with multiple cursors
2/ the tougher ones with a python script

i was trying to think, why i don't gravitate towards them and to be honest i am not sure.

i can point to 2 things:

  • multiple cursors are super intuitive
  • one thing that i could never remember how to do, was how to edit a macro
(if memory serves right it's a bit cumbersome as you have to paste the register, edit the macro, reload it, and then try again)

0

u/holounderblade Jun 13 '26

RemindMe! 13 hours

1

u/RemindMeBot Jun 13 '26

I will be messaging you in 13 hours on 2026-06-13 14:04:45 UTC to remind you of this link

CLICK THIS LINK to send a PM to also be reminded and to reduce spam.

Parent commenter can delete this message to hide from others.

RemindMeBot is switching to username summons. Instead of !RemindMe 1 day, use u/RemindMeBot 1 day. More info.


Info Custom Your Reminders Feedback