This VIM trick BLEW MY MIND
Video Overview & Insights
LEARN: https://learn.typecraft.dev/tutorial/editing-a-vim-macro/
thanks nerd
Join the stream! https://twitch.tv/typecraft
Join the community: https://discord.gg/TfPqD3MWVq
ggVG (select whole file)
:norm A, (append , every line)
gv (reselect last selection)
gJ (join lines without separator)
X: https://twitter.com/typecraft_dev
I've been using vim for 10+ years now, and I gotta say, I still learn something new every once in a while. And recently, I realized something about macros, and registers, that BLEW MY MIND. Check out this video on why vim macros and registers are probably a LOT simpler than they seem.
Of course, sometimes macros are overkill, for example if you just wanted to do the same for every line in the file you could just do: ":%s/$/,/|%j"
That is for every line use regex to substitute the end of line ($) with a comma, then (pipe) for every line (j)oin. If you don't want a space added you can just do "%j!"
More User Perspectives
20J is enough vro 🥀
@RenderingUserCome on dude, you click bait "This VIM trick BLEW MY MIND" and it's just a hidden advertisement for your learning platform. There's nothing mind blowing about making a basic macro... At least some comments here were actually useful/new information.
@thenewpotato61i don't use the :put a, i just paste my register anywhere, with "ap then edit it, and yank it again
@malmiteriaI think with most of this stuff, you have to think "how would they have written this in the 70s?" and then you'll realize, yea it was all just ASCII text everywhere..
@1980Hiawathawhy all developers who wear mustage are telling me why vim is so peak?
@qdrion3293Thank you for this tip. So basically everything is just text stored in a register, even macros.
You can write out the exact sequence of characters you want for the macro, then yank it into a register using "ayy, which yanks the whole line into register a.
After that, you can run the macro with @a.
Pretty cool stuff. I’m wondering how I can visualize it faster and learn more about special keys, like how Escape takes you from insert mode back to normal mode.
^v6j$A,^[VGJ
@SeafoodFriedRiceSomeone already mentioned it, but for posterity: you know how you can put a number in front of any command key in vim? Like 5j will move down 5 lines? Or 7dd will delete 7 lines? Well even with registers, you can type 7@a and it will do that macro 7 times. Not only that, but I believe macros can call other macros too
@whatsnextforkevYou don't even need to do use :put a, you can just do "ap.
@dj_chateau@@ replays the last macro. So @a then @@ after that.
@jasonfreeman8022Holy shit I did not know that. This makes so much sense. Thanks.
@uoweme5grandPro tip:
If you noticed, Escape is shown as
^[
which is the same representation as key combo Ctrl+[
And guess what, instead of escape, in Vim you can always use Ctrl+[ and it'd behave the same :)
This is especially useful because pressing the escape key to exit modes is often annoying as the escape key is very far from the rest of the keys.
I know this is just an example / demonstration, but for any newbs watching...
8J would have done this in a second or two without registers. As in, number 8 (-ish), capital J, in Normal Mode. Two keystrokes.
Thanks, Nerds. 👋
imagine someone making this tutorial, and when showing the registers, something crazy shows up
@satoijidesuq for qreate maqro
@satoijidesuOne year later and not a single comment about going to the grocery store to buy meth.
@fraternitas5117Great video ! Thanks!
@smidamarwen8033the 'q' register is classic tho
@devmindscapetutorialsfor that use case i'm using vap then :norm A, <ESc>J
@dagoberttrump9290I'm sorry WHATS THAT on your SHOPPING LIST?!
@Fazal828What does this " yanked " thing mean?
@xiic_17_chiragagarwal30"So if I just take @a @a @a"
You can replay the last run macro with @@
Typecraft: What's the easiest way to do this?
Me: %s/\n/, /g
Typecraft: The easiest way is to create a macro
Me: Oh
Two quickie suggestions to make applying your macro even more efficient: (1) execute the macro once and then repeat with @@ rather than moving your fingers over the keys to hit @a over and over, or (2) better yet, execute the macro a number of times by using a count, such as 6@a instead. Cheers!
@Phileosophosbrodie what's up with the wifi name
@lung-notificationVim is hard to learn and hard to master lol. And absolutely worth it for some people 🫶
@anoni-mouse-on-nixI've been coding in Vim for years and only this week did I learn about this and the amazing norm command.
@BluesManPeichkiwis and meth. My favourite breakfast
@pedroquerido2268Nice video - didn't know about editing macros/registers before. I do have one small tip that you might find that improves on using macros and that's the :map command. Essentially, what I like to do is to do something like ':map = <macro>'. In the example given here, that would become ':map = A,^[J' (without the quotes, of course) to store the macro under the unused equals key. Now you only need to hit ONE key to perform the macro however many times you need to. I'm a touch typist and this is quite a bit faster because I'm not having to awkwardly reach for the shift key all the time.
@nosuchuseridYou can also provide a number to replay the macro # times. rather than press "@a" six times you could press "6@a".
@dangeroushoboGoing @a@a@a@a... Is a lot of wasted key presses. Just do <x>@a where <x> is the number of times you want to run your macro. 😌
@SirWrexesJust reflow paragraph to bring everything in one line then just prepend words with , - done
@mateuszabramek7015there is an awesome FREE book online “lazyvim for ambitious developers” just saying…. the best place to start
@spamcolectorjust use vscode
@girinchutia1382Very nice !
@sep69Tip: If you're on a regular keyboard, @<register> is a bit clunky to type when you want to execute your macro quickly.
To replay the last recorded macro, just go @@
If you know how many times you need to execute it (say 5), do 5@@
Holy shit, this is really cool to know. I can plan my macro while seeing it, then dd@0 (or even have a binding that deletes the row and executes it as a macro immediately.
@Tekay37Yup, as someone actively learning this is a great insight
@SoreBrainTwo years later, after watching and applying your tutorials , YOU ❤ changed many concepts for me to the better even in my life , which reflected my speed and thinking .. writing the code, of course, and focusing on solving the problem instead of focusing on the place of the cursor, as an example, actually I don't need to use the mouse anymore 😎🤘.. Previously, I was a normal programmer, and now I am like Neo in the matrix film 🎉 . I wish you all the best from my heart❤🥹🤜🤛
@MegaOsama99Pubert 😂
@debruceyDidn't know Ted Lasso was a vim expert :D
@JuliusUnscriptedmain takeaway: "i'm yanking all over the place"
@muskyoxesYou can also just register another macro that calls the A macro with J, and record that. But honestly, this whole complicated thing for « just » doing something so trivial is what makes me realize today that Vim is outdated. I use Kakoune and what you described here that you wanted to do is just a couple keypress in Kakoune without needing to think about registering commands or editing registers:
%<a-s>;c,
(those are actual keys I press, and all of that is visually activated, for a nicer feedback). So yeah, I started coding 22 years ago, quickly picked up on Vim, but today, when you look at what other editors / philosophy provide… Vim is seriously outdated.
You seriously need to be a nerd if you are getting impressed by vim keybinds
@greNxNja13"easy to learn, hard to master"
No. Its definitely "hard to learn, harder to master" for vim...
I’ve would have done your first example with a search and replace regex. One line and done.
@marsrocket0:20 famous last words vim is easy to learn
@jemolife15