3

Is there a way to expand a UltiSnips snippet while in vim command-line mode? I can only expand a snippet in insert mode.

I am using UltiSnips to help inserting unicode characters. And I need to write these characters in the command-line for searching. I do not want to insert them using <C-v>u. I also prefer not to use digraphs.

1
  • Digraphs are made exactly for this purpose (IMO using snippets is a bit overkill to insert a single character). Why don't you want to use them?
    – statox
    Commented Sep 13, 2015 at 16:39

3 Answers 3

1

While I didn't achieve to trigger the completion from the command-line mode you can work this like this :

Prepare your command inside insert mode (or at least the part you cannot create from command-line mode). Yank it and insert it inside your command using <C-r> and the " register like so :

Insert Mode

tec<Tab>yiw

Commande-line Mode

:/<C-r>"

In command-line mode, there are only a set of word you can expand, for example : Command names, Tags, File names, ...

See : http://vimdoc.sourceforge.net/htmldoc/cmdline.html#cmdline-completion

2
  • I don't know how to use your answer, can you explain it further, please? E.g. I have a snippet ec that expands to ê and I use <Tab> key to expand snippets. In insert mode, I type tec<Tab> and get . In command-line mode to search, I type /tec<Tab> but get nothing. How do I use this :call UltiSnips#ExpandSnippetOrJump()? Commented Aug 14, 2015 at 14:15
  • Oh ok I didn't understand your question, let me update my answer :)
    – nobe4
    Commented Aug 14, 2015 at 14:20
1

The mapping

cnoremap {lhs} <C-r>=&cedit<CR>:call UltiSnips#ExpandSnippet()<CR><C-c>

maps {lhs} to expand a (simple) snippet in the command line.

Here's what the {rhs} of the mapping does:

<C-r>=&cedit<CR>                                         switch to the command line window (Typically evaluates to <C-f>)
                :call UltiSnips#ExpandSnippet()<CR>      expand the snippet
                                                   <C-c> switch back to the command line

An UltiSnips snippet cannot be expanded in the command line. However, the command line window, being a "standard" buffer, allows snippets to be expanded within.

For simple snippets with no placeholder texts, this mapping works as intended.


However, if a snippet contains multiple tabstops, the user is unable to jump beyond the first tabstop. Also, if a tabstop contains placeholder text, it is not overwritten upon further typing. One possible explanation is that after <C-c> is pressed, the command line window is "forgotten", and along with it, the tabstops' locations.

A workaround is to remain in the command line window, instead of going back to the command line. (ie., remove <C-c> in the mapping above). Then, placeholder texts are correctly overwritten upon typing, and the user can jump between tabstops as desired.

0

I use noreabbrev to insert frequently used unicodes, for example:

noreabbrev vvref ※
noreabbrev vvsec §

Abbreviations are triggered automatically on non-keyword character or CTRL+]. It works both in Insert Mode and Command Mode. Or use cnoreabbrev and inoreabbrev to define Command Mode and Insert Mode only abbreviations. It also can be used to define command aliases.

More information can be found in :help abbreviations.

1
  • 1
    Welcome to Vi and Vim! Your answer was flagged for review since it's too short. I do think it could be improved. Could you explain how you use noreabbrev to insert frequently used Unicode characters? Perhaps some examples of the commands you personally use? That would surely improve this answer... Thanks!
    – filbranden
    Commented Jun 1, 2020 at 14:58

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.