Quickly access project notes

4.4
(26)

We haven’t written much that makes use of Vim’s underutilized tab features. Most Vim power users I know prefer splits as opposed to tabs for separating out contexts. Vim tabs add another layer of abstraction on top of your terminal’s tabs and that of your terminal multiplexer (like screen and tmux). But the one place I find tabs useful in Vim is for accessing notes about my project.

I use the following command to quickly open a notes file in the current project directory:

:tabedit tmp/notes.md

This tells Vim to open a new buffer at the file path tmp/notes.md and to open it in a new Vim tab. The use of a tab here is helpful because I generally want the notes file out of the way of any open source code files I have splayed across my screen. I can quickly get back to my main tab with gt (the mnemonic is “goto tab”). Since I only have one one tab open, I can use gt to continually swap back and forth between my code and my notes.

Having the notes accessible quickly, but out of the way, is a huge advantage of the tab approach. If I keep my notes open in a split, then I am constantly moving code splits around my note split.

We can shorten :tabedit to simply :tabe to make this command shorter. But accessing my notes is such a common task that I use a mapping for this. Let’s try putting this in our .vimrc:

nmap <script>n<CR> <SID>:tabe tmp/notes.md<CR>

Now we can simply press n<enter> to open our notes file in a new tab. But what if that tab is already open? Running the mapping again will open the notes tab a second time. That’s not exactly the best experience.

There’s a better way: with Vim’s :drop command. With :drop, Vim will open the file if it’s not already open, or jump to the open buffer if it is. Combine with :tab and we get an even better mapping:

nmap <script>n<CR> <SID>:tab drop tmp/notes.md<CR>

Watch the video below of this workflow in action. Perfect! Now it’s exactly what we want: if the tab is already open, we go right to it. We can jump back to our main tab with gt. And if it’s not already open, Vim opens it as a new tab and jumps to it.

We’re working on a course all about note-taking in Vim. It’s been delayed a few times because we really want to make it the best it can be. But one day we will put this course out so if you want, you can pre-order it now. You won’t charged at all until we publish the course.

How useful was this tip?

Average rating 4.4 / 5. Vote count: 26

No votes so far! Be the first to rate this tip.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Written by

Colin Bartlett

81 Posts

Vim enthusiast and software developer for more than 20 years.
View all posts

1 thought on “Quickly access project notes”

  1. This tip Looks very interesting but I’m having a problem 🙁

    I Added:

    nmap n :tab drop tmp/notes.md

    to my .vimrc file

    it show in nmap display as:

    n n & :tab drop tmp/notes.md

    But doesn’t seem to work for me… It just does the regular “search for next occurrence” which is the usual normal mode response to “n”.

    If not too much work … can you please explain the and elements in the above (others might be interested too). I googled and found a opaque explanation of but my limited *google-foo* didn’t find a useful “” reference and ….. curious minds just need to know :-).

Leave a reply

Your email address will not be published. Required fields are marked *