Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Editing Multiple files in vi with Wildcards

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.

like image 480
Alan Storm Avatar asked May 14 '10 21:05

Alan Storm


People also ask

How do you edit multiple files in vi?

1 Invoking vi on Multiple Files one. When you first invoke vi, you can name more than one file to edit, and then use ex commands to travel between the files. invokes file1 first. After you have finished editing the first file, the ex command :w writes (saves) file1 and :n calls in the next file (file2).

How do I navigate multiple files in vim?

Using windows. Ctrl-W w to switch between open windows, and Ctrl-W h (or j or k or l ) to navigate through open windows. Ctrl-W c to close the current window, and Ctrl-W o to close all windows except the current one. Starting vim with a -o or -O flag opens each file in its own split.

How do I make different tabs in Vim?

To close a tab, use :tabc. To switch to the next tab, use :tabn, and to switch to the previous tab, use :tabp (short for tabnext and tabprevious respectively). You can also jump over tabs by using :tabn 2, which will move to the second next tab.

How do I switch files in vi?

4 Switching Files from vi. [ctrl-^] Since switching back to the previous file is something that tends to happen a lot, you don't have to move to the ex command line to do it. The vi command ^^ (the "control" key with the caret key) will do this for you.


2 Answers

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.

like image 54
Ignacio Vazquez-Abrams Avatar answered Oct 13 '22 01:10

Ignacio Vazquez-Abrams


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.

like image 33
Abhinav Avatar answered Oct 13 '22 01:10

Abhinav