Using the VIM JavaScript syntax file - vim

I tried including the following syntax file into my vim.
I installed this plugin using Pathogen as per instruction:
git clone https://github.com/jelera/vim-javascript-syntax.git ~/.vim/bundle/vim-javascript-syntax
Then included the following in my .virc file
au FileType javascript call JavaScriptFold()
After restarting vim and opening a JavaScript file I am not able to fold using the standard zo, zc commands .. any clue ?

Check whether the fold settings have been activated:
:verbose set foldmethod?
:syntax list foldBraces
The first should yield syntax, and the syntax group should be defined.
Note that only { ... } blocks spanning multiple lines are folded by this.

For anyone else who may come across this scenario; I had the exact same problem. The fix, for me, was to edit my .vimrc by moving
syntax enable
above
au FileType javascript call JavaScriptFold()

Related

TypeScript file is considered as xml when indenting

In a nutshell:
In my .vimrc I have the following line to have vim indent xml files correctly:
autocmd FileType xml set equalprg=xmllint\ --format\ -
the weird thing is that now vim tries to use xmllint when I try to indent TypeScript files, and I don't understand why...
More details
When I open test.ts in vim, I can see that vim correctly detects the type (ie: set filetype? returns filetype=typescript).
But when I'm trying to indent this block of code
for(var i=0 ; i < 1 ; i++){
console.log(i);
}
by putting the cursor on a curly bracket and pressing =%, then this block is replaced with
Exception : System.Xml.XmlException: Data at the root level is invalid. Line 1, position 1.
at System.Xml.XmlTextReaderImpl.Throw(Exception e)
[... eluded for brievity ...]
at xmllint.Program.Main(String[] args)
(which means that vim launched the xmllint command and that this command failed (that failure is expected since it's not xml. But vim shouldn't have launched xmllint in the first place)).
Two other observations which might be useful are:
when I remove this line from my .vimrc then indentation works as expected
if I change the file extension to anything else (say test.dummy) then indentation works as expected
Even more details
I'm using the TypeScript plugin https://github.com/leafgarland/typescript-vim . If I delete this plugin then set filetype? detects .ts files as xml (not sure where this matching comes from...)
:scriptnames returns
/etc/vimrc
/usr/share/vim/vim81/defaults.vim
/usr/share/vim/vim81/syntax/syntax.vim
/usr/share/vim/vim81/syntax/synload.vim
/usr/share/vim/vim81/syntax/syncolor.vim
/usr/share/vim/vim81/filetype.vim
~/.vim/ftdetect/typescript.vim
/usr/share/vim/vim81/ftplugin.vim
/usr/share/vim/vim81/indent.vim
~/.vimrc
/usr/share/vim/vim81/syntax/nosyntax.vim
/usr/share/vim/vim81/plugin/getscriptPlugin.vim
/usr/share/vim/vim81/plugin/gzip.vim
/usr/share/vim/vim81/plugin/logiPat.vim
/usr/share/vim/vim81/plugin/manpager.vim
/usr/share/vim/vim81/plugin/matchparen.vim
/usr/share/vim/vim81/plugin/netrwPlugin.vim
/usr/share/vim/vim81/plugin/rrhelper.vim
/usr/share/vim/vim81/plugin/spellfile.vim
/usr/share/vim/vim81/plugin/tarPlugin.vim
/usr/share/vim/vim81/plugin/tohtml.vim
/usr/share/vim/vim81/plugin/vimballPlugin.vim
/usr/share/vim/vim81/plugin/zipPlugin.vim
/usr/share/vim/vim81/ftplugin/xml.vim
/usr/share/vim/vim81/indent/xml.vim
/usr/share/vim/vim81/syntax/xml.vim
/usr/share/vim/vim81/syntax/dtd.vim
~/.vim/ftplugin/typescript.vim
~/.vim/compiler/typescript.vim
~/.vim/indent/typescript.vim
~/.vim/syntax/typescript.vim
Also, not sure it it's useful information, but I'm using ViM 8.1 on windows 10 (installed along with git-bash)
I'm not sure why your TypeScript is first detected as XML (maybe some other answer can dive into that), but it can happen that a generic (default, built-in) filetype is first detected, and then revised by a more specific detection (as in your case, via the TypeScript plugin). The problem you have is that the 'equalprg' lingers one, because you used the shortcut :autocmd instead of a proper filetype plugin:
autocmd FileType xml set equalprg=xmllint\ --format\ -
If you only want to enable an option for certain filetypes, use :setlocal option=value (your use of :set would make the setting be inherited by new buffers opened from that one, which is wrong), and put the corresponding :setlocal commands into ~/.vim/after/ftplugin/{filetype}.vim, where {filetype} is the actual filetype (e.g. xml here). (This requires that you have :filetype plugin on; use of the after directory allows you to override any default filetype settings done by $VIMRUNTIME/ftplugin/{filetype}.vim.) Proper plugins also undo any settings via the :help undo_ftplugin mechanism.
So, a proper configuration in your case would replace the :autocmd with ~/.vim/after/ftplugin/xml.vim:
setlocal equalprg=xmllint\ --format\ -
let b:undo_ftplugin = (exists('b:undo_ftplugin') ? b:undo_ftplugin . '|' : '') . 'setlocal equalprg<'
With that, 'equalprg' would still be briefly set as long as the filetype is XML, but it would then be undone by the command defined in b:undo_ftplugin, which Vim executes automatically when the filetype changes to typescript.

adding new file type to ultisnips

I am trying to add for the cuda (.cu) files. The basic objective is to first make all c,cpp snippets available for the cu files and then add additional support. The first thing I did to test is to set the filetype inside vim
set ft:cpp.c
and this works. Then I tried to go to the vim-snippets/snippets and vim-snippets/UltiSnips and tried to copy the cpp.snippets file to cu.snippets. But this is not working (not working as in --the snippets are not detected--) . I have also added
au BufNewFile,BufRead *.cu set ft=cuda
au BufNewFile,BufRead *.cuh set ft=cuda
in my .vimrc. Even after this it is not working.
I also checked the UltiSnipsSnippetDirectories. It is pointing to Ultisnips.
I also tried creating a cu.snippets which just tries to extend cpp (nothing else). This is also not working.
As a side question: As far I understand https://github.com/honza/vim-snippets has two folders with snippets. snippets/* for the snipmate based ones and UltiSnips/* for the ultisnips based ones. However the inc snippet is only provided on the c.snippets in snippets directory (not in ultisnips). But strangely inc works on c files for me. I am positive that I am not using snipmate. How could this happen? Am I missing something. or is it that ultisnips can understand both formats?
Ultisnips uses Vim's filetype detection system. So to see what filetype Vim thinks you have use the :set filetype? command.
If that's incorrect, you can try
autocmd BufRead,BufNewFile *.cu setfiletype cuda
Also, I used Vundle, and I used call vundle#rc(), but I needed to change that to call vundle#begin() and call vundle#end()
I think, some plugin in your plugins list conflicts with your custom filetype detection config. Just faced with the same issue (tried to declare custom filetype for Jest typescript tests for react). My filetype settings overrides by peitalin/vim-jsx-typescript plugin.
So you should to switch off some of yor installed cpp plugins for detect culprit.

Vim Vundle - load ftplugin for different filetype

How, using Vundle, can I load a ftplugin for a filtype it wasn't written for?
In my old .vimrc (before I started using Vundle), I would do something like this:
au FileType xquery ru fplugin/xhtml.vim
But that doesn't seem to be doing the trick.
...any thoughts?
Thanks!
(Not a Vundle user, and I think it should not affect your command.)
Instead of the :autocmd, create a file ftplugin/xquery.vim (either under ~/.vim/ or as a bundle), with the following contents:
runtime! ftplugin/xhtml.vim
runtime! ftplugin/xhtml_*.vim ftplugin/xhtml/*.vim
(This assumes you have :filetype plugin on.)
OK, so I'm just coming back to this, and it turns out that what I had up there actually does work. Except that the line that I'm actually using in Vim uses a lower case "t" in "Filetype". Not sure if that was the issue, but it works perfectly now:
au Filetype xquery ru ftplugin/xhtml.vim

How to enable go.vim by default (automatically)?

The instructions on the Vim site says to just put the file in the /syntax folder. This works all right and well. But, for me to use the syntax I must set the following
:set syntax=go
Every single time. So, I know I am doing something wrong. I just don't know what.
Here are some things from looking around,
My HTML5 syntax set is from Rodrigo's HTML5 omnicomplete function and syntax vimball file. Though this uses some installation script to get it going.
As far as I can tell this would be my first manual adding of syntax file.
Also, my VIMRUNTIME is not set, well because there is no syntax.vim file, so from reading the documentation I see it checks for files via synload.vim
I even read the "Making Your Own Syntax Files" section, which says that same as above with the syntax=go option. Am I supposed to be detecting the .go filetype as described in new filetype section?
How can I enable syntax highlighting for GO by default?
This is for Mac Snow Leopard.
I don't think it is this complicated but I decided to leave all the different documentation I skimmed. GO and Vim say to just add the file. But it is definitely not detecting it automatically
If you are using filetype detection in your ~/.vimrc file with the following line:
filetype plugin indent on
then you can place the file in the following folder:
~/.vim/after/ftplugin/go.vim
or for windows
~/vimfiles/...
For the filetype detection to work, would would want the autocmd in a file in the ftdetect folder:
~/.vim/ftdetect/go.vim
and the contents would be:
autocmd BufNewFile,BufReadPost *.go set filetype=go
Use autocmd:
au BufRead,BufNewFile *.go setlocal filetype=go

Vim: How to change text from within an indent script

I recently switched from Eclipse to Vim. I'm loving it. There are a few hangups I'm working on, but one of the ones I'm having lots of trouble with is the PHP doc comments. In eclipse I could type:
/** [enter]
and the next line would auto fill with
*
So I'd have:
/**
* [comment goes here]
I'm wondering if there's anything like this for vim. It seems there are some plugins to autogenerate doc comments by running a command, but I'd love to have it do them as I'm typing.
I was playing around with the PHP indent script (http://www.vim.org/scripts/script.php?script_id=1120) and I got it to recognize when it's inside of a doc comment block, but I can't figure out how to get it to actually change the text and add a " * " after hitting enter when inside the block.
I've tried what I've seen other plugins do:
let #z = ' * '
put! z
tried this too:
exe 'normal!' '"zgp'
but no luck. Is this not possible from an indent script, and if not, how do I actually get Vim to recognize a doc comment block and act accordingly while I'm typing?
Any help would be greatly appreciated!
No need to mess around with the indentation files. Vim's formatoptions will do this for you and in a variety of languages (not just PHP).
Ensure you have r included in your formatoptions:
:setlocal fo+=r "to set
:set fo? "to query
You can include this in your .vimrc or in .vim/ftplugin/php.vim (if you just want to activate this for PHP).
For more information on formatoptions and file-type plugins, see:
:help 'formatoptions'
:help fo-table
:help ftplugins
Would adding the below code to your vimrc do something similar to what you want?
autocmd BufNewFile,BufRead *.php setlocal formatoptions+=r formatoptions+=o
autocmd BufNewFile,BufRead *.php setlocal comments=s1:/*,mb:*,ex:*/,://,:#
I currently can't quite figure out how to make it work without overriding the <!-- ---> commenting, which this does. I.e. this will break auto-indenting with <!-- --> comments.
Edit. Added ://,:# to comments as Johnsyweb's distribution does.
Try adding this to your vimrc:
let g:PHP_autoformatcomment=1
I'm on a Mac and it seems to be enabled by default. Functions exactly how you stated.

Resources