3. Editing multiple files
-
It is often useful to edit more than one file at a time.
Quit from vim:
:q!Let's create another test file:
ls -l /usr/bin > ls-output.txtStart vim with both test files as argument:
vim foo.txt ls-output.txtTo see the list of buffers (opened files):
:buffersTo switch to the next buffer press
:bn. Try it a few timesTo switch to the previous buffer press
:bp. Try it a few times.We can also switch to another buffer like this:
:buffer 2:buffer 1 -
It's also possible to add files to the current editing session.
:q!vim foo.txt:e ls-output.txt:buffers -
While editing multiple files, it is possible to copy a portion of one file into another file that we are editing. This is easily done using the usual yank and paste commands.
:buffer 11Gandyy:buffer 21Gandp:q! -
It's also possible to insert an entire file into one that we are editing.
vim ls-output.txt3G:r foo.txt:w foo1.txtWe saved the buffer to the file
foo1.txt, but we are still editing the first file and any further modifications will be done to it.:q!