2

When I'm writing Latex files in vim, the syntax highlighting recognizes \newcommand as the start of a new command environment, but not \newcommand*. Digging into the syntax file, it seems that to fix this I want to replace the line

syn match texNewCmd "\\newcommand\>" nextgroup=texCmdName skipwhite skipnl

with

syn match texNewCmd "\\newcommand\*\=\>" nextgroup=texCmdName skipwhite skipnl

I'd prefer it if rather than editing the existing syntax file directly, I could include this change in a separate file. I read that by adding a separate tex.vim file in the ~/.vim/syntax or ~/.vim/after/syntax folder I can include small changes to the syntax highlighting, but that doesn't seem to be working. Is there a way to change what this group is matched to without editing it directly in the included syntax file?

2

Your attempt doesn't work because \\newcommand\*\=\> can never match the text \newcommand*. \> denotes a word right boundary but * is not a word character.

Put this in ~/.vim/after/syntax/tex.vim:

syn match texNewCmd "\\newcommand\*" nextgroup=texCmdName skipwhite skipnl
  • Thanks, that worked like a charm! Just to clarify, does this cause vim to match \\newcommand\* in addition to the original \\newcommand\>, or does it replace it and have it only match \\newcommand\*? – Carmeister Feb 26 '18 at 21:30
  • If I understand you correctly- both \newcommand{ and \newcommand*{ will work properly after my answer. If you are asking about potential conflicts, "When multiple Match or Region items start in the same position, the item defined last has priority" so your customization doesn't replace the existing match per se but vim won't match the default regex after your custom regex in that particular location – Mass Feb 26 '18 at 22:57

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.