0

I would like to define a ultisnips snippet which would enter some text and then save the file. I checked the help and think most likely the vimscript execution would be the way to go.

I've tried to search how to do that, here's what I tried:

snippet trigger ""
some_text
`!v w`
`!v execute ":w"`
`!v :execute ":w"`
`!v :w`
endsnippet

But all of them give me:

File "~/.vim/bundle/ultisnips/pythonx/UltiSnips/snippet/definition/_base.py", line 442, in launch
    snippet_instance.update_textobjects()
File "~/.vim/bundle/ultisnips/pythonx/UltiSnips/text_objects/_snippet_instance.py", line 80, in update_textobjects
    if obj._update(done):
File "~/.vim/bundle/ultisnips/pythonx/UltiSnips/text_objects/_viml_code.py", line 20, in _update
    self.overwrite(_vim.eval(self._code))
File "~/.vim/bundle/ultisnips/pythonx/UltiSnips/_vim.py", line 121, in eval
    rv = vim.eval(as_vimencoding(text))
error: Vim:E121: Undefined variable: w

or similar. How to make vim execute simple save post-trigger?

sygi
  • 4,557
  • 2
  • 32
  • 54
  • 1
    why does it have to be a snippet? I think would better be done in a mapping, or a vimscript function – Doktor OSwaldo Sep 06 '17 at 11:07
  • 1
    or autocommand? – dlmeetei Sep 06 '17 at 11:13
  • 1
    I'd like it work work as a snippet, that is: I enter some text in insert mode, press a combination to expand, expand only when it's the first word in the line, see the suggestion list before expanding, etc. – sygi Sep 06 '17 at 11:55
  • I don't think this will work. You have to look at the code Ultisnippets uses to expand a snippet. I think it is not possible to have it interpreted as vim-commands. Maybe Ultisnippets have some sort of triggers you could use, but i doubt it. I would ask directly on the Github page, or investigate the code directly. – Doktor OSwaldo Sep 07 '17 at 12:34
  • The help tells it is possible to execute vimscript or any python code, so I think they are powerful enough. I tried investigating it on my own, as described above but I'm but overwhelmed by a number of things that's there. – sygi Sep 07 '17 at 16:23
  • To make things simpler, May be simply `set autowrite` in your vimrc and continue with snippet expansion – dlmeetei Sep 11 '17 at 11:55

0 Answers0