0

I'm trying to open and edit a series of files with vim. My expected behaviour, is that vim opens one file, I edit it, type :wq, vim closes and saves that file, and then immediately opens the next file for me to start editing.

I did some googling, and several results profered the solution of vim * or vim pattern*, with variations on standard unix shell file globbing syntax.

However, when I try this on my system, I get unexpected behaviour. After I edit and :wq, vim seems to open that same, first file again (with the saved changes), and then another :wq dumps me back on the command line. No other files were opened for editing

Edit I tried two methods to escape the asterisk, in case something wasn't reading the glob properly

$ vim webform.webform.\*

$ vim "webform.webform.*"

However both of these syntaxes gave me a vim screen where it was editing a new file with the literal name webform.webform.*

Here's what I originally attempted, with the unexpected behavior:

$ vim webform.webform.*

Vim opens the first file:

...   ...
100   draft_pending_multiple_message: ''
101   confirmation_type: page
102   confirmation_title: ''
103   confirmation_message: ''
"webform.webform.my_webform.yml" 184L, 8621C

I edit the file, save, and close it:

:wq

Vim notes that the changes were written, indicates how many files are left to edit, wants me to hit enter for some reason:

"webform.webform.my_webform.yml" 184L, 8621C written
E173: 130 more files to edit
Press ENTER or type command to continue 

Then it opens again the same first file that it just opened.

100   draft_pending_multiple_message: ''
101   confirmation_type: page
102   confirmation_title: ''
103   confirmation_message: ''
                            1,1           Top

I save and close:

:wq

Now I'm back at the bash prompt, no additional files opened in vim.

$

I want to open a file, edit it, save it, close it, and edit the next file. What am I doing wrong?

$ bash --version
GNU bash, version 4.4.12(1)-release (x86_64-pc-linux-gnu)

$ vim --version
VIM - Vi IMproved 8.0 (2016 Sep 12, compiled Jun 21 2019 04:10:35)
...

$ cat /etc/os-release
PRETTY_NAME="Debian GNU/Linux 9 (stretch)"
...
4
  • 2
    You want to use ":w" to write the file rather than ":wq" (write and quit), and then ":n" or ":next" to get to the next file.
    – icarus
    Commented Jun 24, 2020 at 21:25
  • 1
    @icarus that appears to be just what I needed-- want to post that as an answer?
    – user394
    Commented Jun 24, 2020 at 21:31
  • Thanks, but let someone else post a full answer handling removing the buffers, the differences between vi and vim in this area, adds in :rewind and the like.
    – icarus
    Commented Jun 24, 2020 at 22:20

2 Answers 2

3

The problem is not the wildcards. It is that each file is opened in a buffer, and you cannot use :wq to exit a buffer. You could use :w|bd to save the current buffer and unload it. I find it clumsy, though. Much better is to add the -p flag when invoking Vim:

vim -p webform.webform.*

This causes each file to be opened in a new tab, and then you can use the :wq (or :x) to save the file and close the tab.

2
  • Suppose halfway through I decide I don't actually want to edit all 160 files right now. How do I exit out of all these "vims" at once?
    – user394
    Commented Jun 24, 2020 at 21:41
  • 1
    :xa or :wqa saves and close all tabs. :qa quits all tabs (if no modification was made to any of them).
    – Quasímodo
    Commented Jun 24, 2020 at 21:47
0

Based on icarus' comment, using :wn, write then next, instead of :wq gets me the desired behavior, and I don't have to enter two separate commands into vim.

1
  • 1
    Notice there is a slight difference between this and the expected behavior: With :wn the saved files keep open, you are just cycling through then with :wn (you could check that going back one file with :bp). I don't think that is a problem, but just to make it clear.
    – Quasímodo
    Commented Jun 24, 2020 at 21:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .