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?


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 match create a new highlighting for line which will match what follows (:h :syn-match)
  • Error Use the Error highlighting 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 by 1
| improve this answer | |
  • Despite the missing ^ this highlights only the beginning of the line. – quiliup Oct 16 '18 at 9:56
  • @quiliup /^.\{6}1.*/ maybe? – statox Oct 16 '18 at 10:01
  • yes, thank you! The next problem in your answer is that this affects all c-files. How does the highlighting only affect the certain file (/path/to/file)? I also need to type set syntax=c in Ex mode currently, since the file isn't a c-file. How can this be run automatically? – quiliup Oct 16 '18 at 10:10
  • @quiliup I misread your question, I edited the answer to match your need :) – statox Oct 16 '18 at 11:30
  • 1
    Great! That's exactly what I want. Thank you very much! – quiliup Oct 17 '18 at 16:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.