Vim Macros Make The Impossible Possible

Поделиться
HTML-код
  • Опубликовано: 29 дек 2024
  • НаукаНаука

Комментарии • 90

  • @nerdbot4446
    @nerdbot4446 5 лет назад +23

    Instead of running the macro an estimated amount of times, you can use
    :%s/PATTERN//gn
    to count the matches first

  • @AtomToast
    @AtomToast 5 лет назад +28

    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

    • @krassiem1206
      @krassiem1206 5 лет назад

      Exactly. Even 3dd would delete three lines, instead of 6 times pressing d

    • @diegocastillo6470
      @diegocastillo6470 4 года назад

      Was about to point out the same thing

    • @armaanbhojwani2677
      @armaanbhojwani2677 4 года назад

      Or, go into visual mode on the way up, then d

  • @techtutorials8812
    @techtutorials8812 4 года назад +1

    Ok, this is, hands down, my favorite video thumbnail from your channel. Completely awesome DistroTube!

  • @branislavgrujin
    @branislavgrujin 5 лет назад +6

    went for gnome meme. What a legend. It was super nice to hang out during the livestream. Thanks for video :)

    • @DistroTube
      @DistroTube  5 лет назад +6

      I'm hacking the hell out of GNOME. It will be unrecognizable when I'm finished.

    • @mitchelvalentino1569
      @mitchelvalentino1569 5 лет назад +3

      DistroTube YES!!!!!

  • @TomKatom
    @TomKatom 5 лет назад +7

    you actually did it!
    was fun in the stream.

    • @DistroTube
      @DistroTube  5 лет назад +4

      If you are talking about GNOME....yea, I did it. And I hate it! :D

  • @alejandrogiussi6369
    @alejandrogiussi6369 5 лет назад +10

    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).

    • @nmertsch8725
      @nmertsch8725 5 лет назад +3

      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.

    • @alejandrogiussi6369
      @alejandrogiussi6369 5 лет назад

      @@nmertsch8725 I agree.

  • @CS7984651
    @CS7984651 5 лет назад +10

    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
      @brainplot 4 года назад

      I don't understand why this would work. Won't calling the macro in the macro just cause an endless recursion?

    • @CS7984651
      @CS7984651 4 года назад

      @@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
      @blasttrash 4 года назад

      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

    • @CS7984651
      @CS7984651 4 года назад

      @@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.

    • @aidhoss
      @aidhoss 4 года назад

      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.

  • @carlesg0n
    @carlesg0n 5 лет назад +14

    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

  • @Dr-Brown
    @Dr-Brown 5 лет назад +2

    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.

  • @logangraham2956
    @logangraham2956 5 лет назад +11

    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

  • @AtomToast
    @AtomToast 5 лет назад +6

    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

    • @garkeinen7034
      @garkeinen7034 3 года назад

      ddP and ddp do the same. Only have this for visual mode

  • @estebanmarin002
    @estebanmarin002 5 лет назад +1

    Man! Love your channel! cheers for this information!

  • @OsmoZchannel
    @OsmoZchannel 5 лет назад +1

    nice video for my birthday, thanks @DistroTube

  • @thomasjensen1590
    @thomasjensen1590 5 лет назад +1

    Vim is macroes, Vim is life

  • @AnzanHoshinRoshi
    @AnzanHoshinRoshi 5 лет назад +1

    Thank you, Derek.

  • @lemler3337
    @lemler3337 5 лет назад +17

    vim: to advanced for me
    Emacs: never interested
    nano: goog enough for me

    • @zer0python
      @zer0python 5 лет назад

      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
      @shuwan4games 5 лет назад

      @@zer0python no emacs does not have the same bindings

    • @zer0python
      @zer0python 5 лет назад

      @@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. :-)

  • @BorisBrodski
    @BorisBrodski 5 лет назад +2

    Don't record macro into a number registers. They get overwritten, when yanking multiple lines.

  • @NixwayRU
    @NixwayRU 5 лет назад +1

    Thanks for video!

  • @andrewpalm2103
    @andrewpalm2103 5 лет назад +2

    Thanks for another helpful video, Derek! What colorscheme are you using in vim? Looks nice.

    • @DistroTube
      @DistroTube  5 лет назад +2

      Think this was based on Dracula.

  • @EliteTester
    @EliteTester 5 лет назад +1

    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.

    • @DistroTube
      @DistroTube  5 лет назад

      Look for my st config. The color scheme I think was based on Dracula.

  • @VictorRodriguez-zp2do
    @VictorRodriguez-zp2do 5 лет назад +4

    instead of doing dd 6 times you could have done dG which would have deleted till the last line.

    • @rteja764
      @rteja764 5 лет назад

      I was thinking the same !

    • @gregoryshields4258
      @gregoryshields4258 3 года назад

      There are 2 comments above yours which already pointed this out.

  • @bobisoft2k5
    @bobisoft2k5 5 лет назад +1

    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.

  • @catholic_zoomer_bro
    @catholic_zoomer_bro 5 лет назад +2

    Yep, only using vim from now on

  • @TonyFlexPromo
    @TonyFlexPromo 5 лет назад +3

    4:50 there is a faster way to delete all lines to the top/bottom: dG

    • @gregoryshields4258
      @gregoryshields4258 3 года назад

      You should read through comments before offering something that was already mentioned a few comments above yours.
      Nonetheless, you're correct.

  • @horatiopugwash9183
    @horatiopugwash9183 5 лет назад +3

    Editor MACroS 😜

  • @techtutorials8812
    @techtutorials8812 4 года назад

    Wait, you're not using XFCE, are you????

  • @zer0python
    @zer0python 5 лет назад +1

    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!!!"

  • @dingokidneys
    @dingokidneys 5 лет назад +1

    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.

  • @omerresnikoff3565
    @omerresnikoff3565 4 года назад

    How would you do that to order all the countries servers alphabetically?

    • @blank001
      @blank001 3 года назад

      Kinda late but you can use
      :sort

  • @MagicalCritical
    @MagicalCritical 5 лет назад +1

    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.

  • @MilomirDesnica
    @MilomirDesnica 5 лет назад +1

    for a second there i thought the guy in the thumbnail was named "Vim Macros"

  • @jbardolf2805
    @jbardolf2805 5 лет назад +21

    vim sucks
    This post was brought to you by the nano gang

    • @SimGunther
      @SimGunther 5 лет назад +2

      I thought it was the notepad squad, but m'kay then...

    • @sohn7767
      @sohn7767 5 лет назад +1

      SimGunther no no, the *MICROSOFT WORD* and *LIBREOFFICE WRITER* squad. Filthy casuals.

    • @halbgefressen9768
      @halbgefressen9768 5 лет назад +1

      @Ryan M you mean whatever bloats yer boat?

    • @zer0python
      @zer0python 5 лет назад

      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.

  • @alexandrprincip6366
    @alexandrprincip6366 5 лет назад +1

    Gnome on Wayland?

  • @Kakihara73
    @Kakihara73 5 лет назад

    😅 Omg, very complicated for me
    However thanks for that, I will study more and more to learn well how use it
    👏👏👏 GG Derek

  • @zwiebec78
    @zwiebec78 5 лет назад

    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.

    • @gregoryshields4258
      @gregoryshields4258 3 года назад +1

      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.

  • @AleksandarPopovic
    @AleksandarPopovic 5 лет назад

    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.....

  • @MagicalCritical
    @MagicalCritical 5 лет назад +2

    Any way to store a recorded macro directly to .vimrc?

    • @DistroTube
      @DistroTube  5 лет назад +2

      It's possible. In fact, if you find yourself doing the same repetitive tasks, it makes a lot sense.

    • @gregoryshields4258
      @gregoryshields4258 3 года назад

      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.

  • @iakashx
    @iakashx 5 лет назад

    good video.

  • @abel6580
    @abel6580 5 лет назад +1

    Or you could use Emacs

  • @migtrewornan8085
    @migtrewornan8085 4 года назад

    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
      @gregoryshields4258 3 года назад

      That's not true. Macros are sequences of commands stored in a register. The commands can be anything.

    • @migtrewornan8085
      @migtrewornan8085 3 года назад

      @@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.

  • @_yuri
    @_yuri 5 лет назад +3

    hey dt

  • @Canadian789119
    @Canadian789119 5 лет назад +1

    I cant vim. All I can do is nano.

  • @dawid_dahl
    @dawid_dahl 4 года назад +1

    Can Vim Macros get me a girl friend?

  • @vincentzhang2785
    @vincentzhang2785 5 лет назад

    macro? Choose Emacs

  • @aspenshadow7920
    @aspenshadow7920 5 лет назад

    text editors suck

  • @SuperCape
    @SuperCape 5 лет назад +3

    Meh... Emacs is much better with this...
    Spacemacs > Emacs > Vim

    • @mkd1113
      @mkd1113 5 лет назад +9

      Imagine using an operating system for your text editing.

    • @gregoryshields4258
      @gregoryshields4258 3 года назад

      You wish.

  • @bahathir_
    @bahathir_ Год назад

    Hi,
    Using another type of editor, GNU sed; the stream editer.
    $ sed -n '/## United States/,/##/p' mirrors.txt > us_mirrors.txt
    Thank you.