With the args
list, we can do fancy actions because we have a command called
:argdo
which does exactly what the name suggests. It will execute an Ex
command on all files in the args
list.
But first let’s see how to add or remove files without restarting vim.
That’s a good point to note, it’s not a file list, it’s a name
list, that can
be a buffer too that does not have a file on the system. Usually buffers have a
name that matches with the name of the corresponding file, but we can rename a
buffer with :file {name}
(that’s cheating, if we call :w
, it will save the
file with the new name). Another option is to create a new buffer and set
buftype
to nofile
with :setlocal buftype=nofile
. In this case the buffer
will be a Scratch
buffer, but we can still change the name with :file
,
however we can’t save the content with :w
.
Add files
The easiest way to add a single file is to call :arga[dd] {name}
. This will
add {name}
to the argument list. If {name}
is omitted, the current buffer
name will be added to the args
list.
We can add more than one files with globs like
:arga content/posts/advent-of-vim/2021/day0*.md
.
Replace the list
There are cases when we don’t want to add new entries into the list, but we want
a new list, we can do it with :ar[gs] {new lsit}
. Similar to the previous
command :args content/posts/advent-of-vim/2021/day0*.md
, we have all the posts
where the filename matches day0*
, but in this case, we replaced the list, so
even if we had other files in the list, they are gone now.
Delete file
Sometimes we want to delete entries from the list, because it’s easier to get
the desired args
list with a glob add and delete. We can do that with
:argd[elete] {pattern}
. All files from the argument list will be removed where
the name matches the pattern.
Do some magic
Now we can shape our args
list as we want, now we can use :argdo
. Let’s say
we want to change the module path of our fancy go library, but it has a lot of
files and that would be a pain to rename everywhere, unless you know the magic
spell called argdo
. But I warn you, be careful, it’s dangerous one.
" First we load our files into the args list with a glob.
" It may take a bit of time, depending on how large the project, but we can
" split up and do per directory, to make it easier if we want.
:args **/*.go
" Now we have a very long argument list. Let's replace the old module path
" with the new path.
:argdo %s#git.efertone.me/efertone/old#git.efertone.me/efertone/fancy#g
" Now all buffers are updated, but they are not saved. We can use `:up[date]`,
" which does the same as `:w[rite]`, but writes only if the buffer has changes,
" that way we don't waste time saving a file that was not changed.
:argdo update
And that’s it. We moved a go project with 3 commands.