Vim Recipes ‣ Basics ‣ Opening Files
You want to open a file in Vim.
If you want to edit or view an existing file you need to open it first. For example, if your world-changing novel is saved as novel.txt, you want to open novel.txt in Vim.
To open a file from the command line invoke Vim with the filename as an argument. For example: vim docs/novel.txt (on Windows: vim.exe docs\novel.txt).
To open a file from inside Vim you can use :e file
(mnemonic: edit). This closes the current file and opens a new buffer
containing the given file.
If you use Andy Lester's ack utility you can create a shell script to open files without having to specify their path. For example, using bash:
#!/bin/sh vim $(ack -g $@)Name it vack, then vack shapes.rb will search recursively downwards from the current directory to find shapes.rb, then open it in Vim.
You may prefix the filename with +linenumber to instruct Vim to jump to the given line after opening. For example, vim +7 todo.list or :e +100 treatise.txt. If you omit linenumber, i.e. you prefix the filename with +, Vim will jump to the end of the file.
Similarly, prefixing the filename with +/pattern positions the cursor at the first occurrence of the pattern pattern. For example, vim +/^References btrees.textile instructs Vim to open btrees.textile, find the first line that starts with References, then position the cursor there.
The :cd directory command lets you change the directory Vim resolves relative paths to. So if you're working with multiple files in the same directory tree you can use this command to set your working directory so it's easier to open files. For example, instead of opening /home/julie/recipes/pasta/cabonara.txt then /home/julie/recipes/pasta/peperonata.txt you can :cd /home/julie/recipes/pasta then :e carbonara.txt. If you forget which directory you're in :pwd (print working directory) will tell you.
If you supply multiple filenames, Vim opens them all, one in each buffer. The first file named is opened in the current buffer. If you provide a line number or pattern to jump to, this only affects the first named file. Typing :next advances you to the next file in the list.
When working with multiple files you may prefer to view them in tabs or split windows, instead of buffers.
Tabs display a single file at a time, but, by default, provide a list of opened tabs across the top of the screen. You can switch to an open tab by clicking on its name in the GUI or referring to its number. From within Vim :tabedit file opens the named file in a new tab. Or, from the command line vim -p files opens each named file in its own tab.
Split windows display multiple files on screen simultenously. By default the screen is divided horizontally, putting each file beneath the previous, but you may also split it vertically so that each file is displayed next to each other. From Vim :split file splits the screen horizontally between the current file and the named file. :vsplit file effects a vertical division. These operations can be conducted from the command line with vim -o files and vim -O files, respectively.
So far we have specified filenames literally by naming each file to open. However, at other times this is impractical. For example, suppose you want to edit all files whose names end with .txt, or a file that you can only remember has the word lethargy in it. In cases such as these we would rather describe a group of files by using wildcards.
If you're opening a file from the command line (i.e. vim resume.tex), your shell expands any wildcards. Windows is notoriously weak at command-line work, but other operating systems will probably do the right thing here. For example, using the bash shell I can open .txt files whose names start with 1, 2, or 3 with vim [123]*.txt.
Vim commands that accept filenames support a similar set of wildcards with one caveat: some commands only accept a single filename, while others accept a list. The implication is that if you use wildcards with a command like :edit, which only takes a single filename, they cannot expand to multiple files. So if your current directory contained only one .html file, :edit *.html would save you typing and work how you expect. However, if the directory contained multiple .html files, that same wildcard would imply a list of files, and :edit *.html would complain: E77: Too many file names.
Instead of using :edit, you can use :next files which happily accepts a list of files, and opens each one in a new buffer. The :args files command is similar, but instead of appending the list of files to the current list of open files, it uses them to replace the current list.