sponsor Vim development Vim logo Vim Book Ad

code-snippet : Simple input assistance of code snippets

 script karma  Rating 33/14, Downloaded by 2191  Comments, bugs, improvements  Vim wiki

created by
Taku Miyakawa
 
script type
utility
 
description
FILE COMPOSITION:

plugin/code-snippet.vim : a script file.
doc/code-snippet.txt : an English help file.
doc/code-snippet.jax : a Japanese help file.

SUMMARY:

This plugin provides a simple input assistance of code snippets.  A code snippet is an idiom much used in source codes or markuped texts.  Here are the basic steps to use (and they are almost all).

        1. Register snippets (generally in filetype plugins).
        2. Type a trigger string in the insert mode.
        3. Press <S-CR> or <C-CR> to expand a snippet, and jump to each next/previous placeholder.
        4. Press <CR> to fill in a placeholder.

Snippets are registered as abbreviations.  You can therefore expand them by pressing <Esc> like mere abbreviations.

Placeholders can contain default strings.

This plugin is well configurable.  You can configure these points.

        * Map commands to other keys.
        * Use the select mode instead of the visual mode.
        * Change the format of placeholders in general or buffer-local.

EXAMPLE:

Type like this in the commandline mode.

        :StringAbbrLocal fputs() "fputs([~str~], [~fp:stderr~])[~~]"

Then type like this in the insert mode.

        fputs()

And press <S-CR>.

        fputs([~str~], [~fp:stderr~])[~~]

The line is expanded like above and "[~str~]" is selected in the visual mode.  "[~ ... ~]" is a placeholder.  Each time you press <S-CR>, the cursor jumps to the next placeholder.  Press <C-CR> to jump back to the previous placeholder.

When you press <CR>, the placeholder under the cursor is cut and Vim switches to the insert mode.  So that you can fill in the place.  Pressing <CR> at the second placeholder "[~fp:stderr~]", the default string "stderr" is inserted.  If you want to insert another word, it can be deleted by pressing <C-U> (see :help i_Ctrl-U).

The last empty placeholder "[~~]" is not selected by <S-CR>, but simply deleted so that you can insert a semicolon and so on.
 
install details
Extend the zip file to "%HOME\vimfiles\" (Windows) or "$HOME/.vim/" (*nix).  Then open "code-snippet.txt" with Vim under "%HOME\vimfiles\doc\" or "$HOME/.vim/doc/", and type ":helptags ." as an ex command to register help files.

To enable loading plugins and indent files for particular file types, it is recomended to add this line to your .vimrc file.

        filetype plugin indent on

It is not required for running this plugin, but necessary for registering snippets for particular file types.  And the setting makes it easy to register multi-line snippets. See ":help filetype".

On most terminal emulators, and some other systems, <S-CR> and <C-CR> of the default key mappings may not work.  In those conditions, You should therefore map the commands to other keys.  See ":help code-snippet-mappings".

It is same as installing other plugins.  See ":help add-plugin" and ":help add-local-help" for more information.
 

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
code-snippet-2007-12-04a.zip 071204a 2007-12-04 7.0 Taku Miyakawa * Rewrote the Japanese help file.
code-snippet-2007-12-04.zip 071204 2007-12-04 7.0 Taku Miyakawa * Corrected help files.  Placeholders were still written as "(_ ... _)".
* Sorry, changed again the default format of placeholders from "[| ... |]" to "[~ ... ~]".  The format of "[| ... |]" causes a trouble in help files.
code-snippet-2007-12-03.zip 071203 2007-12-02 7.0 Taku Miyakawa * Fixed a bug that "|" is not escaped to <Bar> in :StringAbbr...
* Changed the default format of placeholders from "(_ ... _)" to "[| ... |]".
code-snippet-2007-12-02.zip 071202 2007-12-02 7.0 Taku Miyakawa Initial upload
ip used for rating: 3.133.160.156

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