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?
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.
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.
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.
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.)
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With