Vim and snipMate (plugin) - adding new snippet won't work - vim

I am trying to create a new snippet to my snipMate plugin.
I work with some files called (i.e.) myfile.endfile
All .endfile files should have the same "snippet" like .html files.
So I did
cp html.snippet endfile.snippet
in my ~/.vim/snippets directory.
SnipMate is working with all present snippets, but not with my new created one.
Any suggestions for this problem?
(Btw: after creating the new .snippet file, I ran :helptags ~/.vim/doc command in an vim instance.)

It is because Snipmate works with filetype, which is a Vim option set when opening a file of a particular type.
For exemple, if you are opening, "index.html" the filetype is automatically set to html.
To see how it works, do :
:e $VIMRUNTIME/filetype.vim
As a preliminary test, you can :
1. open test.endfile
2. type :set ft=endfile or :set filetype=endfile
3. Check if your defined snippets now work
To do that automatically add the following in your .vimrc :
au BufNewFile,BufRead *.endfile set filetype=endfile
It means that every time you read or create a new file ending in endfilethe filetype option is set to endfile.
(The filetype is an arbitrary string it doesn't have to be identical to the file extension)

You can assign snippets without altering the filetype (which is desirable, because altering the filetype breaks syntax highlighting).
I believe the proper way to do this in the maintained fork of snipmate is to set g:snipMate.scope_aliases.
In your example, assuming you have an 'endfile.snippet' file, I believe adding the following to your .vimrc would work:
let g:snipMate = {}
let g:snipMate.scope_aliases = {}
let g:snipMate.scope_aliases['html'] = 'endfile'
If you want both html and endfile snippets to work for files of filetype='html', then use:
let g:snipMate = {}
let g:snipMate.scope_aliases = {}
let g:snipMate.scope_aliases['html'] = 'html,endfile'
I've added a pull request to snipmate to have their documentation updated. Edit: It has now been merged.

I found it convenient to use global snippets when using snippets that have uncommon name.endfile.
When you put your snippets in _.snippets file inside snippets folder they become global and are accessible in every filetype.
Maybe this is not directly answer to the question but a lot of users with similar problem can find this convenient. Specially if they don't have need to have everything organised in various files and are happy to have their own snippets in one file that is accessible everywhere.

Related

vim snippet default snippet file issue

I installed Vundle, got the snippetMate running. But when vim launched, the snippet it loaded is _.snippets. With my Understanding, it is the default for snippet. However, i want to use other snippet such as sh.snippets and tex.snippets. I try to run the SnippetMateOpenFile in Vim to locate the snippets file It doesn't appear. I checked and see that all of the snippet file is there.
Yes, the _.snippets contains the global snippets. In order to use the other ones, the correct filetype has to be set. The snippet plugin reuses the same mechanism that Vim uses for syntax highlighting and settings (like indent) that are specific to a certain programming language. Usually, filetype detection works automatically; you can check with
:verbose setlocal filetype?
This needs to print sh for shell scripts, to use sh.snippets. If it doesn't, you have to fix / enable filetype detection (see :help filetype-detect), or, for a one-time fix, set it manually:
:setf sh
(I'm not sure about your particular snippet plugin; I guess it's snipMate, but there are multiple variants around.)
I found out what happened. the snippets won't recognize the snippet files rightaway. So i saved and exit the text and reopen vim again. It works, yet seems like there must be a certain tag in order for vim to recognize the format of the file.

vim-snipmate not expanding non-source code file

I am using snipmate for coding and it works fine.
However I would also like to use it on txt file extension, but this does not work at all.
Is it designed to work like that? How can I get snippet expansion on ad-hoc file types?
*.txt files have the text filetype but you probably don't have snippets for that filetype. You can create them in ~/.vim/snippets/text.snippets and do the same for every filetype for which you don't have snippets.
Note that the snippets in ~/.vim/snippets/_.snippets are "global" and thus available in any filetype.
If you want to expand JavaScript snippets in an HTML file, you can "combine" filetypes with :set ft=html.javascript.

Auto fill the new file with default content - vi [duplicate]

This is really a newbie question - but basically, how do I enable a template for certain filetypes.
Basically, I just want the template to insert a header of sorts, that is with some functions that I find useful, and libraries loaded etc.
I interpret
:help template
the way that I should place this in my vimrc
au BufNewFile,BufRead ~/.vim/skeleton.R
Running a R script then shows that something could happen, but apparently does not:
--- Auto-Commands ---
This may be because a template consists of commands (and there are no such in skeleton.R) - and in this case I just want it to insert a text header (which skelton.R consist of).
Sorry if this question is mind boggeling stupid ;-/
The command that you've suggested is not going to work: what this will do is run no Vim command whenever you open ~/.vim/skeleton.R
A crude way of achieving what you want would be to use:
:au BufNewFile *.R r ~/.vim/skeleton.R
This will read (:r) your file whenever a new *.R file is created. You want to avoid having BufRead in the autocmd, or it will read the skeleton file into your working file every time you open the file!
There are many plugins that add a lot more control to this process. Being the author and therefore completely biased, I'd recommend this one, but there are plenty of others listed here.
Shameless plug:
They all work in a relatively similar way, but to explain my script:
You install the plugin as described on the linked page and then create some templates in ~/.vim/templates. These templates should have the same extension as the 'target' file, so if it's a template for .R files, call it something like skeleton.R. In your .vimrc, add something like this:
let g:file_template_default = {}
let g:file_template_default['R'] = 'skeleton'
Then create your new .R file (with a filename, so save it if it's new) and enter:
:LoadFileTemplate
You can also skip the .vimrc editing and just do:
:LoadFileTemplate skeleton
See the website for more details.
Assume that your skeletons are in your ~/.vim/templates/ directory, you can put this
snippet in your vimrc file.
augroup templates
au!
" read in templates files
autocmd BufNewFile *.* silent! execute '0r ~/.vim/templates/skeleton.'.expand("<afile>:e")
augroup END
Some explanation,
BufNewFile . = each time we edit a new file
silent! execute = execute silently, no error messages if failed
0r = read file and insert content at top (0) in the new file
expand(":e") = get extension of current filename
see also http://vim.wikia.com/wiki/Use_eval_to_create_dynamic_templates
*fixed missing dot in file path
Create a templates subdirectory in your ~/.vim folder
$ mkdir -p ~/.vim/templates
Create a new file in subdirectory called R.skeleton and put in the header and/or other stuff you want to automagically load upon creating a new ".R " file.
$ vim ~/.vim/templates/R.skeleton
Then, add the following to your ~/.vimrc file, which may have been suggested in a way by "guest"
autocmd BufNewFile * silent! 0r $HOME/.vim/templates/%:e.skeleton
Have a look at my github repository for some more details and other options.
It's just a trick I used to use .
It's cheap but If you ain't know nothing about vim and it's commands it's easy to handle.
make a directory like this :
~/.vim/templates/barney.cpp
and as you konw barney.cpp should be your template code .
then add a function like ForUncleBarney() to end of your .vimrc file located in ~/.vimrc
it should be like
function ForBarneyStinson()
:read ~/.vim/templates/barney.cpp
endfunction
then just use this command in vim
:call ForBarneyStinson()
then you see your template
as an example I already have two templates for .cpp files
:call ForBarney()
:call ACM()
sorry said too much,
Coding's awesome ! :)
Also take a look at https://github.com/aperezdc/vim-template.git.
I use it and have contributed some patches to it and would argue its relatively full featured.
What about using the snipmate plugin? See here
There exist many template-file expanders -- you'll also find there explanations on how to implement a rudimentary template-file expander.
For my part, I'm maintaining the fork of muTemplate. For a simple start, just drop a {ft}.template file into {rtp}/template/. If you want to use any (viml) variable or expression, just do. You can even put vim code (and now even functions) into the template-file if you wish. Several smart decisions are already implemented for C++ and vim files.

Executing vim command based on filetype?

I'm trying to have the command
let b:match_words='<:>,<\#<=\([^/][^ \t>]*\)[^>]*\%(>\|$\):<\#<=/\1>'
run every time I open an html file. I tried putting the line
autocmd FileType html let b:match_words='<:>,<\#<=\([^/][^ \t>]*\)[^>]*\%(>\|$\):<\#<=/\1>'
in a file named html.vim in both my ftdetect and ftplugin folders and nothing happened. How do I have the command run everytime I'mm in an html file?
The command is to change the matching behavior of matchit btw.
In general, your autocmd is alright; the problem is that you're trying to redefine the b:match_words definition done in $VIMRUNTIME/ftplugin/html.vim, so the execution order becomes important.
The place for these customizations is in the after directory, i.e. ~/.vim/after/ftplugin/html.vim; just create a new file and put the :let command in there.
You can observe the sequence of sourced scripts via :scriptnames. In other cases, when you're not overriding default behavior, the :autocmd FileType is alright, but I prefer putting these (e.g. custom mappings) into ~/.vim/ftplugin/html_mymappings.vim, as it provides better separation and helps keeping your .vimrc short and understandable.
The ftdetect subdirectory is for filetype detection, i.e. inspecting file path / name / contents to determine the correct filetype. It doesn't apply here, as the filetype is html.

How can I disable the vim plugim snipMate to some kind of files?

My questions is self-explanatory, How can I disable vim snipmate to perl files for example?? I found myself frustating because its stuck with my omni-completion and neocomplcache. I just use snipMate in html files.
This same question could be asked like: How can I enable snipmate for just one type of file (html)?
You could move the snipMate.vim file from $VIM/plugin to another directory that is not autoloaded, like $VIM/extra.
Then in your .vimrc, add a line to use snipmate for HTML only:
autocmd FileType html,xhtml source $VIM/extra/snipMate.vim
Another uglier method would be to simply remove or rename all the snippets files in $VIM/snippets except for html.snippets, or a subset of those you would like to keep. This would be more effective though if you only wanted to remove some (like only disabling perl, for example) than to disable all but one type.

Resources