I program in both C and Lua and therefore sometimes add a semicolmn in my Lua files. This is allowed by the standard, but my company's guidelines forbid semicolmns in lua code. Therefore, I would like vim to highlight any semicolmn it finds in a lua file. To achieve this, I tried adding the following to my .vimrc:

if _curfile =~ ".*\.lua"
    syntax match semicolmn ";"
    highlight semicolmn guibg=red ctermbg=1

But this has some very weird effects: it does not actually work on any Lua file until I run


After which it works, but only for the current tab. So I suspect that the entire .vimrc file might be broken, this is it:

let _curfile = expand("%:t")
if _curfile =~ "Makefile" || _curfile =~ "makefile" || _curfile =~ ".*\.mk" || _curfile =~ ".*\.mcrl2"
    set noexpandtab
    set expandtab

if _curfile =~ ".*\.vhd"
    let g:vhdl_indent_genportmap = 0
    set tabstop=2
    set shiftwidth=2
    set softtabstop=2
    set tabstop=4
    set shiftwidth=4
    set softtabstop=4

if _curfile =~ ".*\.tex"
    set tw=120

colorscheme molokai
set shiftround
set autoread
set mouse=a
set clipboard=unnamedplus
set scrolloff=5
let g:rehash256 = 1
highlight Cursor guifg=white guibg=blue

set backspace=indent,eol,start

syntax on
set hlsearch

" Special save hotkey
noremap <Leader>s :update<CR>
" Remove whitespace
noremap <silent> <F5> :let _s=@/<Bar>:%s/\s\+$//e<Bar>:let @/=_s<Bar>:nohl<CR>
" Fixes copy paste behaviour for gvim
vmap <C-c> "+y
vmap <C-x> "+c
vmap <C-v> c<ESC>"+p
imap <C-v> <C-r><C-o>+

set nocompatible              " be iMproved, required
filetype off                  " required

" set the runtime path to include Vundle and initialize
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
" alternatively, pass a path where Vundle should install plugins
"call vundle#begin('~/some/path/here')

" let Vundle manage Vundle, required
Plugin 'gmarik/Vundle.vim'
Plugin 'bling/vim-airline'

let g:syntastic_vhdl_checkers = ['vimhdl']

call vundle#end()            " required
filetype plugin indent on    " required

au BufRead,BufNewFile *.S set filetype=armasm
au BufRead,BufNewFile *.h set filetype=c
cnoreabbrev X x
set keymodel=startsel

if _curfile =~ ".*\.lua"
    syntax match semicolmn ";"
    highlight semicolmn guibg=red ctermbg=1

So, the actual question is: how do I modify my .vimrc so that any semicolmn in a lue file gets highlighted?

  • In addition to the duplicate target, you could never check things like the current file name from the vimrc file, as that will be loaded once only on startup, and not when loading new buffers. You should always put stuff like this in an autocmd or after directory (see other question for details), so you'll also want to change that if _curfile =~ ".*\.vhd" and if _curfile =~ ".*\.tex" to an autocmd. Aug 22 '18 at 10:50
  • You also want to use setlocal instead of set by the way, otherwise it'll affect all buffers, and not just the vhd/tex buffer. There's got to be a "how do I apply settings for a specific filetype?" kind of question already, but I can't seem to find it right now :-( Aug 22 '18 at 10:55
  • @MartinTournoij But still, the Makefile and vhd commands work perfectly, even when opening new tabs. Did you expect those to fail too?
    – Cheiron
    Aug 22 '18 at 11:39

Browse other questions tagged or ask your own question.