Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using VIM command on all open files

Tags:

bash

vim

vi

I have a list of files i've opened in VIM using this command:

vim `cat list.txt`

I'm looking to run this command on all open files:

:99,104 norm i#

(from lines 99 to 104, insert a comment "#") <--simple, works

When I run the above command to insert it works as expected on the current file. How do I get VIM to run the exact same command on all open files? I tried :argdo, :windo, :bufdo but didn't have any luck.

Any suggestions?

like image 660
Mike J Avatar asked Jan 02 '13 18:01

Mike J


People also ask

How do I navigate multiple files in vim?

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 you edit multiple files in vi?

Invoking vi on Multiple FilesAfter you have finished editing the first file, the ex command :w writes (saves) file1 and :n calls in the next file (file2). Open the two files practice and note. The first-named file, practice, appears on your screen. Perform any edits.

How do I select all files in vim?

To select all in Vim, use ggVG. Then, with the V key, enable the visual mode, and last, with the G key, choose from the beginning to the conclusion of the file.


3 Answers

I guess bufdo would work, if you have set hidden (to allow bufdo to change buffers without saving) before:

:set hidden
:bufdo 99,104 norm i#

Then save all files with :bufdo! w or :wa.

(Note: set hidden could be replaced with set autowrite as well.)

like image 63
miku Avatar answered Sep 21 '22 20:09

miku


I think sed would be a better option if you can use it. sed is a command line editor rather than an interactive editor like vim.

sed -i '99,104s/^/#/' list of files 
like image 38
Philip Aylesworth Avatar answered Sep 24 '22 20:09

Philip Aylesworth


Because you've supplied all files as arguments, :argdo is the correct command. (Unless you open other files, :bufdo would work, too.) If you don't want to :set hidden (as indicated in the other answer), you have to immediately persist the buffer via :update. Because you're using a :normal command, you have to enclose it in :execute to be able to concatenate another Ex command:

:argdo 99,104 execute 'norm i#' | update
like image 27
Ingo Karkat Avatar answered Sep 23 '22 20:09

Ingo Karkat