VIMs Arglist
I think the arglist is one of the most underutilized features of VIM. In this post I'll try to describe what it is, and
how you can use it to incorporate PR feedback, and possibly reduce your usage of sed.
What it is
As per usual you can run:
:help arglist
to get a more detailed description. Here comes the tldr version for understanding the things we'll use it for in this
post: The arglist is a list of files that we want to perform different operations on.
To populate the arglist we can do something like this:
:args src/**/*.ts
Running the command above will populate the arglist with all of the typescript files within the src directory. Most of
the time, we are going to populate the arglist using different types of globs or shell commands. It might be useful to
know how to verify what files the arglist contains. Luckily, that's quite simple:
:arg
To clear the arglist you can do this:
:argdelete *
Next, I'm going to show you how to make use of this list.
Using arglist as a replacement for sed
In the past, when I had to refactor large code bases, I usually went with sed. However, if your codebase isn't gigantic,
you can probably get a better experience by making use of the arglist in VIM.
To get started you would have to populate it with something. Lets say that you want to rename some variables in all of
your typescript files. My imagination isn't that good so lets just go with renaming foo to bar. First, lets get all of
the typescript files into our arglist:
:args **/*.ts
We can now use the argdo command to perform actions on every file in our arglist. Lets do the substitution:
:argdo %s/foo/bar
Executing the command above will cause errors for the files where we don't have any occurrences of foo. We can suppress
those errors by using the e flag:
:argdo %s/foo/bar/e
We might also want to use the g flag to make sure we replace every occurrence:
:argdo %s/foo/bar/ge
If you don't want to replace EVERY occurrence of foo you can use the c flag to make the cursor stop and await your
confirmation:
:argdo %s/foo/bar/gce
Next, I'll show you two more really good use cases for the arglist.
Making the arglist more specific and getting project-wide search and replace.
Globs are great, but it is difficult to create more fine-grained lists of results. Luckily, we can populate the arglist
with shell commands by placing them within backticks:
:args `rg 'some search query' -l`
In the example above I used ripgrep with the -l flag to make it output file paths. This can enable project-wide search
and replace functionality.
Using the arglist to apply PR feedback
Sometimes, when you create pull requests, you get some good feedback on function/variable names. Maybe you used a name
that means something completely different in other parts of the codebase. When that happens we we want to perform
substitutions on the files we've changed in our PR. The git diff command has a really nice flag for that:
:args `git diff --name-only HEAD HEAD~1`
The command above will populate our arglist with the names of all the files that has changed between the two commits. If
your branch has multiple commits you would obviously have to change the last argument to reflect that.
We can now apply the PR suggestions to the files we've worked on:
:argdo %s/old_variable_name/new_variable_name/ge
The end
I usually tweet something when I've finished writing a new post. You can find me on Twitter
by clickingÂ