How do I turn on vim syntax highlighting via modeline? - vim

In my .vimrc file I have
syntax off
Suppose I want to turn on syntax highlighting on a case-by-case basis via vim's modeline? I've tried many combinations, like:
# vim: syntax on:
but I still can't get it to work. What do I need to do in the modeline?

According to :help syntax, using syntax enable or syntax on loads syntax files at runtime. But there's also apparently syntax manual which turns it on based on the syntax type you specify. Looking at the source vimscript, it says:
It installs the Syntax autocommands, but not the FileType autocommands.
You can therefore use syntax= to set the type, and that works in a modeline to either set a specific type or set none which effectively turns it off.
# vimrc
syntax manual
# In your files
# Turn it on for this yaml file
# vim: syntax=yaml:
# Or this PHP file
# vim: syntax=php:
If you want to be explicit about disabling it in a file:
# In your files
# No syntax highlighting for this file (default if omitted)
# vim: syntax=none:

Vim is a multi-buffer/window/tab editor. Changing global state (syntax, colorscheme, loading plugin etc.) per current file is a wrong habit. Luckily, modeline does not allow this.
What you can do in the modeline is to set :h local-options. Accidentally, there's an option also named :h 'syntax'. And it has the same syntax as any other option.
# vim: syntax=OFF
You don't need to set :syntax manual for this to work, as modeline has preference over filetype detection. However, if FileType event is fired manually (e.g. :setf xyz) while :syntax on then buffer's syntax could be redefined to match new filetype. Not a problem though.

Related

Vim: How to permanently allow auto indent only on manual linebreak

Vim has a tendency to automagically mess with the indentations in the line I'm currently working on, as well as placing linebreaks. I want it to do an automatic indentation when I manually make a linebreak (i.e. physically hitting the Enter key), but not in any other case, I don't want it to make any linebreaks itself either, and I want these settings to be permanent.
The textwidth option controls the number of characters after which a line break will be inserted, see :h textwidth.
To turn off the insertion of line breaks while editing, the following lines should be added to .vimrc:
set textwidth=0
set wrapmargin=0
set formatexpr=
Information on the options can be obtained by the usual :h command.
Note that these options are set globally but may be overridden e.g. by a filetype plugin. If the problem persists, see :h ftplugin.
Another question is where :set textwidth=132 originates from. This may be due to an earlier misconfiguration or set from any plugin.

How do you turn off syntax highlighting for new vim windows without a filename or file type?

After I installed the 'artesanal' theme for vim and turned syntax highlighting on, every vim window has syntax highlighting including brand new empty windows [No Name], without a name or file type. I'm wondering if any of you know how to keep syntax highlighting on for every file with an extension but have it disabled for any file without a name or file extension.
This should not happen. I don't know artesanal (and "theme" is an undefined term inside Vim; it has colorschemes, filetype plugins, and syntax scripts; I hope it's not a full Vim "distribution" like spf-13 and Janus, which lure you with a quick install and out-of-the-box settings, but you pay the price with increased complexity (you need to understand both Vim's runtime loading scheme and the arbitrary conventions of the distribution) and inflexibility (the distribution may make some things easier, but other things very difficult)).
It looks like a syntax is active even for plain files. Usually, the syntax is determined by the filetype, so check :verbose setlocal filetype? first. If this returns a value, you need to look into the detection of :help filetypes.
If this is empty, it could also be that something sets 'syntax' directly. You can check in the same way: :verbose setlocal syntax?.
Now, if that also is empty, and :syntax list doesn't show something, the highlighting could also come from :match or :call matchadd() commands; :call clearmatches() would remove this then. (And you still would need to find the source that defines those matches.)
You can check to see if a filetype has been set
if &filetype != ""
syntax enable
endif

Set spelling exception in vimrc

When editing a text in markdown, I don't want to highlight bibliography entries. This can be achieved with the following command:
:syn match CitNoSpell '\[#[^[:space:]]\+\]' contains=#NoSpell
However, if I enter this command to .vimrc, it is ignored. I assume that is because spell file is loaded after vimrc has been read, and this definition is not kept.
How should I force vim to ignore this pattern? I prefer it to stay in .vimrc, as I synchronize the file across multiple systems, but another solution would also be welcome.
As the ~/.vimrc is loaded first (before any files), the syntax of an opened file is set only later, and syntax scripts :syntax clear any existing syntax stuff, including your definition.
The right place for your customization would be the :help after-directory; i.e. ~/.vim/after/syntax/markdown.vim, as this will be sourced after $VIMRUNTIME/syntax/markdown.vim.
If you insist on configuring this within your ~/.vimrc, you can try the following autocmd, which has to be put somewhere after :syntax on:
autocmd Syntax markdown syn match CitNoSpell ...
PS: For consistency, as you're tweaking the Markdown syntax, your added syntax group should also start with the syntax name, i.e. markdownCitNoSpell.

VIM Syntax coloring for error listings

We use shell scripts to call various cmake operations to build our product. The information echoed to STDERR is the errors output from the g++ compiler. I can use stream redirection to get the errors into a file
myBuild.sh 2> errors
and I can edit that file along with the various sources. Syntax highlighting is working in the .cpp's and .h's but the errors file is unhighlighted.
Is there a way to get vim to colorize my errors file? Perhaps adding a filetype as in errors.err or some script stored in $VIM_something?
Example output
/wxyzModule/wxyzModule.h: In member function 'void WxyzModule::setIsTesting(bool)':
/wxyzModule/wxyzModule.h:48:48: error: 'm_isTesting' was not declared in this scope
If I :set filetype=cpp, the 48:48 is red, bool is green, and not and this are yellow. Everything else stays the same white as if no highlighting were done.
Consider instead to using :make from within vim, you can set it to use your shell command by setting :h 'makeprg' option, eg.) set makeprg=myBuild.sh\ 2>&1, the default :h 'errorformat' should work with this, otherwise you could tweak it.
Though your error file contains C++ function and variable names, it is not C++ syntax (especially the overall structure with the filename in front is different). Therefore, trying to apply :setfiletype cpp to it is bound to fail.
If you really need highlighting in that, you have to write your own syntax plugin (e.g. called g++errorformat). You can certainly copy certain syntax elements from $VIMRUNTIME/syntax/cpp.vim, but essentially, you're writing a separate syntax.
Note: If you load the error file into Vim's quickfix list (:cfile errors), you'll get basic highlighting of filename and line / column, and amenities like jumping to the location of the error.
You can set the filetype in vim to e.g. cpp with
:set filetype=cpp
If you want to know what filetype you are currently using, you can just type
:set filetype
If you don't want to type that every time, you can use an autocmd
autocmd BufNewFile,BufRead *.err set filetype=cpp

Quickest Way to Revert Spaces to TABs in VIM

I have always been one to replace TABs in VIM with x amount of spaces (usually 4).
Four lines I almost always use in my .vimrc config file are:
set tabstop=4
set shiftwidth=4
set expandtab
syntax on
Basically, there are moments when I NEED to use a single TAB (such as Makefiles), and I don't know how else to work around that other than leaving the file, editing my .vimrc, and then reloading the file of interest.
That said, what is the quickest way (from within VIM) to revert it back to using TABS, and then reverting back to my original settings? I'm looking for the least-hassle, least-keystrokes solution.
VIM will automatically enable the TAB for a makefile, assuming you name it "makefile," as opposed to "Makefile." Not sure why VIM still doesn't detect the type with a lower-uppercase difference, but such is life. (#Sedrik)
That aside, other alternative solutions are:
Filetype Binding (#ThorstenS #tungd):
autocmd FileType make setlocal noexpandtab
RealTime Switch (#ThorstenS):
Assuming the .vimrc configuration mentioned in the question, do:
:set noet (to switch from spaces to TAB)
and :set et (to switch back)
Just use the magical escape key available in insert mode.
On the *NIX's it is ^V by default when you are in insert mode. On Windows, you need to find out what the magical escape character is - ^V is taken for something else; I think it may be ^Q or ^S?
So! In your makefile:
this: this.c
<C-V><Tab>cc this.c
where the usual meanings apply:
means hit ctrl-V (you should see a ^ hiding away under the cursor)
- hit the tab key. Bingo.
Works for me.
Note: if you use vim settings or startup code which smashes tabs as you read a file, this obviously is a short-term fix. I prefer to learn how to use the retab command to ensure a file is tab-clean, because I don't like a file to be touched unless I consciously choose to do so.
Just type set noexpandtab . Perhaps you bind this to a function key.
Only this configuration helped me solve this problem.
filetype plugin indent on
filetype detect
autocmd FileType make set noexpandtab
Vim defaults to tabstop=8 and noexpandtab, so the defaults are well suited to working with Makefiles. If your .vimrc uses custom options for tabstop, expandtab, etc., one simple solution is to bypass your .vimrc while working with Makefiles.
From the manpage (emphasis mine):
-u {vimrc} Use the commands in the file {vimrc} for initializations. All the other initializations are skipped. Use this to edit a special kind of files. It can also be used to skip all initializations by giving the name "NONE". See ":help initialization" within vim for more details.
Since I can never remember the necessary flag/value/formatting, I've created a Bash alias which remembers for me: alias vimnone='vim -u NONE'
You can create a custom configuration per file type.
mkdir -p ~/.vim/after/indent
echo 'set noexpandtab' >> ~/.vim/after/indent/make.vim
Basically, here we created a new indent configuration for makefiles that will be loaded after other scripts.
source

Resources