I am using a setup where I keep a journal in markdown and have latex code. The trouble is that inside the latex code there are signs that are read as markdown operations and this causes vim to draw some syntax over the second "<".
Is there a way to prevent all syntax highlighting within two pairs of $?
# TEST
* $a < b < c $
Related
I am making changes to an existing C code. If I just want to check my changes, I can easily use vimdiff for that, with old & modified files.
What I want is to limit some syntax highlighting to just the diff part.
Particularly I need to highlight TABS, but only those TABS contained within DiffAdd & DiffChange sections/regions.
What I tried:
syntax region TESTRGN start="TESTRGN_START" end="TESTRGN_END"
highlight TESTRGN ctermbg=lightgreen guibg=lightgreen
syntax match LeadingTabsInRegion display contained containedin=TESTRGN /^\( *\t\+\)\+/
highlight LeadingTabsInRegion ctermbg=darkred guibg=darkred
Above snippet highlights the leading TABS within TESTRGN & tabs in remaining file remain un-highlighted.
However, if I change TESTRGN to DiffAdd in the syntax match line, it does not work as I expected.
My understanding is that DiffAdd is not a region defined using syntax region ... & hence containedin=DiffAdd does not work.
So is there any method/work-around for doing what I am trying to do?
First, what's so bad about having the highlighting everywhere? The diff highlighting stands out well and therefore provides visible scope; couldn't you just ignore the highlighting elsewhere?
In Vim, syntax groups are defined and then linked to colors and attributes defined in highlight groups. The diff stuff uses the second part of that mechanism, but not the syntax part. Therefore, you unfortunately cannot refer to the diff regions within :syntax commands.
There's only a very ugly workaround for this: You have to determine the line numbers that have diff highlighting (via repeatedly stepping through the changes with the ]c motion, and/or using synID() to check for diff highlighting). With those line numbers, you can then define :syntax match commands with the special \%l atom that only matches certain lines. Of course, any addition / deletion of lines would invalidate your definitions, which would need to be frequently re-synchronized (triggered by :autocmd). That's a lot of effort vs. just ignoring the highlighting elsewhere.
Though you need to reset whenever the line was changed, this is just a tip for one of line number based ideas, you can get a list of all diff highlighted lines with:
let dl = filter(range(1, line('$')),
\'index([hlID("DiffChange"), hlID("DiffText"), hlID("DiffAdd")],
\diff_hlID(v:val, 1)) != -1')
And using this, it might be possible to set your TESTRGN with:
exec "syntax match TESTRGN /\\(" . join(map(dl, '"\\%" . v:val . "l"'), "\\|") . "\\).*/"
I want to write a syntax coloring script for a programming language I'm writing a compiler for, but to minimize the work required (as I barely know vimscript) I would like to find a coloring script that produces colors that are close enough to what I want to be able to just edit that script.
What is the best way to have Vim show me a specific code file and then apply every single different syntax coloring script it knows, so that I can determine which is closest? So far I've been using
:set syntax=<the next syntax script>
but is there anything easier, or less repetitive?
You can cycle through all your syntax scripts by applying the following steps:
1: Find all your installed syntax scripts and store them in a variable
:redir #a
:echo glob($VIMRUNTIME . '/syntax/*.vim')
" Hit G
" Hit Enter
:redir END
2: Open a new buffer in vim and paste your scripts from the register 'a' by typing in command mode:
"ap
3: This will output all your syntax scripts (excerpt from the first lines of my 1205 installed ones)
/usr/local/Cellar/macvim/7.4-72/MacVim.app/Contents/Resources/vim/runtime/syntax/2html.vim
/usr/local/Cellar/macvim/7.4-72/MacVim.app/Contents/Resources/vim/runtime/syntax/a2ps.vim
/usr/local/Cellar/macvim/7.4-72/MacVim.app/Contents/Resources/vim/runtime/syntax/a65.vim
/usr/local/Cellar/macvim/7.4-72/MacVim.app/Contents/Resources/vim/runtime/syntax/aap.vim
/usr/local/Cellar/macvim/7.4-72/MacVim.app/Contents/Resources/vim/runtime/syntax/abap.vim
4: Cut these by using Visual Block Mode, so that the result looks like this (again omitting ~1200 lines):
2html.vim
a2ps.vim
a65.vim
aap.vim
abap.vim
abaqus.vim
abc.vim
abel.vim
5: Now strip away the ".vim" ending by applying the following Regular Expression:
%s/.vim//g
Now they will look like this:
2html
a2ps
a65
aap
abap
abaqus
abc
abel
6: Save the result into a buffer. From there you can use this script to cycle through all of them:
v$h"*y:set syntax=^R*^Mdd
Or, in order to color all the open windows, use :windo set syntax=... instead of :set syntax=....
Explanation:
mark the first syntax name
v$h
yank/save the name under the register *
"y
set the syntax in command mode
:set syntax=
delete the last checked syntax name
dd
If you save this script in a macro, you can cycle through them using ##
Enjoy(;
I recommend using colorschemes (read more about them using ":h colo"). You can list all your installed color schemes by using this command:
:colorscheme <ctrl+d>
Those you can copy to a buffer and cycle through them. If you want to go the easy way, there is a script ready that provides that functionality: CycleColor, a script to cycle through (almost) all available colorschemes: https://github.com/vim-scripts/CycleColor
I am looking for a way to pre-compile a text, i.e. reformat it following some custom rules, and then compile it with Tex in Vim.
For example I would like to reformat a text like this
THM The sum $1+2+3 is equal to $$[six] 6.
% a comment line
PROOF $$ 1+2 &= 3 \\ 1+2+3 &= 6
into this
\begin{theorem}
The sum $ 1+2+3 $ is equal to
\begin{align}
\label{six}
6.
\end{align}
\end{theorem}
\begin{proof}
\begin{align*}
1+2 &= 3\\
1+2+3 &= 6
\end{align*}
\end{proof}
and then run MikTex on the latter.
Is this obtainable all inside Vim?
preprocessor
If you want to keep your custom syntax in the source file, and just generate the Latex syntax as an intermediate step to compilation with the Latex compiler, you have to insert a preprocessing step, e.g. using the C preprocessor, cpp. Sketch:
:setlocal makeprg=cpp\ -D\ THM='\\begin{theorem}'\ -\ \|\ miktex\ ...
(In reality, you probably would externalize the (many!) macro definitions in an include file instead of passing them on the command-line.)
snippets
If, on the other hand you just want to avoid typing all those long Latex definitions, but keep the Latex document as the source, you can use snippets to speed up the document creation and editing. snippets are like the built-in :abbreviate on steroids, usually with parameter insertions, mirroring, and multiple stops inside them. One of the first, very famous (and still widely used) Vim plugins is snipMate (inspired by the TextMate editor); unfortunately, it's not maintained any more; though there is a fork. A modern alternative (that requires Python though) is UltiSnips. There are more, see this list on the Vim Tips Wiki.
I'm writing a tutorial/book with Vim, and I'd like to turn off the syntax highlighting for a block of text (the normal text of the book) and reactivate it for the code examples.
I have Googled to no end, but I cannot find a simple solution. Am I missing something? Or is this not achievable in Vim?
I have written the SyntaxRange plugin for that (my main use case is highlighting patches inside emails as with the "diff" syntax). With it, you can :[range]SyntaxIgnore or :[range]SyntaxInclude {filetype} certain sections of a buffer, or, when the sections start and end with certain markers, define dynamic sections that adapt when the number of lines change.
You can create a syntax file for your book.
For example, you can create a script: ~/.vim/syntax/tutor.vim
"
" tutor.vim -- syntax file for my book/tutor
"
syn include #PY $VIMRUNTIME/syntax/python.vim
syn region pyBlock start="{{{" end="}}}" contains=#PY
This is a sample file:
# File: intro.txt
# Date: 2012-08-19
blah, blah ...
So, I will show you some code:
{{{
def hello():
print "world"
}}}
# vim: set syn=tutor :
Thinking tangentially...
How about using something like restructured text or markdown within Vim and render on github. This gives you version management for free. You can have code-blocks.
I have a syntax highlighting file for the q/kdb+ language and I'd like to convert it to a vim compatible file so my q code won't look any more ugly than usual.
Are there utilities available to automatically convert notepad++ xml syntax highlighting files to vi versions? I had a look around but I couldn't find anything.
Alternatively does anyone have a vim q syntax highlighting file?
a q/kdb+ vim syntax highlight files:
https://github.com/simongarland/vim
The answer to both questions is no (I don't know of any converters and I don't have a q syntax highlighting file), but the Notepad++ syntax highlighting XML format looks extremely simple. I don't have the 'Q' one to hand, but I had a look at one of the ones from the website and the translation looks pretty trivial. In that case, you could do most of the work with:
" Remove all the lines that aren't lists of keywords
" (there doesn't seem to be anything much more complicated
" than that in the definition file)
:g!/<Keywords name=/d
" Convert the lines (fairly poor XML parsing here!)
:%s/\s*<Keywords name="\([^"]\+\)">\([[:alpha:]_ ]\{-}\)<\/Keywords>/syn keyword \1 \2/
This generates lots of lines that look like:
syn keyword Words1 case then do while
You'll have to tweak the syntax class (Words1 in this case) to be something that will be highlighted in Vim (or syn-link it to something that will be highlighted in Vim).
You could probably then deal with the symbols with a regexp, but it might be easier to just do them by hand, so convert:
<Keywords name="Operators">- ! " # $ & * , . ; ? # \ ^ { | } ~ + < = ></Keywords>
into:
syn match Operators /\<[-!"#$&*,.;?#\\^{|}~+<=>]/
(this is \< to mark a word boundary, followed by a character class [..] with all the symbols in it).
You would then just need to add:
if exists("b:current_syntax")
finish
endif
at the start and:
let b:current_syntax = "q"
at the end.
Of course, this doesn't get you all the way, but hopefully it will give you a lot of what you need to get the syntax file that you want. There is plenty of help available in:
:help syntax
and by looking at the examples in the syntax directory of the runtime folder.
Good luck!