Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create Vim snippets without installing any plugins?

I don't like using snippets created by other people (because I tend to forget the syntax or basic things when I did so). However, I couldn't figure out how to write my own from scratch in Vim

I have created my snippets from scratch in Atom, Sublime and now I'm moving to Vim but couldn't find the exact guide in how to create from-scratch snippets.

I googled around and people kept mentioning different snippet plugins, such as UtilSnips (but I don't want to install any snippets plugins for now).

Can anyone help me? I just need to know where to put my snippet files, snippet syntax for each file extension and I'll be good. Thanks.


2 Answers

Vim has no support for snippets out of the box. If you don't want to install any plugins, you'll have to write the functionality yourself. The syntax etc. is up to you, then.

There is one low-tech alternative to snippets built into Vim: Abbreviations. Personally, I defined an abbreviation for a common Python snippet:

ia inim if __name__ == "__main__":

After putting this line in my .vimrc, I can type inim in insert mode, followed by whitespace (in my case usually <cr>, to enter the code. This approach is rather limited though.

like image 155
L3viathan Avatar answered Mar 01 '26 11:03

L3viathan


You want to install a snippet plugin, and use only snippets created by your self, right?

Install ultisnips, put all your snippets in ~/.vim/UltiSnips. Add following setting to your vimrc:

let g:UltiSnipsSnippetDirectories = [$HOME.'/.vim/UltiSnips']

If g:UltiSnipsSnippetDirectories contains only one entry that's is an absolute path, it will use it as the only source of snippets.

check :h UltiSnips-how-snippets-are-loaded

You can use :UltiSnipsEdit to edit snippets for current file type.

Check my answer for another question if you want to know further detail.

like image 37
dedowsdi Avatar answered Mar 01 '26 11:03

dedowsdi