Lets say we are editing this totally made up JSON file:
[{
"id_4f7xg4egb": "<some_random_guid",
// ... other fields
}, {
"id_h34k3": "<another_id>",
// ... different fields than prev object potentially
"nested": {
"id_j3h": "<nested_obj_id>",
// ... nested obj fields
}
},
// ...
]
It contains N objects (including the nested ones), and we'd like to replace the id
value with the string coming appended to the field key itself. The result would be something like:
[{
"id": "4f7xg4egb",
// ... other fields
}, {
"id": "h34k3",
// ... different fields than prev object potentially
"nested": {
"id": "j3h",
// ... nested obj fields
}
},
// ...
]
Now, here is what I would do in VSCode using multiple cursors:
With VSCode Ctrl+d
- Select first occurrence of
"id_
with Shift+→x4 - Select (and create a cursor on) every occurrence of the remaining N-1
"id_
with Ctrl+dx(N-1) - Select the id string(s) following
"id_
with →,← (to deselect), Ctrl+Shift+→ - Cut them with Ctrl+x (yes, each cursor gets its own "clipboard")
- Delete the
_
with Backspace - Select the portion we want to replace (
"<every_diff_id>"
) with →x4,Shift+End, ←x2 - Replace the value with Ctrl+v and Esc to get rid of the extra cursors
So we are talking about 5+N+5+2+1+8+3=N+24=o(N) keystrokes (taking into account the fact that, for example, the Ctrl key is counted only once and then held down for the rest of the N-1 Ctrl+d commands).
With the Power of Vim
And... my question is: How to accomplish the same result using Vim (in =< # of operations, ofc)?! I'm a noob, one week old vimmer, and I'm loving it so far! I've been using .
and basic macros for similar tasks, but I'm not sure what would be the most efficient way to tackle this one :(. I'd also prefer a solution NOT using plugins or involving adding some complicated mappings/functions to my .vimrc. After all, the VSCode solution is vanilla VSCode ;).
Thanks!