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.
Related
I'm new to vim editing. I would like to create a script or an :ab command to smart replace using variables.
I thought to do it with an :ab command in my vimrc but i'm not sure how or if I can do it for variable.
For example, when im writing:
:<ab or something else> kuku v1 v2
where:
kuku is a shortcut,
v1 is a first variable,
v2 is a second variable.
And i would like the replacement to write for me:
for (i=v1, i<=v2; i++){
}
Any idea how I implement it? The for loop was an example for a text replacement but I will need it for few variations.
You can't really put logic in abbreviations so you can forget about :ab.
The only built-in thing that can take arguments like what you describe is a custom command, as described under :help user-commands, possibly backed by a custom function, as described under :help user-functions.
Here is a crude approximation:
function! ForSnippet(init, max)
put='for (i=' . a:init . ', i<=' . a:max . '; i++){'
put='}'
normal O
endfunction
command! -nargs=+ Kuku call ForSnippet(<f-args>)
That you would use like this:
:Kuku foo bar
Note that this is already quite involved for a crude solution and will require you to:
make a lot of refinement before you get something usable,
write and maintain lots of those things in the future.
Therefore, I would recommend you to not copy/paste the code above in your config and instead explore snippet expansion plugins like SnipMate or UltiSnips, which don't work exactly like you want but let you save quite a lot of typing.
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.
I was looking at the vimscript syntax file in the syntax directory, and under the keyword vimTodo were the words COMBAK, FIXME, TODO, and XXX. I can figure out what FIXME and TODO mean pretty easily, and I can guess what some might use XXX for, but I have no idea what COMBAK is for.
It must have a meaning of some sort, else there would be no reason to highlight it. I get that it's a code tag, but what does it mean? My best guess so far is an abbreviation for COMEBACK, though I doubt this.
Here is what I found so far:
Googling it got me nothing of use, and a google code search for COMBAK (with or without the quotes) got 0 results. I eventually Googled codetag "COMBAK" and found a single result, which uses it as a tag in a comment twice (a [ctrl+F] will find it): http://pastebin.com/H6mjbyBh.
The program is written in Vimscript, and contains both a vimscript syntax file and vimscript indent plugin file for lisp, along with some other massive functions.
Yes, it literally means "COME BACK".
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.
How can I insert a literal $1 in a snippet, using snipMate? I could not find that information in the manual…
Results in nothing (tries to find a placeholder):
snippet s
$1
Result in $ only:
snippet s
$$1
I found a workaround. Use $${0:1}. SnipMate doesn't interpret ${0}, but seems to insert the default text instead.
Basically, you can't. The closest workaround is using default text:
snippet s
$${1:1}
This requires you to hit tab once.
This is a listed issue with snipMate -- and has been for two years, so I wouldn't hold my breath waiting for it to get fixed.
I am currently on a promoting tour for UltiSnips on StackOverflow. UltiSnips support escaping chars, the corresponding snippet looks like this:
snippet s
\$1
endsnippet
A conversion script for snipMate snippets is shipped with UltiSnips, so switching is easy.