Hacker News new | past | comments | ask | show | jobs | submit login

I've been using Vim since 1994, but only this past year have I started getting better at juggling large numbers of buffers. There are three main improvements in how I use Vim compared to before and they are simple.

1. I switched to :autochdir, so that in any buffer, the current working directory is that of the file. Then I dumped :autochdir due to certain annoyances and found a nice way of emulating its good behaviors.

2. I started using sessions to save and recover the states of the buffers. I associate sessions with certain tasks and can return to those tasks quickly.

3. I started using a buffer navigator.

Regarding (1) here is the setup:

  :let $top=getcwd()
  :au BufWinEnter * exec "lcd " . expand('%:h')
  :au BufWinEnter * if expand("%") == "" | exec "lcd $top" | endif
  :au BufWinEnter *.git/* exec "lcd $top"
I know you don't need the colon in your vimrc, but I have it anyway.

The $top variable captures the top level directory on startup; I refer to it in command lines when I need it using $top. Why the dollar sign? Ah, this makes it an environment variable. Vim is much better about interpolating environment variables than other variables; there are more places you can use them thanks to the dollar sigil.

The other three commands above set up a local chdir (lcd) to the directory of the file containing the buffer. The third rule is smart: if we are editing something under .git/ then we go back to the top. This is what autochdir gets wrong. What do we edit under .git? Oh, COMMIT_EDITMSG, for one thing! I want to be in the repository root when editing a commit message.

Regarding 2, here is my setup for working with sessions:

  :nmap + :wa<Bar>exe "mksession! " . v:this_session<CR>
  :command -nargs=1 S exec "mksession! " . expand('$top/') . expand('<args>')
+ becomes a command for saving the current session. Then I have a :S <whatever> command to save a session under a new name, relative to the top directory: the $top variable rears its head.

Regarding 3, there are many buffer navigators. I'm using bufexplorer (version 7.4.32). I make the following tweak to the plugin/bufexplorer.vim file:

  " added before the other maps

  if !hasmapto('BufExplorer') && g:bufExplorerDisableDefaultKeyMapping == 0
      nnoremap <script> <silent> <unique> <Leader>\ :BufExplorer<CR>
In Vim the "leader key" for hot key commands is backslash by default. Instead of using the default key binding for launching the bufexplorer view, which is like \be or something, I map the backslash key to do that. So just by hitting \\ (backslash twice) I get the buffer explorer.

Mostly I use the LRU view in buffer explorer (most recently used buffer goes to top, pushing others down). Should be called MRU, but everyone who knows memory cache hierarchies understands LRU.

The above three things are very simple, and have a big payoff. I have many more tricks in my vimrc. For instance I use a C program called autotab to analyze a sample of lines from the loaded file and adjust the indentation settings.

Applications are open for YC Winter 2024

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