Override syntax highlighting in vim file - vim

I am trying to override a markdown file syntax by placing the following file md.vim in my after/syntax directory. md.vim contains the following code:
syntax region mdNote start=/\<\cNOTE\>/ end=/\r/
highlight def link mdNote Todo
I have tested that the code works by sourcing it directly but when I launch a file with an extension .md, the mdNote syntax does not work. For example, given the following markdown file:
# Main Heading
Note: This is a note
If I put the cursor inside the word Note and get the syntax group, I get:
mkdNonListingItemBlock which comes from the plasticboy/markdown plugin that I have installed.
Does anyone know why my syntax file is not working?

Markdown files will reference the markdown.vim file in after/syntax. To get this information for any file, you can open a file of the desired format and run :set syntax?. Rename your file to markdown.vim and it should work.

Related

Vim - How to insert a backslash at the start of a line in a new file using autocmd and a template file

I followed this guide to automatically insert different header templates into new files of different types based on the file extension:
http://www.thegeekstuff.com/2008/12/vi-and-vim-autocommand-3-steps-to-add-custom-header-to-your-file/
It works great! I have a custom header for python source files that gets inserted automatically when I open a new .py file.
I want to do a similar thing so that a basic LaTeX template is inserted when I open a new .tex file...
Except I can't get it to work...
My ~/.vimrc says this:
autocmd bufnewfile *.tex so /home/steve/Work/tex_template.txt
and my tex_template.txt says this:
:insert
\documentclass[a4paper,12pt]{article}
.
but when I open a new file like this:
vim test.tex
(where test.tex does not exist already)
I get this:
"test.tex" [New File]
Error detected while processing /home/steve/Work/tex_template.txt:
line 2:
E492: Not an editor command: :insertdocumentclass[a4paper,12pt]{article}
Press ENTER or type command to continue
The problem appears to be with the backslash at the start of the line because if I delete the backslash from tex_template.txt the the new file opens up with documentclass[a4paper,12pt]{article} in it. Except I need the backslash because otherwise it's not a tex command sequence.
If you look at :help :insert it says this:
Watch out for lines starting with a backslash, see
line-continuation.
Following the link to line-continuation explains that the \ is a continuation character which can be overridden by passing the C flag to cpoptions.
It should work if you change your template as follows:
:set cpo+=C
:insert
\documentclass[a4paper,12pt]{article}
.
:set cpo-=C
You might want to consider using a snippets engine like vim-snipmate or (my favorite) ultisnips. With those you can insert snippets of text everywhere, not just at the beginning of a file.
As a bonus, these snippets can e.g. substitute variables and even run commands. The following is my snippet (for ultisnips) set to produce the header for a TeX file;
snippet hdr "File header for LaTeX" b
% file: `!v expand('%:t')`
% vim:fileencoding=utf-8:ft=tex
%
% Copyright © `!v strftime("%Y")` ${1:R.F. Smith} ${2:<my#email>}. All rights reserved.
% Created: `!p snip.rv = fcdate(path)`
% Last modified: `!v strftime("%F %T %z")`
$0
endsnippet
This will automatically fill in the file name and the time when the file was last modified. It fills in my name and e-mail with default values but gives me the opportunity to override them. The fcdate function is a piece of Python code that I wrote to retrieve the birthtime of a file.
I have the hdr snippet defined for several different filetypes, and a general one that is used for all other files. If I type hdrtab at the beginning of a line, the appropriate snippet is expanded.

the dollar sign `$` cannot be removed from iskeyword [duplicate]

In my vimrc, I have included a script (say, otherscript.vim, which I need to include for work reasons) that says:
autocmd FileType php setlocal iskeyword+=$
and I don't want this behaviour. So, sometime later in the vimrc, I say:
autocmd FileType php setlocal iskeyword-=$
(I also tried using set instead of setlocal.) But, when I open a php file, iskeyword still contains the $ symbol in it. I am using vim 7.2. The output of ':verbose set iskeyword' is
iskeyword=#,48-57,_,192-255,$
Last set from /path/to/otherscript.vim
The output of ':scriptnames' is:
...
7: /usr/share/vim/vim72/ftplugin.vim
8: /home/yogeshwer/.vimrc
...
74: /path/to/otherscript.vim
...
Can somebody help me how I can revert the changes to 'iskeyword' made by the other script? Thanks a bunch.
I like to avoid autocmds when I can and use the after directory structure.
$ mkdir -p ~/.vim/after/{ftplugin,syntax,indent}
$ echo 'setlocal iskeyword-=$' >> ~/.vim/after/ftplugin/php.vim
This sets up a basic after directory in your user-specific vim config folder. Whereas ~/.vim/ftplugin/$FILETYPE.vim would be used in lieu of vim's standard $FILETYPE.vim file, files in an after directory get executed after, allowing you to override or change the behavior of your ftplugins, syntax definitions, and indent commands.
As an additional example to show you how these work, I'll include part of my local after/syntax/python.vim file here. I like all the "structural punctuation" of my code to stand out when I read it, so I do this:
syn match pythonParen /[()]/
syn match pythonBrack /[][]/
syn match pythonCurly /[{}]/
hi def link pythonParen Paren
hi def link pythonBrack Brack
hi def link pythonCurly Curly
I've also got an after/indent/php.vim file that was supposed to fix some of the annoying indent issues I ran into with the indent behavior when switching in and out of <?php ?> regions in a template file, but the code is a mess and never really worked in the first place, so I won't reproduce it here. I mention it only to give you an example of what can be done with the after hooks.

Proper way to create a syntax file in vim that depends on another syntax file

I am currently creating a Vim syntax file for WordPress and have it inside a file called wordpress.vim
All WordPress files are PHP files but not all PHP files are WordPress files.
My wordpress.vim syntax file depends on the php syntax file. So I have included it using the following line
so <sfile>:p:h/php.vim
My question is what is the most elegant way to include all PHP syntax into WordPress syntax and where should I put the the new wordpress.vim file that I have created.
The right location for your syntax file should be:
~/.vim/syntax/wordpress.vim
You should take a look at $VIMRUNTIME/syntax/cpp.vim for what I assume is the correct syntax:
if version < 600
syntax clear
elseif exists("b:current_syntax")
finish
endif
" Read the C syntax to start with
if version < 600
so <sfile>:p:h/c.vim
else
runtime! syntax/c.vim
unlet b:current_syntax
endif

Removing a character from 'iskeyword' in vim

In my vimrc, I have included a script (say, otherscript.vim, which I need to include for work reasons) that says:
autocmd FileType php setlocal iskeyword+=$
and I don't want this behaviour. So, sometime later in the vimrc, I say:
autocmd FileType php setlocal iskeyword-=$
(I also tried using set instead of setlocal.) But, when I open a php file, iskeyword still contains the $ symbol in it. I am using vim 7.2. The output of ':verbose set iskeyword' is
iskeyword=#,48-57,_,192-255,$
Last set from /path/to/otherscript.vim
The output of ':scriptnames' is:
...
7: /usr/share/vim/vim72/ftplugin.vim
8: /home/yogeshwer/.vimrc
...
74: /path/to/otherscript.vim
...
Can somebody help me how I can revert the changes to 'iskeyword' made by the other script? Thanks a bunch.
I like to avoid autocmds when I can and use the after directory structure.
$ mkdir -p ~/.vim/after/{ftplugin,syntax,indent}
$ echo 'setlocal iskeyword-=$' >> ~/.vim/after/ftplugin/php.vim
This sets up a basic after directory in your user-specific vim config folder. Whereas ~/.vim/ftplugin/$FILETYPE.vim would be used in lieu of vim's standard $FILETYPE.vim file, files in an after directory get executed after, allowing you to override or change the behavior of your ftplugins, syntax definitions, and indent commands.
As an additional example to show you how these work, I'll include part of my local after/syntax/python.vim file here. I like all the "structural punctuation" of my code to stand out when I read it, so I do this:
syn match pythonParen /[()]/
syn match pythonBrack /[][]/
syn match pythonCurly /[{}]/
hi def link pythonParen Paren
hi def link pythonBrack Brack
hi def link pythonCurly Curly
I've also got an after/indent/php.vim file that was supposed to fix some of the annoying indent issues I ran into with the indent behavior when switching in and out of <?php ?> regions in a template file, but the code is a mess and never really worked in the first place, so I won't reproduce it here. I mention it only to give you an example of what can be done with the after hooks.

Vim problem with gf command

I am using Vim and I have set the path (set path+= c:/work/etc/etc) to my project directory (for C#), but still using command 'gf' give me error:
E:447 Can't find file.
Is there anything I am doing wrong over here?
G'day,
To get a bit more detail on your current path settings you can see what's being included and the files vim can't find by entering the command:
:checkpath
and you'll get a dump of the files not found, e.g.
--- Included files not found in path ---
<io.h>
vim.h -->
<functions.h>
<clib/exec_protos.h>
Or you can get a listing of all included files, both found and not found, by entering
:checkpath!
Enter
:help path
to get more info on the path syntax.
Edit: Don't forget that using the syntax
set path=/work
will completely reset your path variable to what you've just declared. I'd suggest using
set path+=/work
instead. This won't clobber the current path and will just add your /work directory instead.
HTH
I also found out that
:set path+=./foo/bar
adds a search location relative to the directory of the current file, just like '.' does.
My vim didn't want to search for such include
#include <common/util/string.h>
So what I needed to do was
:set path+=foo/bar
instead of
:set path+=./foo/bar
The former adds a search path relative to current working directory. Hopefully it helps someone.
First can you open the file using :find file.name ? (:help find for more info). If this does not work then your path is wrong. If :find does locate your file then do the following:
Insure that you are not in Visual/Insert mode
Place cursor on the first letter of the filename and press gf
I know this is an old question, but I also had some troubles with this for another reason and it took me some time to find out why. I hope this might be helpful to someone.
When a directory is matched with wildignore, gf does not work for files in it, nor does :find.
This is obvious if you read wildignore's documentation, but I forgot I ever changed this variable, and what it was for exactly. Also I used a glob, and it was not immediately apparent to me that the directory I was using gf in, was also matched with this glob.
Make sure there is no leading character to the file name if you press gf, i.e. using gf when the cursor is on help.txt will not work here:
file=help.txt
If you are talking about the gf tool wri††en by tomnomnom then here's how to set-up:
Setting PATH for GO (if you have not setup yet).
export GOROOT=$HOME/go
export PATH=$PATH:$GOROOT/bin
Step 1: Download tool from github
Step 2: cp -r path/to/tomnomnom/gf/examples ~/.gf
Step 3: source ~/tools/gf/gf-completion.bash
Now gf should work along with auto-completion from anywhere.
Source: Original sources are present at his repo.

Resources