Why does vim use registers for almost every command?

151 views Asked by At

I love the simplicity of composing commands that have semantic meaning. Like, for example cib reads like a sentence change in brackets - wonderful.

But why does vim need to copy the old contents into my clipboard? Nowhere in that command have I suggested I want to copy it and the problem goes much deeper.

dw diw etc all copy to the my clipboard/register as well. Why? It seems like this abandons the semantic value of these commands and I would say it is unexpected behaviour.

Am I using these commands wrong, or is there some way to completely disable this feature? Currently I have done a few remappings like this:

nnoremap dd "_dd

nnoremap cc "_cc

but I would like to not do that for every single possible combination of non-explicit copying.

2

There are 2 answers

0
TankorSmash On BEST ANSWER

By default, most of the commands you're talking about use the unnamed register, ". It sounds like you're dealing with the clipboard being overwritten for all these things too, which can be a symptom of setting clipboard to unnamed or unnamed_plus.

To go back to standard, you can probably do set clipboard=, if the output of set clipboard? is one of those two options.

                        *'clipboard'* *'cb'*
'clipboard' 'cb'    string  (default "autoselect,exclude:cons\|linux"
                          for X-windows, "" otherwise)
            global
            {not in Vi}
            {only in GUI versions or when the |+xterm_clipboard|
            feature is included}
    This option is a list of comma separated names.
    These names are recognized:

                        *clipboard-unnamed*
    unnamed     When included, Vim will use the clipboard register '*'
            for all yank, delete, change and put operations which
            would normally go to the unnamed register.  When a
            register is explicitly specified, it will always be
            used regardless of whether "unnamed" is in 'clipboard'
            or not.  The clipboard register can always be
            explicitly accessed using the "* notation.  Also see
            |gui-clipboard|.

                        *clipboard-unnamedplus*
    unnamedplus A variant of the "unnamed" flag which uses the
            clipboard register '+' (|quoteplus|) instead of
            register '*' for all yank, delete, change and put
            operations which would normally go to the unnamed
            register.  When "unnamed" is also included to the
            option, yank operations (but not delete, change or
            put) will additionally copy the text into register
            '*'.
            Only available with the |+X11| feature.
            Availability can be checked with:
                if has('unnamedplus')
0
Amadan On

I'll offer a dissenting view, and suggest that you are likely using Vim non-idiomatically. This is in no way unexpected behaviour. Are you using cib as a preparation for pasting, so that cib screws it up? Do vibp instead.

dw diw etc all copy to the my clipboard/register as well. Why? It seems like this abandons the semantic value of these commands and I would say it is unexpected behaviour.

d then p is the normal Vim idiom for cut-and-paste (i.e. move text). This is an operation I do every day, multiple times per day, and it would be really annoying if d did not also yank. You seem to think d is equivalent of Del on other text editors; it is rather the equivalent of ctrl-x (cut). To cancel this, you'd do "_d as you said; I find that I hardly ever need it.

As an advanced example, the default semantics of c and visual-mode p makes it trivial to exchange two objects; for example:

I drank all their food and ate all their whiskey.

Go to "drank", diw (delete the word and yank it), go to "ate", viwp (select a word and paste over it, yanking the previous content), ctrl-o to go back to where "drank" was and P (paste before cursor):

I ate all their food and drank all their whiskey.

(I also have a plugin which defines "function parameter" as a text object, so I use the same idiom in coding if I mix up, or refactor, the parameter order.)

The only thing I want to prevent more commonly is yank-on-paste in visual mode (so that I can paste the same content multiple times); for this, I use

xnoremap <expr> P '"_d"'.v:register.'P'

(from here). This only remaps P in visual mode (which is otherwise identical to p in visual mode). Neither p nor P outside visual mode yank, so it's a non-issue.