How to Open Files with Vim (2024)

Published:

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.

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.

How to Open Files with Vim (1)

(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>).

How to Open Files with Vim (2)

(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!

How to Open Files with Vim (2024)
Top Articles
How Much Are Points And Miles Worth In 2024? | Bankrate
What Is Leveraged Yield Farming?
123Movies Encanto
Palm Coast Permits Online
Libiyi Sawsharpener
Ffxiv Palm Chippings
Euro (EUR), aktuální kurzy měn
Boomerang Media Group: Quality Media Solutions
Coffman Memorial Union | U of M Bookstores
Es.cvs.com/Otchs/Devoted
Practical Magic 123Movies
What Auto Parts Stores Are Open
Stl Craiglist
Arrests reported by Yuba County Sheriff
Teamexpress Login
Fnv Turbo
Best Cav Commanders Rok
Hardly Antonyms
Bros Movie Wiki
Palace Pizza Joplin
Studentvue Columbia Heights
Lancasterfire Live Incidents
Invert Clipping Mask Illustrator
Labby Memorial Funeral Homes Leesville Obituaries
Zoe Mintz Adam Duritz
Www Craigslist Com Bakersfield
Hewn New Bedford
Babbychula
Watertown Ford Quick Lane
Culver's.comsummerofsmiles
Truvy Back Office Login
Narragansett Bay Cruising - A Complete Guide: Explore Newport, Providence & More
Farm Equipment Innovations
Paradise Point Animal Hospital With Veterinarians On-The-Go
Page 2383 – Christianity Today
Deepwoken: Best Attunement Tier List - Item Level Gaming
Robert A McDougal: XPP Tutorial
Kacey King Ranch
Fairwinds Shred Fest 2023
NIST Special Publication (SP) 800-37 Rev. 2 (Withdrawn), Risk Management Framework for Information Systems and Organizations: A System Life Cycle Approach for Security and Privacy
Sedano's Supermarkets Expands to Orlando - Sedano's Supermarkets
Lichen - 1.17.0 - Gemsbok! Antler Windchimes! Shoji Screens!
Snohomish Hairmasters
Thanksgiving Point Luminaria Promo Code
Daly City Building Division
Noaa Marine Weather Forecast By Zone
No Boundaries Pants For Men
Top 40 Minecraft mods to enhance your gaming experience
Rise Meadville Reviews
Epower Raley's
ats: MODIFIED PETERBILT 389 [1.31.X] v update auf 1.48 Trucks Mod für American Truck Simulator
Latest Posts
Article information

Author: Ouida Strosin DO

Last Updated:

Views: 5455

Rating: 4.6 / 5 (76 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Ouida Strosin DO

Birthday: 1995-04-27

Address: Suite 927 930 Kilback Radial, Candidaville, TN 87795

Phone: +8561498978366

Job: Legacy Manufacturing Specialist

Hobby: Singing, Mountain biking, Water sports, Water sports, Taxidermy, Polo, Pet

Introduction: My name is Ouida Strosin DO, I am a precious, combative, spotless, modern, spotless, beautiful, precious person who loves writing and wants to share my knowledge and understanding with you.