Skip to main content

3. Editing multiple files

  1. 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.txt

    Start vim with both test files as argument:

    vim foo.txt ls-output.txt

    To see the list of buffers (opened files):

    :buffers

    To switch to the next buffer press :bn. Try it a few times

    To 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
  2. It's also possible to add files to the current editing session.

    :q!
    vim foo.txt
    :e ls-output.txt
    :buffers
  3. 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 1

    1G and yy

    :buffer 2

    1G and p

    :q!

  4. It's also possible to insert an entire file into one that we are editing.

    vim ls-output.txt
    3G
    :r foo.txt
    :w foo1.txt

    We 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!
Loading asciinema cast...