When you launch Vim
It’s important that you always launch Vim from the command line and at the root of your project. I’ll explain why later in the section on projects, but for now just remember to start your work from there.
From your shell
We already saw this one in the hello world:
$ mvim path/to/file.txt
Every time you do this you’ll open up a new window. It also means leaving Vim and both of these will get annoying pretty quickly. Fortunately, there are other options.
From within Vim
Remember how I said that Vim was a platform for creating the perfect editor for you? Well, there are many ways to open a file in Vim, just like all the modern editors.
If you’re using MacVim Command+o will bring up the open file dialog just like any other desktop app. GVim on Linux can do the same thing, although it’s probably Control+o
Type the path
If you just want to type in the path to the file you want to open then Edit is your friend.
If you enjoy something like Atom’s fuzzy-finder (
⌘+Shift+p ) you
should check out the Control-P Plugin.
Visual folder structure
If you like a clickable sidebar with the folder structure of your project then Vim’s Netrw is what you want.
Replaces the text you were working on with the contents of the file you told it to edit. The path is relative to the root of your project and, as with your shell, tab completion works to help you find it faster. Note: this doesn’t get rid of the changes you were making to the file that was originally open. It will just change what’s being shown in the viewport. Your changes will hang around in a buffer.
This tells Vim to always assume the “current directory” is the same one as the file buffer that currently has focus.
If you use
:edit and are 3 directories down in your project you’ll have to do
something like this to open a file in the root of the project:
I hate having to count how many directories down in a project I am especially when I have to go up some to get to the root and then down some to get to the file I want to open. So I never use this option.
See Plugins for how to install and manage plugins.
Control-P is one of my favorite Vim plugins. It’s got a bunch of functionality that I never use, because its default setup is just about perfect.
After installing add the following to your
let g:ctrlp_map = '<c-p>' " ^^ enables control-p to be invoked by typing control+p let g:ctrlp_cmd = 'CtrlP'
If you don’t you’ll have to type
:CtrlP to invoke it and that sucks.
Netrw is a terrible name. I think it means “Net Read Write” because, one of the many things it can do is let you edit files over a network. The manual doesn’t actually say, and it doesn’t matter, because we’re going to ignore that and use the feature most folks use it for: “exploring” directories.
The default view has a rather cluttered banner at the top with lots of
information you don’t need. I’m going to show you how to clean it up to look
more like the tree view you see in Atom or Sublime. Before that, you may want to
take a quick look. Type
:Vexplore Take a glance. After taking a peek,
split window that appears (your cursor will change focus to that one) Now, let’s trim out everything except the actual directory browser.
We’ll do this by adding to your ~/.vimrc.
let g:netrw_banner = 0 " removes the netrw banner let g:netrw_liststyle = 3 " tree style listing let g:netrw_browse_split = 4 " see help on this one. lots of useful options let g:netrw_altv = 1 " change from left splitting to right splitting let g:netrw_winsize = 25 " initial size of new explore windows " By default netrw leaves unmodified buffers open. This autocommand " deletes them when they're hidden (using :q for example). autocmd FileType netrw setl bufhidden=delete
Once you’ve done that (and loaded the changes) it’s time to open it up. You can open it up as a vertical sidebar with
or across the bottom with
or, if you want lots of real estate you can replace the entire window with the explore view
Each of them will bring up a file explorer that starts in the root of your
project. Move your cursor onto any of the folder names or files and hit
<Enter> to open them, or you can click with your mouse if you’re so inclined.
See File Relativity above if you’d prefer the directory brower to start its view in the same folder as your current file.
If you’d like to have the file browser open by default you can add
this to your
augroup ProjectDrawer autocmd! autocmd VimEnter * :Vexplore augroup END
Sometimes you open it and decide you don’t need to switch files. If you’ve
opened it along the side or bottom, then it’s just a
and you can just quit it with
:q like anything else. It’s a bit trickier if
:Explore and it’s taken over the whole window. Quitting that
will make the whole window go away. Instead you need to delete the
buffer it’s in. To do that you execute the
buffer delete command with
I like netrw but I don’t like typing
:Vexplore or even
:Vex So I use this
function to toggle it with
" and a handy-dandy function to toggle a Netrw sidebar " Toggle Vexplore with Ctrl-E function! ToggleVExplorer() if exists("t:expl_buf_num") let expl_win_num = bufwinnr(t:expl_buf_num) if expl_win_num != -1 let cur_win_nr = winnr() exec expl_win_num . 'wincmd w' close exec cur_win_nr . 'wincmd w' unlet t:expl_buf_num else unlet t:expl_buf_num endif else exec '1wincmd w' Vexplore let t:expl_buf_num = bufnr("%") endif endfunction map <silent> <C-E> :call ToggleVExplorer()<CR>
If you wan it to always launch when you launch vim you can add this to your vimrc.
" To show a vertical file explorer when you launch vim augroup ProjectDrawer autocmd! autocmd VimEnter * :Vexplore augroup END