16

I recently read about the black hole register "_ which is a write-only register.

Vim documentation describes it this way:

  1. Black hole register "_

When writing to this register, nothing happens. This can be used to delete text without affecting the normal registers. When reading from this register, nothing is returned.

In all the articles and books I read the authors always use the same example which is to use this register when we need to delete some text without erasing what we previously yanked.

I understand this use but I'm surprised by the fact that I can't find other use cases of this register, which leads me to 2 questions:

  • Is this register useful in some other use cases? If yes, which ones?

  • If the only purpose of this register is to be used in the use case I quoted before, why did the creators of Vim decided to solve this problem with such a register? I mean this answer shows a solution to the problem which implies less keystrokes than using "_. Using "0 can also avoid the use of the black hole register so why did they create it?

3 Answers 3

20

The black hole register is used in the same situations as /dev/null: when you do an action that normally outputs something but you have no use for that output.

Vim's default behavior is to "cut", not "delete". In most cases it doesn't matter but, sometimes, users may actually need to "delete". That's where the black hole register comes handy.

Is this register useful in some other use cases?

The black hole register is here to let us cut stuff without clobbering the unnamed register and the numbered registers.

The possible use cases are numerous but all unsurprisingly very similar:

  • provide an actual "delete",
  • have a better control over registers, mainly the unnamed one,
  • keep the numbered registers in a stable state,
  • cut something while retaining the ability to put from the unnamed register, giving us "repeatable puts",
  • put something over something you don't care about,
  • prevent side effects in scripts…

One could use "0p for the "repeated puts" scenario (the most common use case, hence its ubiquity in the Vim literature) but that register is far from being as useful as "_ for the other scenarios.

4
  • I see your use cases more as consequences of the behavior of this register, but I think your answer clearly describe this register with ` The black hole register is here to let us cut stuff without clobbering the unnamed register and the numbered registers.` I was expecting other answers but it seems that it's the only use of this register. Thank you a lot for your answer!
    – statox
    May 27, 2015 at 5:40
  • Stating the obvious, but an example use of the black hole register is "_diw which will delete the inner word to the black hole register, effectively deleting it instead of cutting it.
    – icc97
    Nov 7, 2022 at 17:58
  • Is there some kind of 'canonical' resource for "repeatable puts"? The Pasting from Visual mode explains it very well, but is there anywhere else that explains it? For example is it somewhere in the actual vim docs?
    – icc97
    Nov 7, 2022 at 18:00
  • @icc97 "If you do need it you can use p with another register. E.g., yank the text to copy, Visually select the text to replace and use "0p . You can repeat this as many times as you like, and the unnamed register will be changed each time." from :help v_p, which is not linked from many places.
    – romainl
    Nov 7, 2022 at 19:27
9

Besides deleting text, you can use the black hole register to delete other registers. Example:

:let @a=@_

This is one way of clearing register "a.

3
  • 3
    In case anyone else reading this thinks, "I wonder if this is a shorter, clearer replacement of :call setreg('a', [])," as I did: regrettably, the answer is no. It acts like :call setreg('a', '') (leaving "a in the output of the :registers command.
    – Rich
    Mar 5, 2018 at 15:42
  • @Rich What's the significance of leaving "a in the output of :registers for the uninitiated? Jun 7, 2019 at 18:41
  • 1
    @AlexejMagura Well, if your goal is to tidy up what you see when viewing your register contents, then leaving a bunch of empty registers might not be ideal. I'm not sure if there's any other practical difference between a deleted register and an existing one that happens to be empty.
    – Rich
    Jul 10, 2019 at 10:53
1

One practical use in one function. In the bellow function the python comment # is added and removed depending on the result of getline('.')[0]. If the line has a comment the char # is deleted, but in this case we don't want it to mess with our default register.

function! ToggleComment()
    let l:win_view = winsaveview()
    if getline('.')[0] == "#"
        normal! 0"_x
    else
        normal! I#
    endif
    call winrestview(l:win_view)
endfunction
nnoremap <Leader>t :call ToggleComment()<CR>
vnoremap <Leader>t <C-o>:call ToggleComment()<CR>

Your Answer

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

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