Hi. Big fan of Vim here. But... Isn't this (the first one) the kind of task sed was created for? I think $ sed -ni '/United/,+1p' does the job (please correct me if i am wrong). But anyway, great video (as usual).
When you don't know how many times you have to call the macro, you can just call the macro in the macro. This will make a loop that will go through your document.
@@brainplot Exactly. Of course, it is not useful if you don't want the macro to edit the whole file but i have used recursion multiple times when i bulkrename files in ranger.
@@blasttrash It would stop at the end of the file. All you have to do in your macro is to end it on the line below, replace the cursor at the right position and call the macro you're making and end it.
Yep, recursive macros halt at the end of the file. You can spend commands to a macro with Q, so in this case, the sequence Q@1q would add the macro call to the macro itself, then run it by @1 as per usual, and sit back and watch. That said, for this particular use case, a global might work better and much more efficiently. Something like ":g/^Date:/move -2", which would run the command "move -2" (move the current line to be below the address given by two lines up, or, move one line up) on all and only lines that match the pattern "^Date:", but I understand the need to find examples of when to use macros.
Hey distrotube, great video! Just to point it out, I think the first macro didn't work as expected: In case you have two consecutive United States entries in your mirror list, the second one will be removed (you can in fact see it with the example you showed on video) Regards
The first macro accidentally deletes some entries for United States. Instead, try using visual mode with grep to filter out "United States". Something like: vip:!grep -A 1 "United States" | grep "http" Second grep only keeps URLs, so filters out comments and discontiguous marker -- from first grep.
Oh, also did you know that there is a :move command to move lines around? It's such a common task that you should absolutely use it. I have it set to move the current line up and down to alt+j/k
lol, don't nano and emacs use almost the same keybindings though? anyway, vim at a basic level really isn't that advanced, you just need to know how to go into insert mode (press i, escape to leave it) and save/quit. (:w saves, :wq save&quit, ZZ save&quit, :q! just quit without saving.) You can make ridiculously complex edits quick in vim, when used properly. And I can't tell you how many times I've used macros to save me from doing similar repetitive tasks. (copy & paste just can't compete, you can't add any "logic" to a copy and paste.) And I would like to add the visual select (^v) block feature of vim is super handy. Use of registers to store repetitive comment blocks or what have you, is useful too. (" selects as register, you can then yank (yy) or put (p or P) using selected register, there are some "reserved" registers though. see :help registers in vim.) Proper use of f, F, t and T can also help speed up editing. of course, none of this matters if you don't use an editor that much. If you LIVE in an editor like I do though, you'll want to upgrade from nano/pico sooner rather than later and save yourself some time.
@@shuwan4games I meant interface-wise, it's driven by Ctrl/Meta keybindings. But yeah, probably not the exact same. I haven't used Emacs in years, haha. :-)
Irrelevant question but which colorscheme are you using for vim? The dotfiles seem to be missing a call to colo [yourcolorscheme]. Im asking because it seems identical to the one I use.
I like how the question is “How can I sort this file” and the answer isn’t simply :sort I know the rest of the question is “...using something like a vim macro” but c’mon.
You should've also mentioned that if you mess up in a macro, you may need to start over. :) (vim doesn't like u's in its macros it seems, or at least in my experience it doesn't.) It's a super PITA if you're just about done with a complex macro and you mess up. It's like "craaap!!!"
I love VIM and use it everywhere but I'm much less a fan of macros than of using awk or sed for those kinds of edits. Macros have their place no doubt but the way my brain works, I'd usually rather write a short awk script, even on the command line, than run a macro. Also you can use '.' to repeat the 'dd' which is often easier and quicker than either counting the instances or just typing 'dd' over and over again. It repeats the last delete, yank or paste command.
I managed to break a nano user away into using vim. Now they don't want to go back. :) vim is just a different style of editing. Remember it evolved from a line editor (vi) ... But once your used to the notion and flow of visual line editing, you find every other editor trash, or at the very least cumbersome to use.
dG will delete to the end of the file /^Date will only find lines with Date at the very start of the line and you can run macros over a visual selection nice vid, thanks for all you do.
Yours is the fourth comment to mention dG. Interesting how some people think that posting their own comment is more important than reading the comments of others.
You can save macros to a Vim script file, and then load them up from the file whenever you like. I don't think I would use my vimrc for that, as it's already huge.
I try to avoid macros as a general policy, you can pretty much do anything a macro can using :global :normal and :substitute. Macros are a bit of a failure to think about a better way of doing things.
@@gregoryshields4258 I'm not denying macros can include any commands but, for example, the second example can be done with :g/Date:/m-2 . . . maybe it's just me but that seems so much simpler and clearer without mucking about with macros.
Instead of running the macro an estimated amount of times, you can use
:%s/PATTERN//gn
to count the matches first
You can also just press dG to delete until the end of file instead of 6dd. Not much less keypresses but might just be a bit more efficient
Exactly. Even 3dd would delete three lines, instead of 6 times pressing d
Was about to point out the same thing
Or, go into visual mode on the way up, then d
Ok, this is, hands down, my favorite video thumbnail from your channel. Completely awesome DistroTube!
went for gnome meme. What a legend. It was super nice to hang out during the livestream. Thanks for video :)
I'm hacking the hell out of GNOME. It will be unrecognizable when I'm finished.
DistroTube YES!!!!!
you actually did it!
was fun in the stream.
If you are talking about GNOME....yea, I did it. And I hate it! :D
Hi. Big fan of Vim here. But... Isn't this (the first one) the kind of task sed was created for?
I think $ sed -ni '/United/,+1p' does the job (please correct me if i am wrong).
But anyway, great video (as usual).
Yes, I also would never use a vim macro for such a task, but as an introduction to vim macros, I think an easy example like this one is great.
@@nmertsch8725 I agree.
When you don't know how many times you have to call the macro, you can just call the macro in the macro. This will make a loop that will go through your document.
I don't understand why this would work. Won't calling the macro in the macro just cause an endless recursion?
@@brainplot Exactly. Of course, it is not useful if you don't want the macro to edit the whole file but i have used recursion multiple times when i bulkrename files in ranger.
if it recursively calls the macro, when it will stop? new to vim, so not sure I understand how you did macro in macro. Thanks
@@blasttrash It would stop at the end of the file. All you have to do in your macro is to end it on the line below, replace the cursor at the right position and call the macro you're making and end it.
Yep, recursive macros halt at the end of the file. You can spend commands to a macro with Q, so in this case, the sequence Q@1q would add the macro call to the macro itself, then run it by @1 as per usual, and sit back and watch.
That said, for this particular use case, a global might work better and much more efficiently. Something like ":g/^Date:/move -2", which would run the command "move -2" (move the current line to be below the address given by two lines up, or, move one line up) on all and only lines that match the pattern "^Date:", but I understand the need to find examples of when to use macros.
Hey distrotube, great video!
Just to point it out, I think the first macro didn't work as expected: In case you have two consecutive United States entries in your mirror list, the second one will be removed (you can in fact see it with the example you showed on video)
Regards
The first macro accidentally deletes some entries for United States. Instead, try using visual mode with grep to filter out "United States".
Something like:
vip:!grep -A 1 "United States" | grep "http"
Second grep only keeps URLs, so filters out comments and discontiguous marker -- from first grep.
or you know , don't even need to enter the file using reflector ...
reflector -c US -l 10 --sort rate --save /etc/pacman.d/mirrorlist
Oh, also did you know that there is a :move command to move lines around? It's such a common task that you should absolutely use it. I have it set to move the current line up and down to alt+j/k
ddP and ddp do the same. Only have this for visual mode
Man! Love your channel! cheers for this information!
nice video for my birthday, thanks @DistroTube
Vim is macroes, Vim is life
Thank you, Derek.
vim: to advanced for me
Emacs: never interested
nano: goog enough for me
lol, don't nano and emacs use almost the same keybindings though? anyway, vim at a basic level really isn't that advanced, you just need to know how to go into insert mode (press i, escape to leave it) and save/quit. (:w saves, :wq save&quit, ZZ save&quit, :q! just quit without saving.) You can make ridiculously complex edits quick in vim, when used properly. And I can't tell you how many times I've used macros to save me from doing similar repetitive tasks. (copy & paste just can't compete, you can't add any "logic" to a copy and paste.) And I would like to add the visual select (^v) block feature of vim is super handy. Use of registers to store repetitive comment blocks or what have you, is useful too. (" selects as register, you can then yank (yy) or put (p or P) using selected register, there are some "reserved" registers though. see :help registers in vim.) Proper use of f, F, t and T can also help speed up editing.
of course, none of this matters if you don't use an editor that much. If you LIVE in an editor like I do though, you'll want to upgrade from nano/pico sooner rather than later and save yourself some time.
@@zer0python no emacs does not have the same bindings
@@shuwan4games I meant interface-wise, it's driven by Ctrl/Meta keybindings. But yeah, probably not the exact same.
I haven't used Emacs in years, haha. :-)
Don't record macro into a number registers. They get overwritten, when yanking multiple lines.
Thanks for video!
Thanks for another helpful video, Derek! What colorscheme are you using in vim? Looks nice.
Think this was based on Dracula.
Irrelevant question but which colorscheme are you using for vim? The dotfiles seem to be missing a call to colo [yourcolorscheme]. Im asking because it seems identical to the one I use.
Look for my st config. The color scheme I think was based on Dracula.
instead of doing dd 6 times you could have done dG which would have deleted till the last line.
I was thinking the same !
There are 2 comments above yours which already pointed this out.
I like how the question is “How can I sort this file” and the answer isn’t simply :sort
I know the rest of the question is “...using something like a vim macro” but c’mon.
Yep, only using vim from now on
4:50 there is a faster way to delete all lines to the top/bottom: dG
You should read through comments before offering something that was already mentioned a few comments above yours.
Nonetheless, you're correct.
Editor MACroS 😜
Wait, you're not using XFCE, are you????
You should've also mentioned that if you mess up in a macro, you may need to start over. :) (vim doesn't like u's in its macros it seems, or at least in my experience it doesn't.) It's a super PITA if you're just about done with a complex macro and you mess up. It's like "craaap!!!"
I love VIM and use it everywhere but I'm much less a fan of macros than of using awk or sed for those kinds of edits.
Macros have their place no doubt but the way my brain works, I'd usually rather write a short awk script, even on the command line, than run a macro.
Also you can use '.' to repeat the 'dd' which is often easier and quicker than either counting the instances or just typing 'dd' over and over again. It repeats the last delete, yank or paste command.
How would you do that to order all the countries servers alphabetically?
Kinda late but you can use
:sort
Save and quit: ZZ its so eaZZy. grep or fgrep > file? There is flag to print the line after the matching line. i think. im not sure.
for a second there i thought the guy in the thumbnail was named "Vim Macros"
That's my porn name. ;)
vim sucks
This post was brought to you by the nano gang
I thought it was the notepad squad, but m'kay then...
SimGunther no no, the *MICROSOFT WORD* and *LIBREOFFICE WRITER* squad. Filthy casuals.
@Ryan M you mean whatever bloats yer boat?
I managed to break a nano user away into using vim. Now they don't want to go back. :)
vim is just a different style of editing. Remember it evolved from a line editor (vi) ... But once your used to the notion and flow of visual line editing, you find every other editor trash, or at the very least cumbersome to use.
Gnome on Wayland?
Sadly, yes.
😅 Omg, very complicated for me
However thanks for that, I will study more and more to learn well how use it
👏👏👏 GG Derek
dG will delete to the end of the file
/^Date will only find lines with Date at the very start of the line
and you can run macros over a visual selection
nice vid, thanks for all you do.
Yours is the fourth comment to mention dG.
Interesting how some people think that posting their own comment is more important than reading the comments of others.
Hey dt the next big thing in the Linux world is fedora silverblue, that change the linux as we know it in future, loved to hear your opinion.....
Any way to store a recorded macro directly to .vimrc?
It's possible. In fact, if you find yourself doing the same repetitive tasks, it makes a lot sense.
You can save macros to a Vim script file, and then load them up from the file whenever you like. I don't think I would use my vimrc for that, as it's already huge.
good video.
Or you could use Emacs
I try to avoid macros as a general policy, you can pretty much do anything a macro can using :global :normal and :substitute. Macros are a bit of a failure to think about a better way of doing things.
That's not true. Macros are sequences of commands stored in a register. The commands can be anything.
@@gregoryshields4258 I'm not denying macros can include any commands but, for example, the second example can be done with :g/Date:/m-2 . . . maybe it's just me but that seems so much simpler and clearer without mucking about with macros.
hey dt
Hi, pink.
I cant vim. All I can do is nano.
Can Vim Macros get me a girl friend?
Of course, you crafty devil!
macro? Choose Emacs
text editors suck
Meh... Emacs is much better with this...
Spacemacs > Emacs > Vim
Imagine using an operating system for your text editing.
You wish.
Hi,
Using another type of editor, GNU sed; the stream editer.
$ sed -n '/## United States/,/##/p' mirrors.txt > us_mirrors.txt
Thank you.