1

VIMs Arglist

2

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

3

how you can use it to incorporate PR feedback, and possibly reduce your usage of sed.

4

5

What it is

6

As per usual you can run:

7

8
:help arglist
9

10

to get a more detailed description. Here comes the tldr version for understanding the things we'll use it for in this

11

post: The arglist is a list of files that we want to perform different operations on.

12

13

To populate the arglist we can do something like this:

14

15
:args src/**/*.ts
16

17

Running the command above will populate the arglist with all of the typescript files within the src directory. Most of

18

the time, we are going to populate the arglist using different types of globs or shell commands. It might be useful to

19

know how to verify what files the arglist contains. Luckily, that's quite simple:

20

21
:arg
22

23

To clear the arglist you can do this:

24

25
:argdelete *
26

27

Next, I'm going to show you how to make use of this list.

28

29

Using arglist as a replacement for sed

30

In the past, when I had to refactor large code bases, I usually went with sed. However, if your codebase isn't gigantic,

31

you can probably get a better experience by making use of the arglist in VIM.

32

33

To get started you would have to populate it with something. Lets say that you want to rename some variables in all of

34

your typescript files. My imagination isn't that good so lets just go with renaming foo to bar. First, lets get all of

35

the typescript files into our arglist:

36

37
:args **/*.ts
38

39

We can now use the argdo command to perform actions on every file in our arglist. Lets do the substitution:

40

41
:argdo %s/foo/bar
42

43

Executing the command above will cause errors for the files where we don't have any occurrences of foo. We can suppress

44

those errors by using the e flag:

45

46
:argdo %s/foo/bar/e
47

48

We might also want to use the g flag to make sure we replace every occurrence:

49

50
:argdo %s/foo/bar/ge
51

52

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

53

confirmation:

54

55
:argdo %s/foo/bar/gce
56

57

Next, I'll show you two more really good use cases for the arglist.

58

59

Making the arglist more specific and getting project-wide search and replace.

60

Globs are great, but it is difficult to create more fine-grained lists of results. Luckily, we can populate the arglist

61

with shell commands by placing them within backticks:

62

63
:args `rg 'some search query' -l`
64

65

In the example above I used ripgrep with the -l flag to make it output file paths. This can enable project-wide search

66

and replace functionality.

67

68

Using the arglist to apply PR feedback

69

Sometimes, when you create pull requests, you get some good feedback on function/variable names. Maybe you used a name

70

that means something completely different in other parts of the codebase. When that happens we we want to perform

71

substitutions on the files we've changed in our PR. The git diff command has a really nice flag for that:

72

73
:args `git diff --name-only HEAD HEAD~1`
74

75

The command above will populate our arglist with the names of all the files that has changed between the two commits. If

76

your branch has multiple commits you would obviously have to change the last argument to reflect that.

77

78

We can now apply the PR suggestions to the files we've worked on:

79

80
:argdo %s/old_variable_name/new_variable_name/ge
80

81

The end

82

I usually tweet something when I've finished writing a new post. You can find me on Twitter

83

by clicking 

normalintroduction.md
||104:3

Recently Edited

Recently Edited

File name

Tags

Time to read

Created at

context

  • go
  • context
8 minutes2024-02-28

circular-buffers

  • go
  • concurrency
  • data processing
5 minutes2024-02-04

go-directives

  • go
  • compiler
  • performance
4 minutes2023-10-21

async-tree-traversals

  • node
  • trees
  • graphs
  • typescript
19 minutes2023-09-10

All Files

All Files

  • go

    5 files

  • node

    2 files

  • typescript

    1 file

  • frontend

    1 file

  • workflow

    7 files