I was refactoring a bunch of imports. We use remix-utils
in our Remix codebase at work and we have been on v4 for quite a long time. This version exported some utility functions for http responses
import { badRequest, serverError, forbidden } from 'remix-utils'
Latest versions do not export these functions anymore, so I replicated them in a utility file.
import { json } from '@remix-run/node';
export function badRequest<Data = unknown>(data: Data, init?: Omit<ResponseInit, 'status'>) { return json(data, { ...init, status: 400 });}
// ... other HTTP helpers
I wish I could’ve just find-and-replace-all, but some of the imports were mixed with other non-http imports!
The flow was:
- Grep for
remix-utils
imports - Replace the import with
~/utils/http.server
- Save the buffer
- Close the buffer
- Repeat
I cannot automate the grep
’ing, but I can for sure automate the rest. Finally, the time to learn about Vim macros has come to save my sanity.
First, start recording the macro pressing q
. Then, press the key you want to use a register. I chose j
:
qj
Second, record the actions. I have a remap on <leader>bd
to delete the buffer:
ci'~/utils/http.server<esc>:w<leader>bd
This will:
- Change the text inside the
'
quotes to~/utils/http.server
- Save the buffer
- Close the buffer
And to finish, stop recording the macro pressing q
again.
q
Now I just had to play the macro with @j
and it would repeat the whole sequence 🤯. I saved so much time with this!