# Tag Info

## Hot answers tagged snippets

31

I've been using ultisnips for several weeks now. I think the main advantages of this plugin are the following: It is pretty fast even with a great number of snippets available. The basic syntax to define a new snippet is easy to understand, thus it is easy to quickly create a new snippet doing what you want to do. (For more complex snippets some additional ...

22

I've been using the original SnipMate since I started using Vim. It doesn't have external dependencies. It uses a very simple syntax. It is very easy to set up. It has been abandoned since 2009. I have nothing to complain about.

13

SnipMate and UltiSnips are the two most popular snippet engine for Vim. Both are inspired from TextMate's snippet syntax. UltiSnips can run all SnipMate snippets but also have additional syntax to make it more powerful. A good rule of thumb is that if your Vim has python support, then use UltiSnips. If not, then use SnipMate. UltiSnips dropped support for ...

12

Here is a list of features from mu-template. Discl.: I'm its maintainer. Template-files can be expanded: automatically when opening a new buffer (unless deactivated from the .vimrc), explicitly through menus or the command line, from the INSERT-mode in a snippet-like fashion ; from the VISUAL-mode to surround the selection with a snippet -- the surrounding ...

8

Honza's UltiSnips Snippet gentbl<number>x<number> available in the repository https://github.com/honza/vim-snippets/blob/master/UltiSnips/tex.snippets Using this snippet which uses the python interpolation feature of UltiSnips, you can enter e.g. gentbl5x3<tab> which will expand to \begin{tabular}{||||} & & \\ & & ...

6

readtags universal-ctags provide readtags to filter tags: Read all members of a struct: readtags -Q '(and (eq? $kind "member") (eq?$scope-name "struct_name") )' -l (and (...) (...)): (...) and (...) (eq? $kind "member") restrict kind to member (eq?$scope-name "struct_name") restrict scope-name to struct_name, your tag fields must include Z for this to ...

5

:UltiSnipsEdit will automatically open ultisnips for current filetype. From there you can see that it's supposed to be python.snippets.

5

You could probably implement this as a snippet by leveraging Ultisnips' Interpolation feature (VimCasts demo). As an alternative, I whipped up a simplistic function to generate a matrix template on the current line per your sample: function! CreateMatrix(rows, ...) abort let cols = a:0 ? a:1 : 3 let matrix = ['\begin{bmatrix}'] call extend(matrix, ...

5

You can manually fix the indent by typing CTRL-F (this is documented at :help indentkeys-format). Thus, an autonomous solution would be to issue this key-press after snippet expansion. global !p def fix_indent(snip): vim.eval('feedkeys("\<c-f>")') endglobal post_expand "fix_indent(snip)" snippet it "Individual item" b \item $0 endsnippet 5 You have to use the i option. This is in the help page: i In-word expansion - By default a snippet is expanded only if the tab trigger is the first word on the line or is preceded by one or more whitespace characters. A snippet with this option is expanded regardless of the preceding character. In other words, the snippet can ... 4 When you register it this way, your new autocommand will be triggered after all other autocommands registered on the same event, in particular after the one that sources your ftplugin -- I suppose you define the mapping in an ftplugin (*). You could change that, probably by changing the order between this line and the :filetype xxxx in your .vimrc, but this ... 4 I came across a solution that suits me well, it may be useful for you also. I add some mappings/abbreviations in insert mode starting with \ (or any character you like, and use not all the time): " use an abbreviation (css debugging) iabbrev \r *{color: red !important;} " use a mapping (html doctype insertion) inoremap \doctype <!DOCTYPE map PUBLIC "-//... 4 As multiple people have said in various comments, check out :h skeleton. This sounds like just what your looking for. However if you want a little more customizability you can try the following... Detecting when a file is "created" might be hard, since vim creates files by writing buffers to files. But you can detect when you write to a file with the ... 3 Change the entire format to Google's... It's not documented, but the Python snippets file from vim-snippets does allow you to specify docstring style. Simply add let g:ultisnips_python_style = 'google' to your configuration, and the snippet will behave accordingly. When adding exception in a forward trigger, the docstring will add a "Raises" section (... 3 TL;DR: Check out :help abbreviations. Example: Let's say I wanted to make a snippet to quickly create a JavaScript object in the following format with the abbreviation "obj" (| represents the cursor position after doing the abbreviation): var foo = {|}; I could do: iabbrev obj var foo = {};<Left><Left> Then, whenever I type obj in ... 3 All you need to do is to insert a \ before the <tab> Now the code will look like this instead: execute "normal! o\<tab>" 3 The inoremap <expr><TAB> ... also has a <Plug>-mapping on the right-hand side of the mapping. These mappings only get expanded when you use :imap. So, though you should normally use :noremap, this is one exceptional case where you need to allow recursive mappings. 2 Discl. I'm maintaining an alternative to c.vim. As such, I don't use it and I don't know its intricacies. If you've installed c.vim into ~/.vim/cvim and if by % vim ~/.vim/cvim/c-support/templates/Templates you are editing the original files shipped with the plugin, it's probably a bad idea. Indeed, next time you'll update the plugin, you'll lose all your ... 2 It's because you indent the snippet options ('keywords'), by not following the help example (:h neosnippet-snippet-syntax). So, you need: snippet stdlib options head alias std #include<stdlib.h>${0} BTW, I'm not sure if ${0} is needed at all, in this example. 2 The answer is synchronized placeholders; I just couldn't find anything online/in the manual because I didn't know the terminology. The correct snippet would be: snippet thing abbr thing #ifndef THING_${1:something}_H #define THING_$1_H #include <something.h> BEGIN_DECLS // ... END_DECLS #endif /* THING_$1_H */ For ...

2

UltiSnips uses a strategy similar to how Vim detects :h ftplugins. That is, it will look for the filenames that match the following pattern in your UltiSnips directory: ft.snippets, ft_*.snippets, or ft/*, where ft is replaced with your Vim filetype (python, c, etc.) and * matches any string including the empty string. A table from :h UltiSnips-how-snippets-...

2

You could use abbreviations as we used to do 2 decades ago, but I wouldn't recommend it for multilines snippets as the ones you've shown. Of course it's possible, to use abbreviations. As we can do with abbreviations almost everything we can do with snippet plugins -- on the details that you'll want to eat away the extra space you'll be inserting with the ...

2

Hey not a long time ago I was also looking to have the same setup as yours and ended up with this config. Although I ended up using <c-j> and <c-k> for navigating the completion list and also for snippet jumps. let g:coc_snippet_next = '' let g:coc_snippet_prev = '' inoremap <expr> <c-j> \ pumvisible() ? "\<c-n>" : ...

2

global !p def insert_before_unique(name): vim.command('/unique_text') snip.expand_anon(name + '\n\n\n') endglobal post_jump "if snip.tabstop == 0: insert_before_unique(snip.tabstops[1].current_text)" snippet trigger "tricky stuff" $1 endsnippet This seems to do the trick. Or close to it. If you type trigger and hit tab you'll be ... 2 I got it to work: global !p def mycontext(char): x = vim.current.window.buffer y = vim.current.window.cursor[0] z = vim.current.window.cursor[1] try: return x[y-1][z-1] + x[y-1][z] != char except: return True endglobal context "mycontext('()')" snippet ( &... 1 To get a single character to use as an "argument" in a mapping, you can use the getchar() function, which waits for one. You should then use nr2char() to convert it to a string. If you want to use the character more than once (as it's your case here), you should store it in a variable. It's easier if you use a function to do so. Then use an <expr> ... 1 I think you're mixing up two different and not directly related kinds of snippets here... Vim snippets are templates you can use while editing text and are meant to help you fill up a lot of the boilerplate you need in many programming languages. For instance, you can use snippets to quickly insert skeletons of classes or functions, a template for a main ... 1 At this time, I have something extremely similar in my lh-cpp plugin for C++. Within a class context, I type :Constructor init, and my plugin will fetch all the member data (thanks to the API of two other plugins of mine: lh-dev + lh-tags) and generate the constructor. We aren't far from what you wish to accomplish. The first step will be to extract the ... 1 UltiSnips#SnippetsInCurrentScope return all snippets whose trigger matches the current word, I think the match here means =~, not ==. You need to check word before cursor and the triggers by yourself: function! s:is_ultisnips_expandable() " get word before cursor let word = matchstr(getline('.'), printf('\v\w*%%%dc\w', col('.') - 1)) if(empty(word)) ... 1 Try this: snippet guard "add guard to functions" b if !exists('*!p snip.rv = re.search('\S+\s+(\S+)\(', snip.v.text.splitlines()[0]).group(1)')${VISUAL} endif \${0:jump here <C-j>} endsnippet The !p snip.rv = ... part is a python interpolation. The evaluation of the expression to the right of the assignment operator after snip.rv replaces ...

Only top voted, non community-wiki answers of a minimum length are eligible