37

When using the programmers text editor vi, I'll often use a wildcard search to be lazy about the file I want to edit

vi ThisIsAReallLongFi*.txt

When this matches a single file it works great. However, if it matches multiple files vi does something weird.

First, it opens the first file for editing

Second, when I :wq out of the file, I get a message the bottom of the terminal that looks like this

E173: 4 more files to edit
Hit ENTER or type command to continue

When I hit enter, it returns me to edit mode in the file I was just in. The behavior I'd expect here would be that vi would move on to the next file to edit.

So,

  1. What's the logic behind vi's behavior here

  2. Is there a way to move on and edit the next file that's been matched?

And yes, I know about tab completion, this question is based on curiosity and wanting to understand the shell better.

Alana Storm
  • 164,128
  • 91
  • 395
  • 599

2 Answers2

57

vi supports having multiple files available for editing. :n goes to the next file, :N goes to the previous. Use :h arglist for more information.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
10

ANSWER TO CURRENT QUESTION

You may write: vim -p myfile* and vim will open all the matches for myfile* in the current directory into multiple tabs. Then, you can edit all files one by one. Navigate using gt for going to the next tab and gT for going to the previous tab. For saving and quiting all the files in a single go, just write :wqa inside vim.

ANSWER TO A SIMILAR PROBLEM

I was facing a similar problem. I had a file named "myfile*" inside multiple subdirectories of my current directory. I wanted to edit a small change in all of them without getting to open them one by one. So, in the command line, I wrote this :

$find . -type f -name "myfile*" -exec vim -f {} \;

This way, find runs vim for each file. As soon as I quit an instance of vim, find automatically runs another with the next file until all of them have been done. Although, I agree that it would have been better if all the files could have been opened as tabs.

Abhinav
  • 1,882
  • 1
  • 18
  • 34
  • 3
    Additionally, as the max number of open tabs is **10**, it can be convenient to increase the limit by setting a custom `tabpagemax` in your `~/.vimrc`. See [link](https://www.linux.com/learn/tutorials/442422-vim-tips-using-tabs) – Campa Jun 18 '13 at 12:28
  • +1 for showing how to search within subdirectories and for showing `-exec` paramter for `find`. BTW what is trailing `\;` for? – Dimitry K Dec 06 '15 at 12:11
  • // , @Abhinav, thanks for the best response. This deserves more votes. – Nathan Basanese Sep 14 '16 at 03:25
  • @DimitryK 4 years late, but the `\;` is to mark the end of the command for `-exec`, since you can pass multiple args to the command, and then `find` options after. The \ is just to escape the semicolon, you could also do e.g. `find . -exec foo bar {} baz ';' -type f` – coolreader18 May 23 '20 at 01:27