DEV Community

loading...

Close all open Vim buffers except the current

Rickard Natt och Dag
・1 min read

During development, I often use :ls to display open buffers. However, after a while, the list can become long. I'm going to add a custom command in my .vimrc – which is similar to "Close others" in VS Code – that deletes all buffers, except the one I'm currently on.

command BufOnly silent! execute "%bd|e#|bd#"
Enter fullscreen mode Exit fullscreen mode
  • command – Define a user command
  • BufOnly – The command name we want to use
  • silent! – Silence messages, ! silences errors too
  • execute – Execute the following string expression

Now let's breakdown the actual command. The pipes (|) break the string into three commands:

  • %bd – Deletes all open buffers (bd is short for bdelete)
  • e# – Opens the last buffer (e is short for edit)
  • bd# – Deletes the [No Name] buffer that gets created

After restarting Vim or sourcing the updated .vimrc I can run :BufOnly to clean up my :ls list.

Quick command

Create a binding to run the command quickly whenever needed.

" I have <leader> mapped to <Space>
nnoremap <leader>b :BufOnly<CR>
Enter fullscreen mode Exit fullscreen mode

Discussion (0)

Forem Open with the Forem app