sponsor Vim development Vim logo Vim Book Ad

SnippetySnip : Inserts named snippets from various other files into a file

 script karma  Rating 0/2, Downloaded by 713    Comments, bugs, improvements  Vim wiki

created by
Anders Schau Knatten
 
script type
utility
 
description
SnippetySnip is a vim tool for automatically inserting parts (snippets) of other files in a file. I wrote it to insert sourcecode examples from source files into html blog-posts, but it can be used for all sorts of text files.

In the source file, you mark up a region of text (a snippet) and give it a name. In the target file, you mark where you want this particular snippet inserted.

For instance:

source.py:
    (...)
    #snippetysnip_begin:foo
    def foo():
        return 0
    #snippetysnip_end
    (...)

target.html:
    <p><code>
    <!-- snippetysnip:/path/to/source.py:foo -->
    </code></p>

When SnippetySnip is run, the function foo() from source.py is inserted into the html file, which then looks like this:

target.html:
    <p><code>
    <!-- snippetysnip:/path/to/source.py:foo -->
    def foo():
        return 0
    <!-- snippetysnip_end:/path/to/source.py:foo -->
    </code></p>


SnippetySnip does not care about the format of the comments in your particular language, it only looks for the snippetysnip-strings. A snippet name can consist of letters, numbers or any of the characters "-_.".

You can have many snippets in a source file, and a target file can reference multiple snippets from multiple sources.

Paths can be relative or absolute.



HOW TO USE
In the source files, surround each snippet with snippetysnip_begin:SNIPPET_NAME and snippetysnip_end. These tags need to be on separate lines, but except from that, they can be inside the comment style of your choice, depending on the host language. See the example above.
In the target file, put on a separate line snippetysnipp:FILE_NAME:SNIPPET_NAME
Paths can be relative or absolute. See the example above, or python/SnippetySnip/integration_tests/ for a more extensive one.
To run SnippetySnip and insert all your snippets, do ":call SnippetySnip()" without the quotes. It is suggested you make a map to make this easier, see INSTALLATION
It can be tedious to type the snippet inclusion strings in the target file. To get this string automatically, go to your source file, place the cursor inside the snippet you want the inclusion string for and do ":call SnippetySnipPrintCurrentSnippetString()" without the quotes. It will print the string for you (with html comments). It is suggested you make a map to make this easier, see INSTALLATION.
About cursor position: Since the entire buffer is replaced, it is impossible to exactly remember the cursor position. SnippetySnip does however make a best effort, and puts the cursor back at the same line and column.



INSTALLATION
Download SnippetySnip-<version>.tgz into your vim directory (usually ~/.vim on Linux systems) and do
tar zxf SnippetySnip-<version>.tgz

To make SnippetySnip easier to use, I suggest you set up a mapping in ~/.vimrc. These should do nicely:
map <Leader>s :call SnippetySnip()<CR> "Type <Leader>s to run SnippetySnip
map <C-s> :call SnippetySnip()<CR> "Press Ctrl+s to run SnippetySnip
map <Leader>S :call SnippetySnipPrintCurrentSnippetString()<CR> "Type <Leader>S to print the name of the current snippet



ADVANCED USAGE
It is possible to give arguments before="..." and/or after="..." when including a snippet. These arguments will be inserted between the snippet command and the actual snippet. Here is an example, inserting code snippets into Wordpress posts:

original target:
    <!-- snippetysnip:source.cpp:foo:(before='[sourcecode language="cpp"]', after='[/sourcecode]') -->

modified target:
    <!-- snippetysnip:source.cpp:foo:(before='[sourcecode language="cpp"]', after='[/sourcecode]') -->
    [sourcecode language="cpp"]
    imported();
    code();
    here();
    [/sourcecode]
    <!-- snippetysnip_end:source.cpp:foo -->

(This was motivated by the fact that html-comments do not work well inside [sourcecode] blocks on Wordpress)

If you want SnippetySnipPrintCurrentSnippetString() to include arguments, define g:SnippetySnipArguments. For instance, you can put this line in ~/.vimrc:
let g:SnippetySnipArguments = "(before='[sourcecode language=\"cpp\"]', after='}[/sourcecode]')"

SnippetySnip is also available on github: https://github.com/knatten/SnippetySnip
 
install details
INSTALLATION
Download SnippetySnip-<version>.tgz into your vim directory (usually ~/.vim on Linux systems) and do
tar zxf SnippetySnip-<version>.tgz

To make SnippetySnip easier to use, I suggest you set up a mapping in ~/.vimrc. These should do nicely:
map <Leader>s :call SnippetySnip()<CR> "Type <Leader>s to run SnippetySnip
map <C-s> :call SnippetySnip()<CR> "Press Ctrl+s to run SnippetySnip
map <Leader>S :call SnippetySnipPrintCurrentSnippetString()<CR> "Type <Leader>S to print the name of the current snippet
 

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
SnippetySnip-1.0.tgz 1.0 2013-10-13 7.0 Anders Schau Knatten SnippetySnipPrintCurrentSnippetString() now prints the full path to the current file.

SnippetySnip has been stable for a long while now, so bumped the version to 1.0.
SnippetySnip-0.6.tgz 0.6 2012-10-17 7.0 Anders Schau Knatten Fixed bug that would match a snippet named "fooMoreChars" when searching for snippet "foo"
Snippet names are now restricted to the letters a-z/A-Z, numbers, or any of the characters "-_." (or if you prefer a regex: /[0-9a-zA-Z\-_\.]+/)
SnippetySnip-0.5.tgz 0.5 2012-06-19 7.0 Anders Schau Knatten Add SnippetySnipPrintCurrentSnippetString() to print the inclusion string for the snippet currently under the cursor
Make a best effort to preserve cursor position
SnippetySnip-0.4.tgz 0.4 2012-05-01 7.0 Anders Schau Knatten Put a line of whitespace at the beginning and end of each snippet for improved readability
SnippetySnip-0.3.tgz 0.3 2011-10-26 7.0 Anders Schau Knatten It is now possible to give arguments before="..." and/or after="..." when including a snippet. These arguments will be inserted between the snippet command and the actual snippet.
SnippetySnip-0.2.tgz 0.2 2011-08-31 7.0 Anders Schau Knatten Improved README with suggestions for keymaps
SnippetySnip-0.1.tgz 0.1 2011-08-29 7.0 Anders Schau Knatten Initial upload
ip used for rating: 54.161.219.112

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.
   
SourceForge.net Logo