Automatically inserts parts of files into another file. Example: Include source code snippets in an article.
Fetching latest commit…
Cannot retrieve the latest commit at this time
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