1

I often use Vim to manipulate clipboard data.

  • I create a new buffer :enew
  • Paste the data in it
  • Modify the data
  • Yank it :%y

At the end of my day, I end up with several [No Name] buffers.

I want to delete :bd! from the list of [No Name] buffers.

How to do that?

I had the idea to use the arglist and to do :argdo bd! but I don't know how to populate the arglist with the [No Name] buffers

11
  • 1
    Does this answer your question? Efficient way of cleaning up the buffer list
    – Matt
    Oct 27, 2023 at 18:51
  • 1
    You could avoid the issue by using a scratch buffer (roll your own or use one of the several plugins).
    – Friedrich
    Oct 27, 2023 at 19:17
  • 1
    something like this perhaps? :bufdo if empty(bufname('%'))|bw|endif Oct 28, 2023 at 8:40
  • 1
    I seem to be late to answer. I can't really recommend anything. I had the original scratch.vim installed and used it now and then. Now that I don't have it, I don't miss it. There days, I'd take a look at its successor of the same name. Also, the answers to this question may be relevant.
    – Friedrich
    Oct 29, 2023 at 7:44
  • 1
    @VivianDeSmedt You might like vimclippy. It's a much more streamlined way to use Vim to edit your clipboard. (N.B. It's not a plugin.)
    – Rich
    Nov 3, 2023 at 17:00

3 Answers 3

2

In the spirit of Friedrich's comment, using scratch buffers instead of regular buffers would seem to be a better strategy.

FWIW, this is a very common use case for me, which I address with the following command:

command! SC vnew
        \ | setlocal bufhidden=wipe buftype=nofile nobuflisted noswapfile
        \ | nnoremap <buffer> ,<CR> :silent %source<CR>

With that, I get a scratch window like this:

scratch window

From there, you can get fancy with a buffer-local mapping that yanks the buffer and closes the window, setting a default filetype, etc.

It works like this:

  • bufhidden=wipe wipes out the buffer when it is hidden or when the window is closed,
  • buftype=nofile prevents accidental write,
  • nobuflisted prevents the buffer from polluting :ls, :b, etc.
  • noswapfile makes sure no swap file is created for that buffer.

See :help special-buffers.

NOTE: before the behavior of :source was changed, the mapping looked like this:

nnoremap <buffer> ,<CR> :silent %y\|@"<CR>
3
  • Thanks I like this solution very much. I like to be able to list these buffer too. I'll keep a simple: command! SC vnew | setlocal buftype=nofile noswapfile Oct 28, 2023 at 13:10
  • Upvoted for flattery. However, the nnoremap in the third line seems unrelated to Vivian's question. When just doing some back-of-the-envelope editing, it's hardly ever necessary to source the buffer, is it? Or maybe it passed above my head...
    – Friedrich
    Oct 30, 2023 at 20:54
  • 1
    Indeed, OP didn't explicitly ask for that. But I lifted that snippet verbatim from my vimrc and I thought that providing a real world example would somehow be helpful. BTW, my ego says thanks for the internet points.
    – romainl
    Oct 31, 2023 at 7:54
1

Turning my earlier comments into an answer to preserve them for future readers.

Instead of creating a lot of unnamed buffers and cleaning them up later, a scratch buffer may be used to do back-of-the-envelope editing work. One can write a command to create a scratch buffer or use an existing plugin.

For DIY instructions, refer to the accepted answer by romainl or the answers to What is a scratch window?

There are also some plugins that provide scratch buffers:

The first one to do so was vim-scripts/scratch.vim, AFAIK. While it doesn't offer a lot functionality, it does one thing and it does it well.

There's also the more modern mtth/scratch.vim which traces its ancestry back to the original scratch.vim above and adds some configuration options and convenience mappings.

My personal experience is that I used the original plugin occasionally over the course of several years. I don't have it installed any more and I rarely miss it. I never tried out its successor but it looks promising.

I asked myself why I'm not using scratch buffers anymore. I found that I rather start a new instance of Vim, do my editing and throw the garbage away with :q! once I'm done. Certainly not the most elegant way to do it. But then, Vim starts instantly and I need to go through system clipboard most of the time, anyway.

1
  • 1
    I also have pbed() { pbpaste | vipe | pbcopy } on macOS for editing the clipboard. I frequently :new, edit, :bw for a scratch buffer.
    – D. Ben Knoble
    Oct 31, 2023 at 13:35
1

I also like to use Vim to edit my system clipboard, and I wrote a couple of blogposts about how I've configured my devices to streamline this.

The basic idea is to use Vim's features to automate moving the text from the clipboard into Vim, and back again after editing, so all you have to do is actually edit the text.

As I'm generally not in Vim when I want to edit my clipboard, I've set up a function in my shell to achieve this (works in at least bash and zsh):

vimclippy() {
  vim +'silent pu!+' +'$d _' +'1' +'set nomodified' +'au BufWriteCmd vimclippy %y+ | set nomodified' vimclippy
}

...but if you want to be able to do this from inside Vim, it's easy to set up a command, too:

function! s:vimclippy() abort
  edit vimclippy
  silent put! +
  $delete _
  1
  set nomodified
  augroup vimclippy
    autocmd!
    autocmd BufWriteCmd vimclippy %yank + | set nomodified
  augroup END
endfunction

command VimClippy call s:vimclippy()

This also allows us to easily create a version that works in the standard Windows command prompt, too*, by dropping a file named vimclippy.bat somewhere in your Path with the contents:

vim +VimClippy

All the versions work in the same way: starting from an empty buffer, they:

  1. :put the contents of the system clipboard,
  2. :delete the extra blank line at the end of the buffer,
  3. Move back to the start of the buffer,
  4. Set up an autocommand to replace the normal behaviour of :w so that instead of actually writing the buffer, it instead yanks its contents back into the system clipboard.

*: I'm sure it's easy for someone that knows how to convert the original shell command into something that works in the Windows command prompt so you don't need to update your Vim configuration. I am not that person.

6
  • thanks for the answer. I'll add the command to by _vimrc. I'm running on Windows with set clipboard=unnamed. The core part of the solution works fine if I replace: " by: *. The initial shell command needs also some adaptation to Windows (I suppose I'll manage to adapt it). Maybe starting with the core of the solution avoid that some Windows user reads it :-) Nov 6, 2023 at 12:28
  • @VivianDeSmedt If you've already set up the :command, then you can just run vim +VimClippy in Windows command prompt. I'll update my answer to make it a bit more Windows-friendly. Thanks for the notes!
    – Rich
    Nov 6, 2023 at 15:02
  • it works great thanks. I took the freedom to make two small changes in your solution such that. The clipboard is not modified and if not modification are made in the buffer the user is not forced to save it. Nov 6, 2023 at 15:42
  • 1
    The missing set nomodified was a bug in the function (you'll see it was present in the original shell command), so thanks for fixing it! Using + works fine for me though on Windows, and * refers to something else in the X11 GUI version, so I reverted that change.
    – Rich
    Nov 6, 2023 at 15:53
  • 1
    thanks it works fine for me with + replaced by * and the shell command gVim +VimClippy. Nov 6, 2023 at 17:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.