Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I've become comfortable with using vim /within a file/, what I'm having trouble with is using vim while having multiple files open (keeping track of which ones I've saved, switching between them, etc.)

My current workflow is to have multiple files open each in a separate terminal tab, but navigating between them Cmd+Shift+[ and ] feels suboptimal.

Does anyone have any great tips? I work on Java and Ruby (rails) projects most of the time.



I used to use splits as some of the sibling comments describe. This was helped by bindings for C-j and C-k that would maximize the split when moved to (vertically). So, I had a stack of splits and I would rearrange them for relevance to each other as needed for faster switching. But it was still tedious going up and down the stack sometimes.

Then I discovered buffers which was life changing. Suddenly I could manageably switch between 10s of files and never really worry too much about closing them. Sessions are helpful too for truly long running editing and maintaining a "workspace".

The buffer pro tips are:

- Use :set hidden

- ^ to switch to last buffer

- Switch buffers with :ls which will stay visible after typing :b and you can switch by number, or you can switch by name with tab completion (even by going straight to :b). So, I can switch to IWouldRatherBeWritingSomethingElse.java with ":b IWo<TAB>".

There are plugins to make switching even easier with fuzzy finding and whatnot but the above is all vanilla vim so you can do all this on some random cloud host anytime. I can't recommend a plugin because before really settling on one, I switched my serious development to Doom Emacs which has very similar buffer semantics and switching bells and whistles.

Check out Drew Neil's awesome Vimcasts to see buffers in action. http://vimcasts.org/episodes/working-with-buffers/


Thank you, this is helpful!


You're welcome!

One small correction: Switching to the last buffer is actually C-6 not ^ (ctrl + 6 rather than shift + 6). I realized that when actually sitting at a computer whereas my comment was typed on mobile. Whoops. It's all muscle memory by now!


I use Vim for development with multiple files; I find that up to 9-12 open files are manageable, but more than that and the lack of a file browser impedes me[1].

Example usage:

Open files in vertical splits:

    vim -O src1.j src1.c src2.c
Open files as I need them in new tab:

    :tabe src3.c
In new tab, open two more files (as I need them):

    :vsplit src3.h
    :vsplit CHANGELOG
I've mapped Ctrl-PageUp/Down to next previous tab/next tab.

When I want to exit, I save the session:

    :mks!
When I want to restart that session:

    vim -S Session.vim
Like I said, it gets unwieldy after around 9-12 files open (typically 3 in a tab).

[1] Nerdtree messes up my splits so I don't use it.


FZF.vim is what you need, both for opening files and switching buffers.


As an exercise to the reader, I'd recommend looking up how to use vim buffers. I'm sure a web search would yield plenty that will be useful. For example you can open all the java files in a directory with >vim *.java . All the files will be loaded in vim and you can switch between them with :bn :bp. You can open the previously open buffer with :e#.

But I personally tend to use split windows and tabs. :split and :vsplit to split a window. Ctrl w and navigation keys to move between them. Each window uses buffers the same way, so within each window you can cycle between buffers, open different files, etc...

Tabs are also useful for keeping collections of similar windows organized. Unlike a lot of other editors where a single file is associated with a single tab, in vim, a tab is just another collection of windows/buffers. So you can have one tab that is split into two windows, say for a cmake file and config file, then another tab with three vertical splits, each with a different .java code file, then another tab with a log file. (:tabnew for a new tab, :tabn and :tabp for moving to next/previous tab, or just ctrl (+shift) + PgUp/PgDn depending on platform).

And the really nice thing about all this is that you can save your vim session like this with :mksession _your_session_name.vim, close out of vim entirely, then restore your session, with all it's tabs, windows, files, etc... by opening _your_session_name.vim with >vim -S _your_session_name.vim . And you have everything back just the way you left it. I use this all the time when working on various aspects of a code base; a different vim session with all the files/tabs/etc... that are most relevant to whatever I'm working on. Need to switch tasks? Save my session, and load the one for the other task and I've instantly got everything up that I need for that task.

Vim also has a built-in file browser, so you can navigate the file tree by simply "editing" a directory, e.g. :e. will edit the current directory and you can navigate the tree and select a new file to edit by moving the cursor to its name and pressing enter. Done with the file? :e# will bring back the previous buffer, which was the file navigator.


I have taken a while but I live in split windows. Ctrl-v to split horizontally, "new" to do so vertically. Ctrl-v them h,j,k,l to navigate between splits. I have two nice short cuts that help you open files relative to the directory of the file in which ever split you are in!!

I am not a vim expert and I usually stick to a vimrc file that intake with me everywhere and I get my zen quickly. Happy to share more.


Less of a core vim tip, but since it's vim-style: I like to use ctrl shift H and L to move L/R between tabs in whatever terminal app I'm using.

(And since I'm open to learning other tips, I'm just going to say that this is the best method possible for navigating the tabs, period)


I used to use the Ctrl-P plugin, but to be honest, these days I just use an IDE with a vim plugin. Usually if I'm in a remote terminal session, I don't have more than a few files open and can get by with :bn.


I'm using these for switching buffers:

  " Move between buffers
  nnoremap <silent> <c-l> :bn<cr>
  nnoremap <silent> <c-h> :bp<cr>


I use several things to work better with multiple files, in a tree:

1. Vim sessions: saving named sessions and restoring. (With some custom commands, see below.)

2. A plugin called BufExplorer (slightly hacked, see below).

3. My own implementation of "autochdir" (current working directory follows file).

My own implementation of "autochdir" consists of:

A variable $top bound on startup which keeps track of the original directory where Vim is executed. This is assumed to be the root of the project. The absolute path to the root is available in $top, which can be interpolated almost anywhere.

  :let $top=getcwd()
The following three commands implement the behavior similar to Vim's built in autochdir:

When we load a buffer, change to its directory via "lcd" (local cd):

  :au BufWinEnter * exec "lcd " . expand('%:h')
If the file name is blank, go to the top directory:

  :au BufWinEnter * if expand("%") == "" | exec "lcd $top" | endif
If the file is under .git/, likewise go to the top. E.g., when we are editing ./git/COMMIT_EDITMSG, we don't want to be under .git.

  :au BufWinEnter *.git/* exec "lcd $top"
For helping with sessions:

:S colon command for creating a session. Argument is a name, interpreted relative to $top (defined above). For example :S foo saves a session called $top/foo.

  :command -nargs=1 S exec "mksession! " . expand('$top/') . expand('<args>')
+ command for saving the session at any time. Overrides the + which goes to the next line (never used by me). You just hit + in command mode to save the session.

  :nmap + :wa<Bar>exe "mksession! " . v:this_session<CR>
A saved session is restarted with "vim -S <sessionfile>".

My small for BufExplorer is this. The default command for launching BufExplorers menu is the Vim leader character (\) followed by "be". I found this to be obtuse so I changed it to be \ followed by \. Thus two backslashes brings up the BufExplorer screen:

  :nnoremap <script> <silent> <unique> <Leader>\ :BufExplorer<CR>
BufExplorer is a window in which you have an interactive view over the buffers. They can be shown in various orders like most recently used first. You can move around in them, hit Enter to activate the one under the cursor, d to delete, search through them, and so on. There are other buffer managers for Vim.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: