Variables in snipMate definition, is it possible? (vim) - vim

I use vim for programming purposes and I use the snipMate utility. I'm aware of the basic snippets definition, but I'm trying to do something like the following (this doesn't work):
snippet ${1}_.
<$1 class="${2}">${3}</$1>
I think it would be easier to explain with an example. What I'm trying to do is to insert a html tag when typing a word followed by _. :
So if I type div_. and press tab, it should change to:
<div class="(position of cursor)">(position of cursor)</div>
If I type span_. and press tab, it should change to:
<span class="(position of cursor)">(position of cursor)</span>
And so on. Hope you get the idea. I'm aware that I can write a snippet for every case, but I'm trying to avoid that.
Thanks!

Make the snippet do the hard work for you:
snippet tag
<${1:div} class="${2}">${3}</$1>
You may also want to take a look at emmet-vim and surround.vim.

Related

Is there a way to get a snippet to work immediately after a word?

Whenever I try to use a snippet (using snipMate) after a word, without a space, it does not work. So I have to hit space, type my snippet, hit tab, and then eliminate the space. Is there a better way of doing this? Is there a way to get the snipppets to work even immediately after a word? Here is what I mean:
let us say my snippet is this:
snippet test
<some code>${1}</code>${2}
typical use:
hello test[TAB]
turns into this:
hello <some code>|</code>
but if I try this:
hellotest[TAB]
it turns into this:
hellotest_____
the _ being white space. Is there a way to fix this?
Vim abbreviations can be of three types (full-id, end-id, and non-id, cp. :help abbreviations), which help solve this problem. snipMate, however, allows all non-whitespace characters for snippet names, and therefore has to rely on whitespace for separation.
You have to modify the parsing of the snippet name, in plugin/snipMate.vim, it's in the function TriggerSnippet():
let word = matchstr(getline('.'), '\S\+\%'.col('.').'c')
There's no setting to that effect if that's what you ask. You will have to look at the source and do the change there yourself, I'm afraid.
Also, it can probably seen as a limitation but it's definetely not a bug so what you are after is an improvement, not a "fix". My advice, though, is to use it as it was designed: having triggers work even if they are part of another word makes no sense at all. Spaces are the most natural way of separating ideas and words.

Vim snipMate LaTeX template

I am using Vim to write my LaTeX files, and figured I'd make a few snippets to help me along. It's been no problem writing simple snippets (like one for begin, one for figure etc.), but then when I tried making one to set the title (with a default value including some curly brackets ({and }), I have this problem. The goal of my snippet is that it creates the following text:
\title{My name\\\texttt{me#email.com}}
I want all the text inside the outmost curly brackets (the ones belonging to title) to be a placeholder. I try to accomplish this by writing the snippet as follows:
\title{${1:My name\\\texttt{me#email.com}}}${2}
My problem, however, is that snipMate seems to use only the name and the email (omitting the closing bracket for the texttt command) as a placeholder.
SnipMate has some known problems with these things, the parsing for nested
braces just doesn't work. Maybe someone knows how to do this, but in the
meantime I suggest you creating two snippets, one for \title and another for
\texttt — just to simplify things a bit.
snippet \ti
\title{${1:My name}}
snippet \te
\texttt{${1:me#email.com}}

UltiSnips not triggering correctly

I'm using UltiSnips.
http://www.vim.org/scripts/script.php?script_id=2715
Everything works. Let's say when I type st<tab> it inserts submit_tag…. But if st<tab> is in between other characters, it won't work.
For example: foost<tab>bar
Anyway to fix this?
I am the author of Ultisnips. You might get faster feedback on our bug tracker for things like this: github.com/SirVer/ultisnips.
The feature you are looking for is called inword expansion. Use it like this:
snippet st "My cool st snippet" w
stollen
endsnippet
The 'w' will make sure your snippet is also expanded inside of other words.

Vim: quickly returning to a part of the text

I have been trying to learn Vim and have been using it for 2 weeks now.
My question is how do I return the cursor immediately to the middle of the text I just typed:
I have a tendency to type:
<div>
</div>
and returning back to the content of the tag and writing its contents:
<div>
text
</div>
This also goes for functions:
function eat() {
}
before getting back to the middle of the and typing it's contents:
function eat(){
blah
}
An uppercase O, so Shift+o, inserts an empty line above the one you're currently on and puts you into insert mode where you can begin typing. It was kind of an epiphany for me when I first figured that out.
If you work a lot with html / xml tags, have a look at surround.vim
I agree with michaelmichael, O works in both of your examples above.
In general, in vi or vim, you can use "macro" to achieve this. This feature acts like a bookmark, despite its name.
ma will define a macro called 'a'.
`a will take you back to where the bookmark was defined. If you want the beginning of the line, use 'a
So, if you typed 'ma' at the appropriate spot, continued typing, then typed '`a', it would achieve the effect you're looking for.
Snipmate plugin - Completion codes
dynamics
see an example of plugin in action at the vimeo site

What does ragtag plugin do

I am reading the doc still not sure what the plugin does.
https://github.com/tpope/vim-ragtag/raw/master/doc/ragtag.txt
Any further clarification will help.
Thank you.
The ragtag plugin only works with certain filetypes, which can cause some confusion, especially when editing an unsaved document.
*html*,wml,jsp,php,asp*,cf,mason,eruby,liquid,xml,xslt,xsd,docbk
It adds tags based on shortcuts. Its actually really useful for certain things.
For instance, in rails, you write the word foo (as in all the examples), press <C-X>+ and you get (^ is where your cursor ends):
<%= foo^ %>

Resources