At every opening of a special file I want vim to automatically highlight all the lines, where the 7th character is a 1. How do I do that?
Asked
Active
Viewed 298 times
1 Answers
5
First you probably want to create your own syntax file, you can read :h mysyntaxfile for more details
$ mkdir -p ~/.vim/syntax
Then you want to define your own filetype for the file you are editing, you could do that with a modeline (:h modeline) e.g. adding this line as the first or last line of your file:
vim: ft=myownfiletype
If the file already has a filetype you can set several ones, for example if this is a C file:
//vim: ft=c.myownfiletype
If you can't add a modeline to your file you can use other ways to define a file type (see :h ftdetect or this link)
Then you need to create the syntax file for this new filetype (see :h :syn-files):
$ touch ~/.vim/syntax/myownfiletype.vim
Finally in this file you can put your commands, a basic example would be:
syntax match Error /^.\{6}1/
Which means:
syntax matchcreate a new highlighting for line which will match what follows (:h :syn-match)ErrorUse theErrorhighlighting group. Note that you could use another one or define your own one (see:h highlight-groups)/^.\{6}1/match the lines beginning (^) with 6 characters.\{6}followed by1
statox
- 49,782
- 19
- 148
- 225
/^.\{6}1.*/maybe? – statox Oct 16 '18 at 10:01set syntax=cin Ex mode currently, since the file isn't a c-file. How can this be run automatically? – quiliup Oct 16 '18 at 10:10