DEV Community

Cover image for Scratch notes in vim
Igor Irianto
Igor Irianto

Posted on • Edited on • Originally published at irian.to

6 1

Scratch notes in vim

Follow @learnvim for more Vim tips and tricks!

While coding, sometimes I need to quickly take notes to capture random thoughts. Mac's Notes used to be my default go-to app. However, wouldn't it be nice if I can take scratch notes inside Vim so it won't disrupt my flow?

Well devs, if you're wondering the same thing, you're in for a treat! Vim has a built-in ability to write scratch notes and here is how you can do it too!

Taking notes

Before we start, let's make sure we are on the same page. In my ideal world, a scratch note needs to:

  1. Not be saved into our directory (otherwise we are creating new file)
  2. Not get lost when hidden
  3. Be quick to access

Powered by :new

Vim's scratch note starts from :new command (:vnew works too), a command to create new window.

Once a new window is generated, we need to add additional setups:

:setlocal buftype=nofile
:setlocal bufhidden=hide
:setlocal noswapfile
Enter fullscreen mode Exit fullscreen mode

I will explain what they do:

  1. setting buftype=nofile will tell vim that this newly-created buffer should be considered nofile type. If you noticed in buffers list (:buffers), originally our new file is listed as "[No Name]". After adding :setlocal buftype=nofile, it will now be listed as "[Scratch]"! This means Vim categorizes any nofiles as scratch file type.

  2. Adding bufhidden=hide will keep it persistent when the buffer is hidden. Without this setting, if your current scratch buffer gets hidden by another buffer, it will disappear... forever 😱😱!! Adding bufhidden=hide prevents this so it is accessible until we exit vim.

  3. noswapfile prevents swapfile from being generated.

Improvements

Although I think this capability is great, it is still not quick to access. We have to call 3 setlocals each time we generate new scratch notes. Wouldn't it be nice if we can setup our vim to automatically perform those setups?

After playing around a bit, here's an example script I came up to address that issue. Add this inside your .vimrc:

function! s:ScratchGenerator()
  echom "Creating scratchy..."
  exe "new" . "__Scratchy__"
  echom "Scratchy created!"
endfunction

function! s:ScratchMarkBuffer()
  setlocal buftype=nofile
  setlocal bufhidden=hide
  setlocal noswapfile
endfunction

autocmd BufNewFile __Scratchy__ call s:ScratchMarkBuffer()
command! Scratchy call s:ScratchGenerator()
Enter fullscreen mode Exit fullscreen mode

What's going on here? I'll start from the end:

command! Scratchy call s:ScratchGenerator()
Enter fullscreen mode Exit fullscreen mode

This adds a new command :Scratchy. When we invoke it, it calls ScratchGenerator function, which creates a new file named "__Scratchy__".

autocmd BufNewFile __Scratchy__ call s:ScratchMarkBuffer()
Enter fullscreen mode Exit fullscreen mode

This is an autocommand that gets invoked if new buffer file (BufNewFile) is created and that buffer is named exactly "__Scratchy__".

Source your .vimrc. You can now create a scratch note by typing command :Scratchy from normal mode!

To access it even quicker, you can add shortcut.

I like to keep it memorable, so my shortcut choice is Ctrl+s (for scratch). The mapping is:

nnoremap <C-s> :Scratchy<CR>
Enter fullscreen mode Exit fullscreen mode

Now every time I have an itch for scratch notes (pun intended 😃), I can just press Ctrl+s!

Alternatively, there is a vim plugin called scratch.vim with more feature. Experiment to create your perfect scratch note!

That's it! Now you can scratch that off your list!

Resources

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay