Vim logo
I’m using IntelliJ (blasphemy) with IdeaVim (understandable) plugin. I like Vim motions, but I’m still too addicted to some IntelliJ features, so I find it hard to use bare Vim/NeoVim. And I have a life, so I don’t like to dive into configuring Vim.
Yesterday, I needed to get a lot of product data and use a REST endpoint to create a bunch of products in my eCommerce project. I had a CSV file with a lot of information and I only needed 3 columns from that CSV. I didn’t like the idea of reading parsing and transforming CSV to JSON (which is the format I needed). It would be much easier but why spend 5 minutes when you can waste 5 hours on something else? Am I right?
So I decided to manually prepare the JSON payload. I deleted useless columns from the CSV file, copy-pasta’d the data to an IntelliJ scratch file. Good. Now I have ~1500 lines of product data. Each line is something like this:
24601 Shoes My Special Brand Shoe
I need to transform this to:
{
"id": "24601",
"type": "Shoes",
"name": "My Special Brand Shoe"
}
I knew IntelliJ had a feature called cursor duplication. I would basically have more than one cursor and I could edit all of them at the same time.
Ok, how to add multiple cursors? I don’t know. I used Shift + Shift
to search for the action. It doesn’t have a keybinding. So I went to settings and added a keybinding for it.
Now I can add multiple cursors. I pressed the super-special-characters on my keyboard for dozens of times. First gotcha: You can’t have more than 1000 cursors. I had ~1500 lines. Ok not a problem, I can do it in 2 batches.
To test if it actually works, I pressed I
to go to the beginning of the line and switch to insert mode. IntelliJ is unresponsive. Waited for a couple of minutes. Still unresponsive.
I killed the process, opened up the IntelliJ again. Ok, I said, maybe 1000 cursors are very hard for IntelliJ to handle. Let’s try 300. Why not? Even if it feels sluggish, I can bare to wait a couple of seconds. (At the time, I thought I could bear it.)
Same thing, an unresponsive IntelliJ. Killed it, opened it, I lost the scratch file somehow (magnificent). I opened the CSV file again, copy-pasted the data to a new scratch file.
I’m looking at the same file, only 15 minutes wasted. Nice. What I was doing?
Then Vim gods communicated with me: “Btw, I use Vim. I can do this easily in Vim. You know that right?” I remembered ThePrimeagen mentioned something called macros.
What are they? Why are they useful? No idea. Opened the browser, vim macros !g
enter. Opened the first link, learned that they are useful for doing something repetitive. Nice, that’s what I need.
How do I use them? Read more. After 5 minutes I think I have an idea how they work.
Going back to IntelliJ, hoping IdeaVim supports it. Pressing qa
to start recording a macro to register a
.
First try, I failed. No problem, I can do it again. I recorded a macro to:
- Go the beginning of the line
- Go into insert mode
- Write
lorem
(I was testing) - Go back to normal mode
- Go one line below.
- Stop recording the macro.
Ok, macro is recorded into a
. Now I try @a
. IT WORKS! Trying 5@a
. IT WORKS AGAIN!
Undo everything, now I’m ready to do the actual modifications I want to. I don’t know if this macro is correct or not because I’m writing it after a day, from memory (it’s probably not correct). But here it’s an example:
qaI{ id: "<Esc>ea",<Esc>wItype: "<Esc>ea",<Esc>wIname: "<Esc>A" },<Esc>jq
At least, good enough to give you an idea. The gist is:
- I only edited one line, using only the generic Vim motions as possible. (No hjkl) (Except the last motion)
- My every motion is recorded into a register.
- After recording, I can say Vim do this motions & changes to this line or do this motions & changes to 5 lines.
After recording the macro, the only thing I needed to do was 1500@a
. Voilà, now every line is a JSON object.
I manually added the [
and ]
to the beginning and end of the file.
Did I really need JSON? I don’t know. Could I use CSV parsing? Absolutely. Could I find a better way to do all of this? Probably. But thanks to Vim macros, now I’m blazingly fast than ever.