vim-vsnip icon indicating copy to clipboard operation
vim-vsnip copied to clipboard

change registery when replacing placeholder

Open kkharji opened this issue 4 years ago • 8 comments

hey @hrsh7th, is there a way to change the registry when replacing the placeholder. I was so annoyed for sometime now that I'd need to skip replacing the placeholders because it means I will lose what I have in my main registry .

Thanks

kkharji avatar Mar 15 '21 03:03 kkharji

You should describe the example. It is hard to understand.

Shougo avatar Mar 15 '21 07:03 Shougo

Sorry about that. For example: given the following snippet:

    "body": [
      "```${1:lang}  ",
      "$0",
      "```"
    ]

When expanding it the cursor goes to lang placeholder, when I change it, my main vim registry gets replace with lang. And that basically my main issue

kkharji avatar Mar 15 '21 07:03 kkharji

The example is better, but I don't understand why the feature fixes your problem. Please compare with current behavior and your desired behavior.

Shougo avatar Mar 15 '21 07:03 Shougo

Okay, Say I have this in my main registry +:

type KeyProduct struct{}

When I write go to replace lang, + registry get replaced with lang. so when a tab over to the body and try to paste, I get lang.

The desired behavior is that when I replace lang, the main + or * registry does not get modified or overwritten with the placeholder.

kkharji avatar Mar 15 '21 08:03 kkharji

Ah I get it. This is the feature of replace. It is hard to fix...

Shougo avatar Mar 15 '21 08:03 Shougo

And the "registory" should be "register" in Vim word. I was confused.

Shougo avatar Mar 15 '21 08:03 Shougo

I'm having the same issue. I think we can have two possible solutions:

  1. The easiest: Use vim-cutlass/cutlass.vim or copy/call this function:
    function! cutlass#overrideSelectBindings()
      let i = 33
    
      " Add a map for every printable character to copy to black hole register
      " I see no easier way to do this
      while i <= 126
          if i !=# 124
              let char = nr2char(i)
              if i ==# 92
                let char = '\\'
              endif
              exec 'snoremap '. char .' <c-o>"_c'. char
          endif
    
          let i = i + 1
      endwhile
    
      snoremap <bs> <c-o>"_c
      snoremap <space> <c-o>"_c<space>
      snoremap \| <c-o>"_c|
    endfunction
    
  2. Maybe vim-vsnip can implement some autocommands so we can run our custom hooks, like ultisnips. Then we can change the clipboard before and after the expansion, for example:
    autocmd User VimVsnipEnterFirstSnippet set clipboard-=unnamedplus
    autocmd User VimVsnipExitLastSnippet set clipboard+=unnamedplus
    

I had used solution 1, It's a bit hackish (clever?) but got some problems with other plugins and it polluted the mappings, so I'm evaluating other solutions.

Personally, I would like 2 (is more flexible and powerful). But I'll understand if it's not something the maintainers would like to add to the codebase.

EDIT: A third option, but not optimal: add an explicit mapping to delete the text and send it to the blackhole register before writing the replace text:

snoremap <silent> <c-h> <c-g>"_c

So when you have lang selected, press <C-h> before writing go.

davidsierradz avatar Sep 16 '21 21:09 davidsierradz

Maybe vim-vsnip can implement some autocommands so we can run our custom hooks, like ultisnips. Then we can change the clipboard before and after the expansion, for example:

autocmd User VimVsnipEnterFirstSnippet set clipboard-=unnamedplus
autocmd User VimVsnipExitLastSnippet set clipboard+=unnamedplus

Interesting, this looks clean and simple, @hrsh7th any chances this can be implemented?

For now I will try to adjust to snoremap <silent> <c-h> <c-g>"_c

Thanks @davidsierradz

kkharji avatar Oct 13 '21 22:10 kkharji