20
u/calvers70 Mar 13 '24
Realy no reason to macro something like this:
Select the lines with visual mode and then :'<,'>s/\v\w+\s+(\w+).*/\1/ - there's probably a more elegant regex too, that was just easy to write.
Also `:h :g``
36
Mar 13 '24
Cool regex. Definitely more effort to design that than to just stumble through a macro though.
13
u/justsomepaper :cope Mar 13 '24
Depends on your level of practice. Especially in Neovim with a real time preview of the regex match, I find regexes easier than macros most of the time.
5
u/odaiwai %s/vim/notepad++/g Mar 14 '24
That's not a complex regex, though. Regex is also (more or less) a transferable skill across many apps and editors, so it's well worth the time to learn the basics.
2
u/tommcdo cx Mar 14 '24
I would usually break it down into two steps:
:%norm daw- delete the first word on each line
:%s/\s.*- taking advantage of the optional closing pattern delimiter (/) and empty replacement for a quick way to delete from some pattern (in this case, whitespace) to the end of the line2
u/calvers70 Mar 13 '24
I agree you can spend more time on regex than you would have on a macro sometimes, but not in this case. Took me about 15 seconds to type that straight into the comments. All regexes look a bit gnostic, but there's not a lot going on with that particular one. It's pretty basic
2
u/dfwtjms Mar 13 '24
Nice one. The 'very magic' mode was new for me. It makes everything more readable.
2
2
u/watsreddit Mar 14 '24
I would definitely just use
cut::'<,'>!cut -f 2 -d ' '. Fastest and requires no thought.1
3
173
Mar 13 '24
Or you could do :%!awk '{print $2;}' ...
67
u/guildem Mar 13 '24
Exactly. Vim alone is powerful, but vim + all the other tools we have is awesome and unlimited.
38
Mar 13 '24
Yup, and when the data file gets big, like 100GB+ , vim isn't feasible at all as it OOMs. In those cases, processing the data as a stream is one of very few options available. So knowing "all the other tools" is IMHO more important than vim macros. YMMV...
45
7
u/guildem Mar 13 '24
Exactly, the more you know, the more you can do. Writing shell scripts is a good way to learn all those tools. Then depending on your usage, you'll have the most optimized tool for every task.
3
u/dustractor ^[ Mar 13 '24
This. I had an 800,000 line xml file without line breaks. Tried fixing it with a macro and vim was gonna take DAYS to do what sed could do in a few seconds.
10
u/tactiphile Mar 13 '24
I had an 800,000 line xml file without line breaks.
Sounds like you had a 1-line xml file
3
3
u/agramata Mar 14 '24 edited Aug 05 '26
Unbelievable raindrop cobblestone kettle cucumber wander meadow glove ginger
This post was anonymized with Redact.dev
22
u/waterkip Mar 13 '24
or
cut -f 2would work as well.(reddit needs to fully support the markdown editor in their beep UI).
3
u/watsreddit Mar 14 '24
It should be
cut -f 2 -d ' '.cutdefaults to tab delimiters, so you need to change it to a space.1
Mar 14 '24
You can use four leading spaces for code blocks (if that's what you were after). I get the feeling the dev team has other priorities.
2
u/waterkip Mar 14 '24
No, the problem is that I have markdown as the default editor. But they changed it to "fancy pants". Which is the opposite of friendliness towards my default setting. Im in tech oriented subs, I use code and code blocks all the time. There was a reason I want markdown as a default.
1
Mar 14 '24
cut can be iffy with tab/spaces. awk works every time.
1
u/waterkip Mar 14 '24
Never noticed. Isnt their field seperator a space so behaviour should be the same?
1
u/watsreddit Mar 14 '24
It's not iffy, you just need to specify your delimiter with
-dif it's something other than a tab, e.g,cut -f 2 -d ' '.1
Mar 14 '24
but if the file you're working with has unknown tab vs space, or mixed, awk is the winner.
8
u/funbike Mar 13 '24
That's how I would have done it.
It's rare for me to use a macro for mass processing of lines, maybe less than 5% of the time. Instead I'll usually use
:%s/,:%!sedor:%!awk.4
u/vainstar23 Mar 14 '24
Yea I've been using vim for a couple of years now. I have never found a use for vim macros that I couldn't do in awk or sed.
1
Mar 14 '24
Same. Also, not all hosts have vim, and when working on lots and lots of different variants of UNIX/POSIX at the same time, I've found it easier to just stick with the old, but always available tools instead of relying on vim-specific functionality.
15
u/bramley Mar 13 '24
'<,'>!cut -d " " -f 2
6
u/c2n382nv2vo_w Mar 14 '24
Til you can run unix commands on selections??
5
u/watsreddit Mar 14 '24 edited Mar 14 '24
Yep. You can also pipe the current buffer (or a range) to stdin of a command using
:w !some-cmd, and read stdout of a command into a buffer with:r !some-cmd. Vim is much more powerful when you're using the full power of the shell with it2
12
u/Nitrodist Mar 13 '24
What's the program you're using to screen record / zoom and provide subtitles, etc?
26
5
15
u/Zeioth Mar 13 '24
Macros are awesome for such scenarios. As powerful as regex, but intuitive enough to do it in 5 seconds.
10
4
3
2
2
1
u/Scholes_SC2 Mar 13 '24
This is something I wanted to do long ago. I was waiting for the macro to complete over 5k lines. I feel dumb now
3
u/IrishPrime g? Mar 13 '24
You can also use
:h lazyredrawto make that type of thing a bit quicker when changing a bunch of lines.1
u/vim-help-bot Mar 13 '24
Help pages for:
'lazyredraw'in options.txt`:(h|help) <query>` | about | mistake? | donate | Reply 'rescan' to check the comment again | Reply 'stop' to stop getting replies to your comments
1
u/priestoferis Mar 13 '24
What are you holding down to continously execute the macro?
3
u/dalbertom Mar 13 '24
Last macro run is stored in the
@registry, so you can press@@to run it, or keep it pressed to run it multiple times.2
u/priestoferis Mar 13 '24
Ah, lol, makes sense, I somehow thought that would not register that way, but obviously it does.
1
1
1
u/ohcibi The Plugin Using Vimmer Mar 14 '24
„Why vim is the best“
Video shows how to record and play a macro. Has been a feature in MS Word 97. So vim is at least as good as MS Word 97. wow. I’m impressed.
1
u/ohcibi The Plugin Using Vimmer Mar 14 '24
432 upvotes on this bullshit. I can’t express the disappointment about this crap.
1
1
1
u/evanthebouncy Mar 17 '24
Or just ask chatgpt to write a Python script... Would've taken 15 seconds top
0
Mar 13 '24
Yea, but how often do you that(me thinks not so much, at max 5/year) in order for that knowledge to actually become valuable and not some exotic one?
4
u/IrishPrime g? Mar 13 '24
The point of this type of thing is that there isn't much to memorize with specific commands or tricks, it's just an approach.
qregister.Make the changes manually to one line.
qto stop recording.
@qto run the macro or:norm @qto run it over a range.I almost never have to perform this specific type of task, but I frequently record short macros and run them over arbitrary portions of a buffer.
2
u/delatorrejuanchi Mar 13 '24
When I need to refactor code, I’ll use similar macros multiple times per day. They’ll usually be a bit more complex than what is shown in the video but they’re really easy to come up with and save me a lot of time doing each change separately.
0
u/dustractor ^[ Mar 13 '24
'go to the next line and go to the start of the line' -- that's what plus movemement is for.
34
u/ArmchairSpartan Mar 13 '24
:%norm dwWD