I'm making a syntax highligthing plugin for a language, I have this project tree :
Ark/
ftdetect/
Ark.vim
syntax/
Ark.vim
syntax/Ark.vim :
if exists("b:current_syntax")
finish
endif
syntax keyword ArkKeyword let mut
highlight link ArkKeyword Keyword
let b:current_syntax = "arkscript"
And when I open a .ark file, those keywords aren't highlighted, and I can't figure out what is wrong, what should I do to make the syntax highlighting work ?
Related
Would the following function always detect a vim help file? If not, what might be a better pattern or function to set this?
function IsHelpFile()
if trim(getline('$')[0:3]) !=? 'vim'
set filetype=markdown
colorscheme OceanicNext
endif
endfunction
For example, from:
No, this function doesn't detect help files. It detects if last line does (not?) start with pattern '.vim' (where '.' means any character in RegEx).
I think you wanted to check if it does start with it, if so then it should be
if getline('$') =~ '^.vim'
" ...
endif
=~ checks if left side matches regular expression on the right. ^.vim means "line starting with some charater followed by string 'vim'".
But it still wouldn't detect whether the file is help file. It would detect if last line contains something what could be a modeline (:h modeline).
The easiest way to check if file is help page is to check its filetype:
function IsHelpFile()
if &filetype == "help"
set filetype=markdown
colorscheme OceanicNext
endif
endfunction
Note that colorscheme won't apply only to help page, but to whole Vim session
Although I discourage you from changing filetype of help file. It has a lot of special formatting and options to make it readable and easy to navigation. Changing it to Markdown will remove all of that.
In vim, I am editing a file of filetype "markdown", but which contains latex math expressions such as $x_i$. Vim's syntax highlighting for markdown thinks the pattern *_* (letter-underscore-letter) is an error, and highlights the underscore in such patterns bright red. I would like to turn this off by adding a line to my .vimrc:
autocmd BufEnter *.Rmd "Dear vim, please don't highlight the pattern *_*"
What is the appropriate command to do that? Is it possible at all to do that in .vimrc, without editing a syntax file?
Note: I want to keep the markdown syntax highlighting in general, only turn off that particular feature.
If you want to remove _ from the markdown error pattern, you can redefine it. In my case I want to turn off error notifications of underscores in a word as I put a lot of URLs in my documents.
There's a line that defines the error pattern inside syntax/markdown.vim file
" Original error pattern
syn match markdownError "\w\#<=_\w\#="
Remove the _ from the pattern and add that to ~/.vim/after/syntax/markdown.vim.
" New error pattern without the underscore
syn match markdownError "\w\#<=\w\#="
You have to modify the Markdown syntax for that. One way would be to remove the parsing of the error:
:syn clear markdownError
But that would cause the other syntax rules to start an italic section on that _ char. Better just clear the error highlighting with:
:hi link markdownError Normal
To maintain the general error highlighting, but only define exceptions for the special $x_i$ string, define an overriding syntax group; luckily, this is easy because no existing syntax is there:
:syn match markdownIgnore "\$x_i\$"
(Adapt the regular expression to match all possible math expressions.)
This needs to be put into ~/.vim/after/syntax/markdown.vim to be executed after the original syntax script.
I write blog posts with Jekyll, and for that I end up using three different languages in the same file: YAML at the start for post metadata, Markdown in the body, and C++ in code snippets.
I tried to setup a script so that I can have vim highlight all three properly for me, and ended up with something like this in syntax/jekyll.vim:
" Build upon Markdown syntax
runtime! syntax/markdown.vim
" pretend there's no syntax loaded
unlet b:current_syntax
" Bring in YAML syntax for front matter
syntax include #Yaml syntax/yaml.vim
syntax region yamlFrontmatter start=/\%^---$/ end=/^---$/ keepend contains=#Yaml
" pretend there's no syntax loaded
unlet b:current_syntax
" Bring in C++11 syntax for code snippets
syntax include #Cpp syntax/cpp.vim
syntax region cppCodeSnippet matchgroup=Comment start=/^{% highlight cpp %}$/ end=/^{% endhighlight %}$/ keepend contains=#Cpp
let b:current_syntax='jekyll'
I also set up a file detection script to set ft to this syntax.
It almost works. When I open a file that gets detected as this type, I get everything correct except for the C++ highlights. However, if I type :syn on after that, everything works fine. I can delete the buffer and open the file again and all highlights are ok. If I close vim and start it again, I need to run :syn on again.
What am I missing? How can I debug this issue?
Quick fix: append syntax on to the last line of your .vimrc, which is the same as setting :syn on in the live session.
Not So Quick:
Looks like you might have installed the custom 'jekyll' syntax alongside the default syntax files in $VIMRUNTIME.
According to Vim wiki section on custom syntax, it's preferable to keep all personal customizations within ~/.vim. For example, putting your jekyll.vim syntax in ~/.vim/syntax/.
Do not use a directory containing the files distributed with Vim because that will be overwritten during an upgrade (in particular, do not use the $VIMRUNTIME directory).
In the Vim syntax docs:
:syntax enable runs ':source $VIMRUNTIME/syntax/DEFAULT_SYNTAX.vim'.
:syn on (or :syntax on) will "overrule your settings with the defaults".
So if setting :syntax on makes your custom syntax work, it must be contained in the default syntax set.
Try keeping all the custom stuff in ~/.vim and see if that settles things.
I'm trying to add a conditional to my vimrc file testing if number (line numbers) option is set. I have :number added only with HTML file type,
:autocmd FileType html source $HOME/.vim/html_vimrc.vim
:if ('&number' == 0) "IF number is off
: set spell "turn on spell checker
:else "ELSE file type is HTML
: set nospell "turn spell checker off
:endif
I've tried,
:if (&number == "number")
:if $number
I've tried with parentheses, quotes, no quotes. All my tests either set spell for all or none. I'm reading the VIMDOCS on conditionals, but I'm not getting something right and I can't find a similar example.
Update: In addition to testing Michael's code below, I've simplified the test with the following in vimrc:
:if exists("g:prog")
: set number "trying a different setting
:endif
And in the FileType loaded html_vimrc file I have,
"number "turned this off
:let g:prog=1
In this situation the test case of test.html the g:prog is set but "number" is not. In the test.vim case g:prog is not set and "number" is not. This seems to suggest there is some fundamental logic to how VIM handles loaded vimrc files that I am missing.
Use &number to test a boolean, without quotes.
if (&number == 1)
Or more simply:
if (&number)
So your entire operation looks like:
if (&number == 0) "IF number is off
set spell "turn on spell checker
else "ELSE file type is HTML
set nospell "turn spell checker off
endif
I simplified the situation to
:if exists("g:prog")
: let g:ok_prog=1 "trying a different setting
:endif
Then I used :let to check if the variable is available to the different filetype use cases. This shows the variable from the .vimrc file loaded before the autoloaded g:var. (If someone else has a VIMDOCS reference that would be awesome). All of the conditionals fail because neither my :number option nor the g:var are loaded before the conditional fails.
I came across this question:
https://softwareengineering.stackexchange.com/questions/87077/how-can-a-code-editor-effectively-hint-at-code-nesting-level-without-using-inde
and thought that Vim might be able to do something similar as well with a plugin.
I believe the indentation level could be indicated with the a sign (icon in GUI, text with highlighting in term). The piece I'm not sure about is displaying the lines without indentation. Does anyone know, is it possible and/or how you would do it?
This question is not whether or not displaying indentation levels in this manner is desirable; but how to accomplish it in Vim.
You could use the conceal feature that is new in 7.3.
Here is a function that does roughly what the article is describing (for spaces; accounting for tabs instead would be a fairly trivial addition):
function! IndentationHeatMap()
set conceallevel=1
for i in range(1,9)
let indentation = repeat(" ", &sts * i)
exe 'syntax match NonText "^' . indentation . '" conceal cchar=' . i
endfor
endfunction
A solution closer to what you are requesting might use conceal to hide all leading whitespace with
syntax match NonText "^\s\+" conceal
and then use signs to provide the indicators based on custom calculations.
Note: NonText in these syntax commands is an arbitrary highlight group.
Take a look at these plugins: Indent Guides and IndentHL
Both have screenshots.