Inserting $1 in a snippet with snipMate - vim

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.

Related

How to insert boilerplate everytime a key is invoked in vim?

When I'm coding in C/C++, when I type '{' I generally want the next couple characters to be a newline, tab, newline, ended by '}'. This is especially convenient with top-down programming so that you can make the format of your function and continue on, only to come back later and it has already been prototyped for you.
So, to clarify, I want '{' to be replaced by
{\n\t\n}
Is that possible in vim, and if so, how?
is this ok for you?
inoremap { {<cr>}<esc>O<tab>
with this mapping, if you type foo{ in INSERT mode, it will change to:
foo {
I
}
I is cursor position.
You may be interested to try out some of the many "auto closing" plugins available. I use DelimitMate but you could try AutoClose or AutoPair.
You have a bunch of yank registers. Perhaps the simplest solution would be to yank that to a designated register and then put from that register each time you need it.
For that kind of "top down" coding with "auto prototyping", snippet plugins are excellent.
With a snippet plugin you can do much more extensive "top down" coding than just expanding brackets.
Ultisnips is a great snippet plugin. If you don't have python support in your vim, then SnipMate, the predecessor of Ultisnips, is quite capable also, but not as advanced.
With Ultisnips, you could use any of the three snippets below:
1) Create the brackets and place cursor following them
snippet {
{
}
$1
endsnippet
2) Create the brackets and place the cursor within them
snippet {
{
$1
}
endsnippet
3) Create the brackets and place the cursor within them, then Ctrl-j to place the cursor following the brackets
snippet {
{
$1
}
$2
endsnippet
But in addition to simple snippets like these three, you could get much more sophisticated, defining whole function/class/etc templates in snippets that can be quickly expanded and jumped through.

Vim snipMate tab jump

Vim snipMate doesn't work correctly
finf[Tab] generate
find(:first<+, :conditions => ['<+<+field+> = ?+>', <+true+>]+>)
but cursor set to end of the line and dont jump by placeholders with [Tab]
Update:
I try it with html - all works fine, maybe this doesn't work in *.rb files with Rails snipmates
I think there's something wrong with your snippet. I use snipMate for Python and all the placeholders looks like ${Number[:description]}. Take a look at ~/.vim/snippets/ for examples. Belows there is a simple example for a if statement in ruby :
snippet if
if ${1:condition}
${2}

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.

How do you reflect on the current sytax scope of the cursor?

I'm trying to create a bit of functionality that will only occur when the cursor is within a double quoted string in a ruby file.
How can I check if the cursor is within a given syntax like that?
This will return the current syntax group.
function! SyntaxItem()
return synIDattr(synID(line("."),col("."),1),"name")
endfunction
From: http://vim.wikia.com/wiki/Showing_syntax_highlight_group_in_statusline
Also See: http://vim.wikia.com/wiki/Identify_the_syntax_highlighting_group_used_at_the_cursor
Everything is built on top of the functions snippet Andy gave you. After that, we can build more advanced functions and commands.
That's what I've done with my map-tools plugin. Here are various examples of use:
for C
for C++
for VimL
for shell scripts
HTH.

How do I get Vim to automatically put ending braces?

While editing .scm files it would be great if Vim would automatically put the ending brace ) as soon as I start (. How do I do this?
You can map the opening brace to your liking:
:imap ( ()<left>
Try to use AutoClose plugin.
The simplest answer is to include a map. Eg.:
:inoremap ( ()<left>
The problem is that you would need to add one entry for each symbol you want automatically closed ('{','[','"',etc). Besides, plugins are normally more smart, providing things like detecting the "closing" character and not repeating it.
The problem with the solution above and most plugins, is that they tend break the undo sequence (gundo anyone?), as explained in:help ins-special-special.
Here is list of plugins that do what you ask (from vimtips):
delimitMate by Israel Chauca Fuentes (configurable, and doesn't break undo/redo/repeat, but - break iabbr) also on GitHub
AutoClose by Karl Guertin (auto-closes specific hard-coded characters, but doesn't break undo/redo/repeat)
AutoClose by Thiago Alves (configurable, but breaks undo/redo/repeat)
auto-pairs Auto Pairs by Miao Jiang (configurable, but breaks undo/redo/repeat)
ClosePairs by Edoardo Vacchi (configurable, but breaks undo/redo/repeat)
smartinput by Kana Natsuno (configurable, but breaks undo/redo/repeat)
Besides vimtips, there is another nice explanation of the issue on the web.
I needed one too, and I already tried a few of the plug-ins:
AutoClose, http://www.vim.org/scripts/script.php?script_id=1849, is a bit aggressive.
simple pairs, http://www.vim.org/scripts/script.php?script_id=2339, depends on Python. If you are on Linux it is not a problem, but on Windows it can be a trouble to match the Vim version to the Python interpreter you have.
My advice would be ClosePairs, that you can find at http://www.vim.org/scripts/script.php?script_id=2373 which has been working perfectly for me. It is simple and useful.
There are many tips and plugins on the subject. Have a look at the relevant entry in the vimtips site.
I'm presently using auto-pairs and it works quite very well.
The issues of the the plugin breaking undo/redo/repeat seem to persist among some of the plugins listed above but i don't think its much of an issue (well, at least not to me at the moment).
Just one caveat though, i wasn't able to use :helptags to generate the help file with this plugin as at the time of writing this.
Check out this new plugin: vim-autoclose by Townk. The previously mentioned AutoClose was to aggressive, sometimes behaving in an undesirable way.
There is a problem with using this (via the imap or one of the scripts). You won't be able to repeat the complete edit by using the . command.
e.g. (foo) with . only gets you foo, without the brackets.
It works fine if you insert the brackets normally, as two characters.
You can try downloading the following plugin
AutoClose : Inserts matching bracket, paren, brace or quote
https://github.com/vim-scripts/Auto-Pairs
Tested this plugin for undu redo. 2013 It just works. Also with python-mode plugin.
There is a new plugin by cohama:
lexima.vim (github)
(not yet on vim.org)
This plugin supports the .command!
Afaik, this is the only plugin supporting this.
Also the undo/redo sequence works.

Resources