sponsor Vim development Vim logo Vim Book Ad

simpleSnip : Simple snippets utility.

 script karma  Rating 12/15, Downloaded by 3253  Comments, bugs, improvements  Vim wiki

created by
Alexandre Viau
 
script type
utility
 
description
See latest version on github: https://github.com/viaa/VimPlugins

Documentation
Name: simpleSnip.vim
Version: 1.5
Authors: Alexandre Viau with thanks to xaizek and Stephan Bittner for helping.

Description

This plugin allow to simply create and use snippets using vim abbreviations.
It allow to use placeholders in snippets and replace multiple placeholders
at once. Snippets may be used with any programming languages or for any kind of writing, not only programming.

Installation

Unpack the archive to your $VIM directory.  It should contain the plugin file
named simpleSnip.vim and several example snippet files:
- snipcs.vim (for C#)
- sniphtml.vim (for HTML)
- snipsql.vim (for SQL)
Then source unpacked *.vim files (open the file and do :so%) or restart vim.

Usage

<leader>rs (reload the snippets used for the current file. For example if current file is a .cs (csharp file) <leader>rs will reload the snippets used for this file. It will reload the file and the snippets file which is a filetype pluging will be reloaded at the same time.)
<leader>nc (to add a new snippet to a snippet file using the text in the clipboard (uses vim's auto-indenting)
<leader>NC (to add a new snippet to a snippet file using the text in the clipboard (uses dosen't use vim's auto-indenting)
<leader>ns (to add a new empty snippet to a snippet (uses vim's auto-indenting)
<leader>NS (to add a new empty snippet to a snippet file (uses dosen't use vim's auto-indenting)
<leader>gs (to temporarily load and use snippets from another filetype. Do <leader>rs to return to normal snippets)

Do <leader>rs (\rs by default) (rs for Reload Snippets) mapping to reload the snippets after
the snippets are modified. So for example if you are editing a .cs file and then you go to modify your snippets file
(which is a filetype plugin) then save the snippets file and then returning to the .cs file in
edition, reload the file to reload the snippets. To reload the snippets do <leader>rs (\rs)
or save the file and reload it using e %

See the snippets in the snippets file:

This usage example uses the CL snippet in the snipcs.vim:

To add a new class in c#, go in insert mode (i) and type CL then press the escape key. The snippet will be inserted.
Press 'n' or 'N' to move to from one placeholder to another in the snippet (note: aa is used as a prefix to search, you may change it if you don't like).
Then do the vim command 'cw' (change word) to replace the current placeholder by the text you type, press the escape key.
If the placeholder exists many times in the snippets, all occurences will be replaced after pressing the escape key.

This is an example of a snippet. Notice that it is simple a vim abbreviation
containing placeholders prefixed by 'aa' and at then end the function S() is
called. The number '2' in parameter to the S(2) function here means is the
number of placeholders in the snippet, here the placeholders are 'exe'
and 'ech'. You specify it so that the search will go back and the cursor
will be on the first placeholder when the snippet is inserted. Check the
snippets files for more example.

abb MA
\class exe<cr>
\{<cr>
\static void Main()<cr>
\{<cr>
\ech<cr>
\}<cr>
\}
\<esc>:cal S(2)<cr>

Be sure not to have blanks (spaces) after the <cr> in the snippets or the
lines may not be automatically indented. And be sure there is a space
after the snippet name (here MA ).

If you prefer to indent the lines of code yourself in the snippet, add the "\<c-o>:set paste<cr>" line as the second line of the snippets and indent your code.
Use tabs or space to indent your code. Here's an example.
      
iabb <buffer> CL
\<c-o>:set paste<cr>
\class from<cr>
\{<cr>
\   aaMemberType aaMemberName;
\   public from()<cr>
\   {<cr>
\       ech<cr>
\   }<cr>
\}
\<esc>:cal S(3)<cr>

It may be cases where vim will not indent like expected, like in the
preceding snippet CL, then in these cases you should indent the code
yourself using the "\<c-o>:set paste<cr>" line.

To add a new snippet copy to clipboard the text you want to put to snippet, and then do <leader>nc (n for new, c for clipboard)
(<leader> is "\" by default so \nc) then the text you copied to clipboard will be pasted to the file as a snippet compatible with
simpleSnip ready to be added or pasted directly to a snippets file.
If you want vim to automatically indent your snippet, delete the "\<c-o>:set paste<cr>" line that is pasted with the snippet and
be sure then that there are no spaces or tabs in your snippet text.

If you want to simply add a new snippet you may use the <leader>ns (n for new, s for snippet), this will
add a new empty snippet in a snippet file.

About automatic comments in snippets
I noticed that the filetype plugins like cs.vim or vim.vim have parameters
to automatically comment the following lines when pressing <enter> or o in
normal mode. Because of this, if you put comments in a snippets, all the
lines after the commented line will also be automatically commented. You may
disable it by going to the filetype plugin of the file type you don't want
these automatic comments, for example if you edit a c# file, go to
VIM$/ftplugin/cs.vim find the line:
setlocal fo-=t fo+=croql
and comment it out.
" setlocal fo-=t fo+=croql
For other filetypes, the syntax may differ but it uses the setlocal command
so find it or search for the word comment which may appear in the comments
of that command. I think for vim.vim the command to comment out is also:
" setlocal fo-=t fo+=croql

About snippets organization
If you like to organize the snippets with indenting like I did in the
C# snippet file, you'll have to comment out a line in the
VIM$/ftplugin/vim.vim file, if not the snippets will not keep their
indentation but will be automatically align to the left. Comment
out this line: " setlocal com=sO:\"\ -,mO:\"\ \ ,eO:\"\",:\"

To use snippets from other filetypes
The <leader>gs (get snippets) mapping allows to temporarily load snippets from another filetype to use them, then to come back to
the current file's filetype. Useful for example if one is using a c# file (with a cs filetype) then one need to insert a sql query but the
sql snippets are not defined in the cs_simplesnip.vim (cs filetype) but they are defined in the sql_simplesnip.vim (sql filetype) file.
Doing <leader>gs (\gs) will change the filetype (temporarily) of the current file making vim think it is a sql file, so the sql snippets
will be loaded, then the user would insert the snippets he needs, then would do <leader>rs to reload the snippets for the current file type,
so in this case the c# snippets would be reloaded. Another example would be to use c# snippets in a aspx file, or javascript snippets in html etc.

Tips

1. Type :ab to see the available snippets for this file.

2. If you should include several snippets that contain same placeholder
names, for example aaCommandName where aaCommandName is used in several
snippets, insert all the snippets before to change the aaCommandName by the
actual name, then once all the snippets are inserted, replace the
aaCommandName and it will be replaced in every snippets at once. So to
change it only one time.

Bugs

1. Il y a une erreur dans le replace all de simplesnip... voir et tester et corriger depuis les fold...avant c'etait ok
2. Le probleme avec simplesnip c'est peut-etre a cause d'une espace <space> qui essaie de folder/defolder..

History

Version 1.0

1. (Xaizek) for the let @/ = "aa" to simulate a search command like /aa but from code.
2. (Stephan Bittner) for the normal /aa
to simulate a search command like /aa but from code.

Version 1.1

1. (Xaizek) Changed filetype of all files from `dos` to `unix`.  The thing is
   that Vim-scripts with `dos` eol's work on Windows without issues, but
   produce plenty of errors while they are loaded on *nix systems.
   While Vim-scripts with `unix` eol's work on all platforms.

2. (Xaizek) Modified directory structure to look like this
   ftplugin
       cs_simplesnip.vim
       html_simplesnip.vim
       sql_simplesnip.vim
   plugin
       simpleSnip.vim

3. (Alexandre) Renamed snippets files to add an underscore in the filenames
   example cs_simplesnip.vim, so they will not conflict with files already
   there, but will still be loaded according to the filetype.

4. (Xaizek) Put `<buffer>` specifier to all :abb commands in the sample snippets,
   so they are defined locally to each buffer.

5. (Xaizek) Added `<silent>` specifier to the :imap command in simpleSnip.vim to
   prevent `:call ReplaceAll()` displaying on the statusbar when I leave insert mode.

6. (Xaizek) Change :abb to :iabb in the snippets files. Otherwise one will be surprised
   with results after typing name of one of snippets while being in the command-line mode.

Version 1.2

1. (Xaizek) Provided the paste/nopaste solution to be able to
   indent manually the snippets instead to have vim indent them manually.
   In some cases, vim did not indent the lines as expected, so his
   idea provides a way to manually indent the snippets.

Version 1.3

1. Added <leader>rr mapping to reload the snippets. So for example if you
   are editing a .cs file and then go to modify your snippets file (filetype
   plugin) then save the snippets file and then returning to the .cs file in
   edition, reload the file to reload the snippets. To do this do <leader>rs
   (\rs) (rs for Reload Snippets) or save the file and reload it using e %

version 1.4

1. Modified usage instructions (be sure not to have blanks (spaces) after
   <cr> in the snippets or the automatic indentation may not be like expected.

2. I added a tips section to the help (comment at the top of the file).

3. I started another snippet file for vimscript snippets.

4. Added the NewSnippetFromClipboard() function which allows to paste as a snippet (in the snippet format) to a snippet file, text copied to the * register (clipboard).
   So no need to manually add the snippets keywords or the "\", "<cr>" etc. The NewSnippetFromClipboard() function will take care of this only you have to copy to clipboard
   the text you want to put to snippet, and then do <leader>nc (n for new, c for clipboard) (<leader> is "\" by default so \nc) then the text you copied to
   clipboard will be pasted to the file as a snippet compatible with simpleSnip ready to be added or pasted directly to a snippets file.

5. Added the NewEmptySnippet() function which uses the NewSnippet() function to add a new snippet to a snippet file.  

6. Added the NewSnippet() function which is used by NewEmptySnippet() and NewSnippetFromClipboard() functions

7. Added several snippets for csharp, vimscript, html, javascript, aspx.

8. Added the S1() function which is like the S() function but a prefix may
   be passed in parameters. To use in case "aa" may match actual code in the
   snippet that is not part of placeholders. See the DataAdapter snippet in the csharp snippet file for example.

9. Modified the regex in the ReplaceAll() function and in this function, I
   added code to go to the next match after a placeholder is modified. Before
   the cursor was staying at the position of the last replacement thus one had
   to do several N to go back.

10. Added ability to temporarily load the snippets that are normally used for another filetype, for example
    to use the snippets in the sql_simplesnip.vim filetype plugin when editing a c# code file, or to load
    the c# snippets when editing an aspx file, or again, to load the javascript snippets when editing an
    html file. It is easy to load <leader>gs, then to come back to previous snippets <leader>rs.

Version 1.5

1. Added folds
2. Corrected a bug in the ReplaceAll() function, there was a space left over at line 9 which doing an error message.

Version 1.6

1. (Xaizek) When a placeholder to replace ended with the end of line ($) it was not replaced by the replace all function. This is corrected. Replaced let l:to = expand("<cword>") by let l:to = getreg('.')

 
install details
Unpack the archive to your $VIM directory.  It should contain the plugin file
named simpleSnip.vim and several example snippet files:
- snipcs.vim (for C#)
- sniphtml.vim (for HTML)
- snipsql.vim (for SQL)

Then source unpacked *.vim files (open the file and do :so%) or restart vim.
 

rate this script Life Changing Helpful Unfulfilling 
script versions (upload new version)

Click on the package to download.

package script version date Vim version user release notes
simpleSnip.vim 1.6 2013-01-02 7.0 Alexandre Viau When a placeholder to replace ended with the end of line ($) it was not replaced by the replace all function. This is corrected. Replaced let l:to = expand("<cword>") by let l:to = getreg('.')
simpleSnip.vim 1.5 2012-12-20 7.0 Alexandre Viau 1. Added folds
2. Corrected a bug in the ReplaceAll() function, there was a space left over at line 9 which doing an error message.
simpleSnip1.4.zip 1.4 2012-11-27 7.0 Alexandre Viau 1. Modified usage instructions (be sure not to have blanks (spaces) after
   <cr> in the snippets or the automatic indentation may not be like expected.

2. I added a tips section to the help (comment at the top of the file).

3. I started another snippet file for vimscript snippets.

4. Added the NewSnippetFromClipboard() function which allows to paste as a snippet (in the snippet format) to a snippet file, text copied to the * register (clipboard).
   So no need to manually add the snippets keywords or the "\", "<cr>" etc. The NewSnippetFromClipboard() function will take care of this only you have to copy to clipboard
   the text you want to put to snippet, and then do <leader>nc (n for new, c for clipboard) (<leader> is "\" by default so \nc) then the text you copied to
   clipboard will be pasted to the file as a snippet compatible with simpleSnip ready to be added or pasted directly to a snippets file.

5. Added the NewEmptySnippet() function which uses the NewSnippet() function to add a new snippet to a snippet file.  

6. Added the NewSnippet() function which is used by NewEmptySnippet() and NewSnippetFromClipboard() functions

7. Added several snippets for csharp, vimscript, html, javascript, aspx.

8. Added the S1() function which is like the S() function but a prefix may
   be passed in parameters. To use in case "aa" may match actual code in the
   snippet that is not part of placeholders. See the DataAdapter snippet in the csharp snippet file for example.

9. Modified the regex in the ReplaceAll() function and in this function, I
   added code to go to the next match after a placeholder is modified. Before
   the cursor was staying at the position of the last replacement thus one had
   to do several N to go back.

10. Added ability to temporarily load the snippets that are normally used for another filetype, for example
    to use the snippets in the sql_simplesnip.vim filetype plugin when editing a c# code file, or to load
    the c# snippets when editing an aspx file, or again, to load the javascript snippets when editing an
    html file. It is easy to load <leader>gs, then to come back to previous snippets <leader>rs.
simpleSnip1.3.zip 1.3 2012-11-22 7.0 Alexandre Viau 1. Added <leader>rr mapping to reload the snippets. So for example if you
   are editing a .cs file and then go to modify your snippets file (filetype
   plugin) then save the snippets file and then returning to the .cs file in
   edition, reload the file to reload the snippets. To do this do <leader>rs
   (\rs) (rs for Reload Snippets) or save the file and reload it using e %
simpleSnip1.2.zip 1.2 2012-11-21 7.0 Alexandre Viau 1. (Xaizek) Provided the paste/nopaste solution to be able to
indent manually the snippets instead to have vim indent them manually.
In some cases, vim did not indent the lines as expected, so his
idea provides a way to manually indent the snippets.
simpleSnip1.1.zip 1.1 2012-11-19 7.0 Alexandre Viau 1. (Xaizek) Changed filetype of all files from `dos` to `unix`.  The thing is
   that Vim-scripts with `dos` eol's work on Windows without issues, but
   produce plenty of errors while they are loaded on *nix systems.
   While Vim-scripts with `unix` eol's work on all platforms.

2. (Xaizek) Modified directory structure to look like this
   ftplugin
       cs_simplesnip.vim
       html_simplesnip.vim
       sql_simplesnip.vim
   plugin
       simpleSnip.vim

3. (Alexandre) Renamed snippets files to add an underscore in the filenames
   example cs_simplesnip.vim, so they will not conflict with files already
   there, but will still be loaded according to the filetype.

4. (Xaizek) Put `<buffer>` specifier to all :abb commands in the sample snippets,
   so they are defined locally to each buffer.

5. (Xaizek) Added `<silent>` specifier to the :imap command in simpleSnip.vim to
   prevent `:call ReplaceAll()` displaying on the statusbar when I leave insert mode.

6. (Xaizek) Change :abb to :iabb in the snippets files. Otherwise one will be surprised
   with results after typing name of one of snippets while being in the command-line mode.
simpleSnip.zip 1.0 2012-11-17 7.0 Alexandre Viau Initial upload
ip used for rating: 3.137.218.215

If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.
   
Vim at Github