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,
accomplish the same thing? i.e., are these two commands the same?
:ls give the same result, then yes, probably. (You may
I would imageine [sic] that in situations where the output of
:arg and the
:ls are not the same,
bufdo would give different
results to the above search and replaces.
Correct. Note that
:bufdo should skip
unlisted-buffers, per my reading of
- When vim opens with the above .vimrc, before entering vim, I obtain the
"./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
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
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
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
Great. How do you search? How do you replace?
There are a myriad of options:
/ (always only the current file)
:grep (can easily be restricted)
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 (
:substitute command is one way, though there are others
: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
:bufdo. But there are also
:tabdo, and more. Some
:cfdo, which iterate the quickfix list (see
above). (Location list variants exist.) Combine with
:help cfilter-plugin) for a very powerful workflow:
- Search your desired files with one of the
- Refine the quickfix list with
:Cfilter if necessary.
:cdo (with no range) or
:cfdo (with a
% range) to run your
This has the benefit of no longer needing the
e flag for
grepprgs, you can limit to different files very easily. For
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
ack/etc.) can usually filter by