0

I use vim -o a* to open 25 files (filenames starting with 'a') together and search & replace (:windo %s/old/new/gcI) some common word in these files. Later I found that not all of them have been modified and it seems to be due to that vim -o opens only 23 of them. However, vim -O a* can open all 25 files.

So is there any safe way to make sure every file is opened (and thus modified), hopefully not by counting windows by eyes.

2 Answers 2

2

In a slightly different take, vim also preserves the files given it on the command-line in an argument list (colloquially: :args). When you use :next/:prev, you traverse that list. You can change, add to, and operate further on that list with many functions/commands (see the help).

Of particular note is :argdo which, alongside its cousins :bufdo, :windo, :tabdo, :cdo, and :ldo, traverses the list and runs a command.

So in your case, I would do

$ vim a*
:argdo %substitute/old/new/g

Alternately:

$ vim a*
:vimgrep /old/ ##
:cdo substitute//new/g

Where this version first searches for the word to change in the arglist (##) and then applies the change. The advantage here is the quickfix list gives you a preview of all the occurrences about to be changed. If you don’t like it, you can try a different search pattern or use the :Cfilter command from a built-in package.

3
  • This is exactly what I need! I'm just not aware that arg and buf are more complete than win. When does arg differ from buf? Thanks.
    – xiaohuamao
    Jun 20, 2020 at 20:12
  • 1
    The buffer list is all buffers in memory. Files in the argument list may or may not be in memory, but are a separate list. Its initially the command line arguments, but can be changed/refined interactively. It is common to maintain the argument list as a subset of the buffer list that you’re particularly interested in.
    – D. Ben Knoble
    Jun 20, 2020 at 20:31
  • 1
    However, after :argdo edit all the files in the arguments list should also be in the buffer list
    – D. Ben Knoble
    Jun 20, 2020 at 20:32
3

When opening multiple files, Vim will only create as many windows as it can display, and load the rest without displaying them, in what we call buffers.

The help states:

Summary:
   A buffer is the in-memory text of a file.
   A window is a viewport on a buffer.
   A tab page is a collection of windows.

You can display a list of currently open buffers with :ls. You could also use Fzf and the :Buffers command it provides.

You can access any buffer with :buffer [name or number], and navigate with :bnext and :bNext, or :bn and :bp (next / previous).

You may want to take a look at :h windows, especially the sections covering "buffers".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.