I have 5000 text files in a folder with different names. I just need to add one more line to the end in all files.
HELLO HELLO HELLO HELLO
I need make it
HELLO HELLO HELLO HELLO abc
I just need to add "abc" to the last line.
Vi and Vim Stack Exchange is a question and answer site for people using the vi and Vim families of text editors. It only takes a minute to sign up.Sign up to join this community
I would probably use the
argument-list for this.
First you have to add all files to the argument list and then you can use
argdo to operate on all of them.
So first let's populate the argument list. There's two ways to do this. The first is to go to your directory and open vim with
vim *. This will open vim with all of the files in the directory open on the argument list. If you can't do that, you can do this (admittedly more complex solution) instead. Open the first file and do this:
:for file in split(glob('*'), '\n') : exe "argadd ". file : endfor
split(): Splits a string into a list
glob(): Expands wildcards (This is what gets all the file names for us)
argadd: Adds the file to the argument list.
Now that the argument list is populated we can use
argdo to do an operation on ALL of them at once.
:argdo execute "normal! Goabc\<Esc>:w\<CR>"
execute: Allows us to use the keys
normal!: Does the following normal mode commands on the buffer
G: Go to the end of the file
o: Open a new line and go into insert mode
abcinto the text. Change this to whatever you need
\<Esc>: Hits the Escape key to get us out of insert mode.
:w\<CR>: Saves the file
:h split, and
:h glob for more info.