3

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
  • vim doesn't support snippets out of the box. If you want snippet functionality you will need to install a snippet package. Once you choose a snippet package, read its docs to see how to define your own snippets. Commented May 10, 2019 at 7:13
  • I had a similar question and solved like this: StackExchange Commented Jun 1, 2023 at 16:37

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks, do you know if I could disable installed plugin-snippets but mine still works?
I don't understand. You want to disable a plugin, but keep its functionality? That won't work.
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.