DEV Community

Rickard Natt och Dag
Rickard Natt och Dag

Posted on

4

Close all open Vim buffers except the current

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

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

Top comments (0)

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay