I'm using proprietary language that has syntax similar to javascript. In my .vimrc I have set BufReadPost for the file set syntx=javascript which works for most of the part except for backtick.
Unlike in .js, backtick is used to iterate over matrix without closing backtick. Since .vimrc is treating it like a js syntax it expects closing ` or else everything afterwards is string. How do I tell vim to ignore backtick.
In general, modifying an existing syntax script is difficult, as there are many complexities and interactions of parsing groups. However, small adaptations are possible.
If you're using the default JavaScript syntax that ships with Vim, it uses this definition for the backticks:
syn region javaScriptStringT start=+`+ skip=+\\\\\|\\`+ end=+`+ contains=javaScriptSpecial,javaScriptEmbed,#htmlPreproc
To get rid of it, you can :help :syn-clear it:
:syntax clear javaScriptStringT
To apply this automatically, the command has to run after the syntax script has been sourced. Use the after-directory, e.g. as ~/.vim/after/syntax/javascript.vim.
Alternative syntaxes
Especially in the JavaScript area, there are several competing syntax scripts. For example, I use this one; fortunately, it has a similar syntax line that can be cleared in just the same way.
Related
In ipython, I can use ? to get the doc for the object preceding it, like os.path.join?<enter>. Then I get the output like this:
Signature: os.path.join(a, *p)
Docstring:
Join two or more pathname components, inserting '/' as needed.
If any component is an absolute path, all previous path components
will be discarded. An empty last part will result in a path that
ends with a separator.
File: ~/conda/envs/test/lib/python3.6/posixpath.py
Type: function
I want to load this output into Vim and get the syntax highlighting.
Which filetype should I set for it?
If no existing filetype describes this format, how to set the syntax highlight for it?
If Vim came with a syntax highlighting for it, there would most likely also be a filetype detection for it, as these usually come together. You could search the Internet whether someone has already created a syntax for that and published it, on vim.org and/or in a GitHub repository.
To start developing your own syntax see :help :syn-define and :help usr_44.txt.
For example, to highlight the Signature:, File:, ... prefixes, you'd use:
syntax match pythondocPrefix "^\a\+:"
Instead of defining custom colors, it is recommended to link to existing highlight groups; cp. :help highlight-groups.
highlight def link pythondocPrefix Type
This is just a start; you can define as many distinct elements as you like! It helps to look at existing syntax scripts (in $VIMRUNTIME/syntax/) to see how it's done. For the Python string after Signature:, it would be nice to include the Python syntax there; see :help :syn-include.
You'd put all these commands into a file ~/.vim/syntax/pythondoc.vim. :help 44.12 has additional tips.
Usually, you'd then define a :help new-filetype with detection, but as you apparently want to trigger the doc lookup via a custom mapping or command, you can just directly :setlocal syntax=pythondoc in that scratch buffer.
How to write complex syntax parser in flex/bison and connect it as vim plugin ?
Need some generic way to make vim colorer (and completion in ideal case) plugins using flex/bison -- especially for some complex languages, can't be covered by dumb vim regexps.
The same method should be used for Eclipse-like "outline" tab -- parse source and make clickable side panel for fast source navigation.
PS: win32 (mingw) .dll preferrably for this build: http://www.vim.org/download.php#pc
So, you want to do the actual parsing of a Vim buffer outside of Vim, by your flex/bison-based parser, and then perform the highlighting within Vim based on that parsing, right?
Well, inside Vim, the highlighting is based on regular expressions (given to the :syntax match or :syntax region commands). There's no way around that (short of extending Vim itself).
The only way that I see is using line / column addressing for parsed elements. Vim regular expression have special atoms that only match at a particular position (:help /\%l, :help /\%v).
In Vim 8, you could trigger your parser (asynchronously in the background) via the new :help channel feature.
Example
Your parser has identified an identifier at line 3, screen columns 14-16. You would synthesize the following Vim command:
:syntax match Identifier "\%3l\%>13v\%<17v."
Critique
This will work for static buffer contents. As soon as you're doing edits, highlighting will probably be trailing behind. Vim applies the regexp-based highlighting as you type, but your external parsing will add some more delay.
For complex syntaxes / large buffers, the number of distinct syntax elements may cause performance degradation within Vim. I'm not sure how efficient the regexp-based addressing is; this would need to be tested.
Vim often shows wrong highlight when opening perl files contain pod paragraphs, use command
:syn sync minlines=9999
can handle this problem.I am curious about the value of minlines,so,which command will show minlines's value of current opened file? I didn't find that in vim reference manual.
I do not think there is a native command for that. I suggest you to check it out directly in syntax files. For example, about Perl, take a look at perl.vim. I am on Arch Linux and this file is available here: /usr/share/vim/vim80/syntax/perl.vim. If you go to line 435, you should see this: syn sync minlines=0.
Be aware that some syntax files define specific minlines rules. In Ruby/Java files, you should be able to call :echo ruby_minlines or :echo java_minlines respectively. This will not work with Python, PHP or JavaScript.
Finally, if you are ready to sacrifice a bit of performance for better ergonomics, you can add the following command to your .vimrc: autocmd BufEnter * :syntax sync fromstart
I use it to avoid annoying issues with syntax highlighting. It works great, but Vim will be extremely slow if you try to edit huge files...
The most preferable way to do this would be in some like my .vimrc file or another location in my vimfiles that's easily persisted and not attached to an extra plugin.
The help files for VIM (along with almost all solutions found on the Internet abroad) relate directly to adding syntax highlighting for a specific file type.
However, how would one add highlights that apply to all files?
An example would be highlighting extra keywords as part of the Todo highlighting group - such as "NOTE", "INTERNAL", etc.
I've attempted to use vimfiles\after\syntax\..., but again, it seems to be predicated on the right file type getting used for the .vim file created in that directory.
So, something like vimfiles\after\syntax\cpp.vim with the following works to achieve this in C++:
syntax keyword cTodo contained NOTE INTERNAL IMPORTANT
for C++ files specifically, and this works how I would expect it to.
But how can this be generalized to all file types when a file is loaded into a buffer?
You can hook into the syntax script loading via the Syntax event. Be sure to define this after :syntax on in your .vimrc:
:autocmd Syntax * syntax keyword allTodo NOTE
If you also want to handle buffers that don't have any syntax (/ filetype) set, add the BufNewFile,BufReadPost events.
Alternative
Depending on the syntax, you may need to specify contained[in], or :syn cluster add=... to augment the existing syntax. That unfortunately cannot be generalized. An alternative approach would be using :match or :call matchadd(...), which goes on top of (and is totally independent of) syntax highlighting. Since this is window-local, the autocmd would be:
:autocmd VimEnter,WinEnter * match Todo /\<NOTE\>/
In short, I need to know in a Makefile whether `make is being invoked from ViM or not. Is there a certain variable (such as ENVIRONMENT or something), that ViM would set to a specific value?.
The reason I need this is the following:
If called from bash, I could do all sorts of wonderful stuff for user (or myself), such as giving messages as to which subsystem is being built and highlighting errors and warnings.
The problem is however that, when called from ViM, the error messages already get captured and introducing \x1b commands (for color) makes the messages incomprehensible to ViM. What I want to do is basically disable coloring when :make is issued in ViM.
Even though I'd rather have the Makefile resolve it, I am open to other solutions.
I'd either tell the makefile explicitly that you don't want colouring, or filter it out.
If you try to guess inside the makefile, it's liable to break when you use a different editor, or shell, or tweak your environment.
My preference would be to just filter out the non-printable characters:
:set makeprg=make\ $*\ \\\|filter
since this doesn't require explicit workarounds in the makefile. However, the required filter itself is non-trivial: see for example this question on unix.stackexchange.
Alternatively, as already suggested, the easiest way to tell your makefile explicitly is to add a variable to your invocation:
:set makeprg=make\ NO_COLOUR=1
or whatever (make should be the current value of makeprg).
.
Inside Vim, you can set environment variables that are inherited by opened shells; e.g.
:let $INSIDE_VIM = 1
If this is just about :make, you can manipulate the 'makeprg' that determines what is invoked:
:set makeprg=export\ INSIDE_VIM=1;make
If you're just concerned about the escape sequences for coloring, you could just set $TERM to something that doesn't understand colors (dumb maybe?) and if the coloring isn't hard-coded (which unfortunately is a big if in many tools), it should obey the terminal setting and not print the escape sequences.
You can alter your vim binding to SOURCE="vim" make, so in your makefile the $$SOURCE variable is set to "vim".
Edit: I see you are in fact not using bindings in vim, you can use them with the following line in your .vimrc (or /etc/vimrc) like this:
:nmap <F5> <CR>SOURCE="vim" :make<CR>
This will bind F5 to what you want
You could check MYVIMRCor VIMRUNTIME environment variables. On my GNU/Linux distribution, SHLVL value is 1 from bash and 2 when make is invoked from within vim.