Hacker News new | ask | show | jobs
by ramses0 481 days ago
I'll share with you this video that I made ~15 years ago.

https://youtu.be/PHskKxkM-nA

...pause it before you watch it, look at the video description, and skip to 1m23s.

Grab the text from the video description in the editor of your choice, compare the before and after (heck, I'll probably go in and paste the "after/goal" so you don't even have to work at it).

Time yourself in your editor of choice to match the "goal" output.

Basically: `var $foo, $bar, $baz;` => `$obj = { "foo": $foo, "bar": $bar, "baz": $baz }`

...I randomly did it in about a minute, conveniently and subconsciously, and thought it was worth making a video about it (15 years ago...).

Nowadays I see easy fixups (eg: `gv`, maybe a macro...) that would have made it faster, but it's not about the speed, it's about the accuracy!

One of the most important "commandlets" of vim is the `.` command: "repeat last edit"

In the video when I perform(!) one "change comma into an arrow", I can `n` to "next" to the next anchor point (eg: a `,`) and repeat the edit(!)

Cleverly stringing together "edits" into interactive repetitions is like getting tetrises in Tetris (and about as easy, once you know to look for them).

vim is not an "editor", it's an interactive programming language for editing text:

`:g/def /norm 0fdf ywA #<c-r>"`

...find lines that match `def ...` (slightly sloppily), find the first 'd', find the following '<space>', yank the subsequent word (also could have yanked until a '('), and then append what was yanked as a comment.

Why?! Dunno. But like 30 keystrokes and I can edit the whole file to perform that operation, or any other arbitrary operation, and I can repeat that operation on any file I'd like.

It's "editing" in the sense that after a certain point you don't think about it, you just do it, and the text transformation isn't polluted by inaccuracies, as "advanced vim" encourages you to repeat, replace, define as opposed to "type".

Nowadays with LLM-autocomplete, and the temptation of "whole-git-repo" refactorings like: "change all my uses of list comprehensions to for loops, and use generators, but only for api responses that come from a particular domain" ... yeah, vim feels pretty baroque.

...but when you actually need to do some editing to fix up the mess that was made by the LLM, come talk to me. ;-)

1 comments

It's much faster to do this with multi-cursor editing in VS code or Sublime Text. And more intuitive and way easier to learn.

Select the $, Ctrl+D to select the other ones, then select and copy the identifiers. Took me 28 seconds in total.

Sincerely, thank you for trying it and sharing "better editing" for everybody! I actually gave it a try and downloaded sublime but got "stuck" after the following:

select $, cmd-d, "enter" (to get them all on newlines), then "$" to replace the lost "$", shift arrows/end to capture "everything", arrow/space over and "cmd-v" for paste, and then I got stuck. (can't "easily" line up the raggedly-pasted items with the multiple cursors and all the cursors seem to "float" weirdly after the paste)

...surely someone more experienced with sublime would know how to "get out of the mess", but I'm more experienced with vim.

I'm wholly not expecting to change your opinion on preferred editor, just sharing a little of the details the make the difference!

Well, to be honest that's where I cheated because I assumed that auto-format would fix it.

In Sublime Text there was an addon that did that. I assume something similar exists for VS Code.

To align the items manually I would Tab (or Space) as much as necessary so that each item has enough space for the alignment. I would then use the mouse to create multiple cursors in the same column. In VS Code it's drag MMB, in Sublime it's Ctrl+LMB or sth. Then Ctrl+Shift+Arrow_Right, Ctrl+Shift+Arrow_Left to select the spaces up until the (misaligned) beginnings of the words, then hit Delete.