I am quite new to vim but I found it to be powerful tool (especially with plugins such as UltiSnips and YouCompleteMe). I quite like the idea of creating personal snippets in conjunction with visual token to perform quick text transformations. However I would like to be able to select some text in web browser, copy it to clipboard and simply type my_snippet in vim to expand my_snippet with text from clipboard as visual token. It would be quite useful but I have no idea how to arrange it. It would be great if somebody can instruct me how to set up vim to do this.
Asked
Active
Viewed 509 times
4
-
1I don't quite understand why you want to use visual tokens in a workflow without any visual selections. You could use a simple token in the snippet and then press `
+` to paste from the system clipboard when you're at that token, or I guess you could use python interpolation in the snippet to automate that. – OhleC Oct 01 '14 at 15:29 -
Hmm, I guess it would be the easiest to get content of clipboard automatically by using Python Interface to Vim directly from Python interpolation. What would be the proper way to do it ? Can anybody give code example ? - thank you in advance (I guess I need to learn more about all those vim buffers...) – user2183977 Oct 01 '14 at 19:33
-
I found that `vim.eval("@+")` would do what I want on my normal local machine. But when I try to do it using vim on server (by ssh) it gets nowhere (it's obvious that this vim does not have access to my local machine clipboard...). If there is no decent workaround then I would probably stay with manual paste... – user2183977 Oct 02 '14 at 07:40
-
Actually, now that you mention it, it's way simpler to just use vimscript interpolation. I don't have a solution for using the local clipboard over ssh, though. – OhleC Oct 02 '14 at 08:20
1 Answers
1
I will try to give you an example, an issue I had for some time. I am constantly trying out new plugins so, copying the github project name like:
psliwka/vim-smoothie
And transforming it on:
Plug 'psliwka/vim-smoothie'
Is something problematic, because along with typing the word Plug at the beggining of the line I have to type the single quotes and remove three spaces and one caridge return
psliwka /
vim-smoothie
So my snippet has to wrap it all in one go. After some research I ended up with this:
snippet plug "Insert a new plugin on my vimrc" w
Plug '${1:`!v substitute(@+, ' \|\n', '', 'g' )`}'
endsnippet
Some Explanation: The !v
part of the snippet means "vim interpolation", so I can use some vim commands. The substitute()
command rules are:
where-substitue, what-substitute, substitution, flags
| | | |
@+ ' \|\n' '' g
clipboard space or enter nothing globally

SergioAraujo
- 11,069
- 3
- 50
- 40