kirill_igum
kirill_igum

Reputation: 3983

call a function in vim when opening a file with a specific name ignoring directories (regex as a variable)

i'm trying to make vim check for the filename of the file it's about to open

if @%== .\+"notes.tex"
  highlight done ctermbg=blue ctermfg=white guibg=#292960 guifg=#AAAAAA
  match done /.\+itemize.\+/
endif

i'd like this script work on the file notes.tex regardless of the directory. the reason i pu .+ before notes is because i want to match all the preceeding characterrs in the filename

in other words i want if to match "notes.tex" and "../notes.tex"

Upvotes: 2

Views: 166

Answers (1)

Prince Goulash
Prince Goulash

Reputation: 15715

I think you'd be better of using the expand("%") function to read the filename, and then using matchstr() to check it:

if matchstr(expand("%"),"notes\.tex$") != ""
  highlight done ctermbg=blue ctermfg=white guibg=#292960 guifg=#AAAAAA
  match done /.\+itemize.\+/
endif

Note the $ in the matchstr statement: so this only matches "notes.tex" if it is at the very end of the string.

The nice this about this approach is that it doesn't care about the slash direction (\ or /) and therefore should be platform independent.

Hope this helps.

Upvotes: 1

Related Questions