vim

Open Oft-used Files when Starting Vim

fbicknel

Frank Bicknell

Posted on August 24, 2024

Open Oft-used Files when Starting Vim

Admit it. You've got some files in your project that you work on often and you're tired of typing their filenames or finding them in a list ... laziness is a virtue.

So long as your vim session is open, you can use Vim's buffer system to access those files quickly and easily. I know my main site playbook is buffer #1 and my inventory is in buffer #3, so :b 1 and I'm there. I don't want to have to resort to even typing a portion of the filename: :b site<tab>.

Supreme laziness is a supreme virtue.

But I might work on something else when I first open the editor. The coveted buffer #1 position is taken by that. Suddenly, my lazy system is ruined and I can't be lazy.

True, you can only do this for a few files. And fortunately, it's not often you have more files that merit this treatment than you can write on the fingers of one hand.

There is a solution. Well, as always, there's more than one solution, but I think this one is better than others.

Especially if you work on more than one project, this solution give you a little laziness system for each of your projects. Assuming, that is, you don't have too many projects.

The solution is to create a map for each project that opens a handful of files that you commonly work on for that project. Put this map in your .vimrc and when you've moved to the project directory and opened vim, all your most-used files are open with two keystrokes.

Here, let me demonstrate:

Say you have three files in the web project you're working on that you nearly always want to refer to or work on. Those files are index.html, main_page.html, and editors_letters.html. They're in different, insanely complicated and deep directory structures.

Add this to your .vimrc:

map <Leader>f :args webpages/index.html content/main_page.html editorial/editors_letters.html<CR>
Enter fullscreen mode Exit fullscreen mode

Ok, so content/ and webpages/ is not insanely complicated. Admittedly, this example is contrived, but it serves, I think.

Now when you open vim, of course there are no files and no buffers open. Buffer 1 occupies the first position, but it just says [No Name].

Just hit your leader key (usually this is \, but you can change that) and f in quick succession. Just like that all three files are open and if you type :ls, you can see them all in buffers:

:ls
  1 %a   "webpages/index.html"           line 1
  3      "content/main_page.html" line 0
  4      "editorial/editors_letters.html" line 0
Enter fullscreen mode Exit fullscreen mode

There remains the mystery of why Vim has skipped buffer #2, but maybe someone who knows better can comment on that.

Ok, so the extension of this simple trick is equally simple: use a different mapping for each of your different projects. Here's where things break down if you have more than 26 52 projects. Yes, you can also use capital letters. Actually, you can also use numbers and most non-alphanumeric ascii characters in mappings, but I digress. Suffice to say there are a LOT of projects you could represent; likely more than you could remember without sticking Post-It notes all over your monitor to remind you what project <Leader>~ represents.

map <Leader>F :args foodir1/foo.yml bardir1/foo.yml bazdir1/snort.yml<CR>
Enter fullscreen mode Exit fullscreen mode

I hope this helps you get "started" in all of your projects.

💖 💪 🙅 🚩
fbicknel
Frank Bicknell

Posted on August 24, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related