Meta
This question is long (4 questions), and we normally prefer to keep questions to
1 question per question. The 4th is also opinion-based enough to be off-topic.
However, the questions are related enough and I have enough to say that I'm
going to (try to) write an answer.
Your questions first:
- Does this mean that at this point in time,
argo
and bufdo
would
accomplish the same thing? i.e., are these two commands the same?
:bufdo %s/search/replace/g
:argdo %s/search/replace/g
Well, if :args
and :ls
give the same result, then yes, probably. (You may
want the e
flag, :help :s_e
.)
I would imageine [sic] that in situations where the output of :arg
and the
output of :ls
are not the same, argdo
and bufdo
would give different
results to the above search and replaces.
Correct. Note that :bufdo
should skip unlisted-buffer
s, per my reading of
it.
- When vim opens with the above .vimrc, before entering vim, I obtain the
following :
"./src/file1.cpp" [dos] 587L, 20677C
Press Enter or type command to continue
Is there a way to suppress this message?
You could try :silent
, but see below.
- In trying to run
:argdo %s/search/replace/g
, is there a way to make it
interactive? That is, can I map this to say <C-s>r
via .vimrc and then I
get prompted for the word that should go into <search>
placeholder, and
the word that should go into <replace>
placeholder?
Of course: something like (in this version you have to press enter after the
prompts, so you have a chance to make corrections/etc.).
nnoremap <expr> <C-s>r ':argdo %s/'.escape(input('search '), '/').'/'.escape(input('replace '), '/').'/g'
- Is the above workflow even good practice? i.e., populating an argument
list via .vimrc for subsequent usage in searches and search-and-replaces?
Well, I think not. I'll talk all about search & replace (S&R) below, but setting
the arglist in your vimrc
- prevents
vim <file>…
from working the normal way;
- makes your vimrc specific to one and only one project; and
- limits your understanding of S&R.
If you want a special arglist, use sessions. Even vanilla :mksession
and vim -S
are quite good, though I recommend
obsession for the avid session user.
So, let's talk S&R.
I would like to restrict my searches and search-and-replaces to only the
/src/
and /include/
subfolders.
Great. How do you search? How do you replace?
On searching
There are a myriad of options:
/
(always only the current file)
:grep
(can easily be restricted)
:vimgrep
(idem.)
and probably more. The last two populate the quickfix list (there are location
list variants that start with l
). We'll come back to this.
The last two also take file names, which can be generated from the arglist if
you like (##
), or any other way of getting filenames (:help {file}
).
On replacing
The ubiquitous :substitute
command is one way, though there are others
(macros, complex :normal
- or :
-commands, :global
, basically anything you
would do interactively, &c.) that can be done from the ex command line.
As for targeting those replacements, you know the %
range and
:argdo
/:bufdo
. But there are also :windo
, :tabdo
, and more. Some
favorites include :cdo
and :cfdo
, which iterate the quickfix list (see
above). (Location list variants exist.) Combine with :Cfilter
(:help cfilter-plugin
) for a very powerful workflow:
- Search your desired files with one of the
:grep
variants.
- Refine the quickfix list with
:Cfilter
if necessary.
- Use
:cdo
(with no range) or :cfdo
(with a %
range) to run your
replacement command.
This has the benefit of no longer needing the e
flag for :substitute
.
With smart grepprg
s, you can limit to different files very easily. For
example, git grep
has lots of options and takes pathspecs as arguments (see
git help glossary
and search for pathspec
; you usually have to quote them
for the shell). Tools like ripgrep
(ag
/ack
/etc.) can usually filter by
file-type.