Hacker News new | ask | show | jobs
by joshstrange 1254 days ago
I use for things that I could do with find and replace (often requiring regex) but I find it faster/easier to just use multiple cursors. With multiple cursors I can convert a PHP array to JSON or JS very easily or take a list of params and turn it in to an object. I do this often when creating client-side TypeScript interfaces for the data the server is going to spit back at me. Example:

* I just select `public` on my first PHP class property

* Use multiple cursors to select all `public`'s that are before the properties I want (Normally by adding 1 selection at a time since I only want the props, not the public methods)

* Arrow over right 3 times (Now my cursor is right before the property names, after the `$`, this is PHP remember)

* Hold shift + control

* Arrow once more to the right (now the variable names are selected)

* Hit Cmd+c (copy all variable names)

* Open new IMyInterfaceDTO.ts file

* Type `export interface IMyInterfaceDTO { <cursor is here now> }`

* Paste in all my variables between the `{}`

* Select the new-line between each variable (I want a cursor before each variable name)

* Then add in `: string;` after each variable (string is the most common, I then manually change it to number/boolean/etc on the props that need it)

Done, now I have a TypeScript interface that matches my php class that gets turned into JSON.

Here is an example of what I'm talking about: https://cs.joshstrange.com/jlH4BnT3

Yes, I could accomplish the same thing with find and replace using a regex but this lets me see each step of the transformation and react easier. Maybe if I was a regex pro I'd feel differently but this method works really well for me and how my brain works. I know it's a lot of steps but I do it reflexively almost on autopilot verses having to stop and think about a regex.

2 comments

Awesome, it really helps me to visualize the usefulness of the feature. Thanks for taking the time to explain it.
Oh, I completely forgot about copying and pasting multiple cursors. I'm a hardcore user of that as well.