How exactly do you open a new file without closing and reopening vim? Read on to learn more about how to open files (or buffers) in vim using various included tools or external plugins. Vim uses the word “buffer” instead of “file”. When you :wq you write the current buffer to a file.

Vanilla Vim

Vim itself includes many helpful tools for opening files (what kind of text editor would it be without this). If you do not want to add multiple plugins or you are in an environment that does not allow plugins (production servers or containers) it can be nice to know how to use the vanilla tools.

Opening files from the Command line

The simplest and likely way you have opened a file before is with a command like this one.

$ vim path/to/file.md

Optionally you can open multiple files by listing them with a space between the file paths.

$ vim path/to/file1.md path/to/file2.md path/to/file3.md

Switching files after opening (Buffers)

Vim will open all three files in ‘buffers’ but you might not see them (see this article for help setting up a plugin that makes them visible). Run :ls or :buffers to get a list of all open buffers. This list might look something like this.

:ls
   1 h    "path/to/file1.md"    line 5
   2 %a   "path/to/file2.md"    line 19
   3      "path/to/file3.md"    line 0
   4 #h   "path/to/file4.md"    line 1

In this case there are four open buffers. The “%a” indicates the active buffer and “#h” indicates the previous active buffer (buffer 3 has not been opened yet and buffer 1 was opened before 4). The first number per row indicates the buffer id. We can use that number to jump directly to that buffer using the :b command. To jump to buffer “3” :b 3. Each row has the file path and the line the cursor is on.

To open the next buffer in the list run :bn or :bnext. You can skip buffers by specifying how many buffers to jump :2bn will skip a buffer. For example if I am currently in buffer id 1 and I run :2bn I will end up in buffer id 3 (vim will wrap around to the beginning if needed). To go backwards use the :bprevious or :bp commands. See also :bfirst and :blast.

(Useful help commands for this section :help bnext)

Open files with :edit command

To open a buffer from within vim use the :e or :edit command.

:e path/to/file4.md

If you want to open a file in read only mode open it with the :v or :view command. It can be helpful to know you will not edit critical files sometimes (log or system files).

:v path/to/file.md

It can get rather tedious typing out the entire path to a file sometimes. Vim has an auto completion engine called “wildmenu” (activate it with :set wildmenu or add set wildmenu to your .vimrc). With the wildmenu setting active while typing a file path press <Tab> and vim will autocomplete the path. Vim will also output a list of possible completions above the command row. Subsequent tabs will cycle through the values in the second row and <Enter> will run the command (in this case open the file).

(Useful help commands for this section :help buffers, :help wildmenu, and :help edit)

Netrw the built in file browser

Vim includes a very powerful file browser called netrw. Open netrw using the :Explore or :Ex command. Movement in netrw is the same as regular vim. To change the sort order of the files press s. Once you have the file selected press <Enter> to open the file in a buffer. Netrw can also be opened directly by listing a directory in the command line instead of a file.

$ vim .

Netrw is powerful and can do more than just open files but that is another article.

(Useful help commands for this section :help netrw)

Open Files with Plugins!

Vim can be extended with plugins. Many plugins have been written to improve file management in vim.

Using Netrw w/vinegar to open files

The first plugin is vim-vinegar by tpope. Vim-vinegar is meant to improve the defaults of netrw and add a few shortcuts. For example pressing the ~ key will jump netrw to the $HOME directory. The default sort order is changed from “name” to “suffixes”. Checkout the README to see the other changes. If you like vanilla vim but also don’t mind a few simple UX improvements tpope writes many other great plugins.

(Useful help commands for this section :help netrw and :help vinegar)

NERDTree the file system explorer

NERDTree is a file system explorer for vim that allows you to see your file structure in a tree like syntax. The plugin includes many more features and configuration options. NERDTree can also be configured to react to the mouse if you aren’t brave enough to use keyboard shortcuts all the time (see :help NERDTreeMouseMode). Checkout the README or help docs for more configuration options and settings.

Screenshot of vim with NERDTree open

(Useful help commands for this section :help NERDTree)

CtrlP and FZF the fuzzy file finders

The last two plugins are my favorite file openers CtrlP and FZF. Both of them are “fuzzy file finding” tools. Start typing the name or path to the file and CtrlP or FZF will filter the file list for you. CtrlP is built purely in vimscript and fzf uses the external binary fzf (written in Go). Both plugins provide great “fuzzy” file searching so pick the one that fits your needs. I have <leader>f :FZF<cr> in my .vimrc so pressing ,f opens the fzf search box. Once open typing the name of the file will filter the list down. Once you find the file you want press <Enter> and the file will open in a new buffer (CtrlP works the same but the command is :CtrlP<cr>).

Screenshot of vim with fzf dialog open

(Useful help commands for this section :help CtrlP or :help fzf)

Conclusion

Vim is powerful and also extendable so there are many great tools for opening and managing files (or buffers). No one tool is the best and nothing is stopping you from using multiple tools (I have both NERDTree and fzf installed on my system). Install a few and try them out for yourself, you might find you only need netrw or the shiny tree stuff in NERDTree is your game. Either way now you know how to open buffers in vim!