1

Preface: I enjoy using vim, but am by no means a vim power user. My .vimrc file is largely the result of googling other's solutions and adapting them to my needs.

With that in mind, I'm curious if it's possible to have the .vimrc file set the values for g:Tex_SmartQuoteOpen and g:Tex_SmartQuoteClose based on the presence or absence of a specific keyword or pattern in the file that is being opened.

Context: Most of my .vimrc edits are related to making vim and the vim-latex plugin behave in a way that makes my life easier. The default setting for SmartQuotes within vim-latex is to set the open quote to `` and the close quote to ''. However, when I'm writing .tex files in languages other than english (particular when writing multi-lingual documents) I need to use the csquotes package so that each language gets the correct style of quotes (high vs low vs guillemets, etc.) Thus, my .vimrc currently defines

let g:Tex_SmartQuoteOpen="\\enquote\{"
let g:Tex_SmartQuoteClose="\}"

But, this is a bit heavy handed as I don't always use the csquotes package. I'm sure there must be a way to have vim do a quick search of the document being opened to look for the existence of csquotes (or, more accurately, a regex-like expression along the lines of

\\usepackage.\{-}{csquotes}

and then assign the SmartQuote variables if the search is successful. Is this possible or is it too complex for a simple .vimrc customization?

1

It's possible and not very complex, but it needs to be done at the right time and doing it in your ~/.vimrc is too early--the .tex file has not been loaded yet so it can't be searched.

I think the place for this sort of customization is in your ~/.vim/ftplugin/tex.vim file, as described in ~/.vim/ftplugin/latex-suite/texrc. You might be able to do it from your ~/.vimrc by employing autocommands, but I'm not sure how to do that so that your customization is done after the .tex file is loaded but before the g:Tex_SmartQuoteOpen and g:Tex_SmartQuoteClose variables are read by the latex-suite files.

I think also that it would be better to set the buffer-local versions of those variables, b:Tex_SmartQuoteOpen and b:Tex_SmartQuoteClose, so that you can apply different quotes in different buffers, should the situation ever arise.

Here is the code that should go into ~/.vim/ftplugin/tex.vim:

if search('\\usepackage.\{-}{csquotes}', 'wn')
    let b:Tex_SmartQuoteOpen="\\enquote\{"
    let b:Tex_SmartQuoteClose="\}"
endif
  • I've tried this suggestion (and several variations) but it's not working. However, it may be due to my lack of experience... In any event, I'm getting the default vim-latex `` and '' SmartQuotes. I've tried looking only for csquotes in the search string as well as setting the global instead of the buffer values (g:Tex_SmartQuoteOpen). Is there some way that vim-latex is running something after tex.vim that's overriding these? Sadly, I'm not familiar at all with the behind-the-scenes magic that vim-latex uses when loading a file... – S. Burt Oct 26 '15 at 19:58
  • I should point out that I know the tex.vim file is being used/parsed, as I have to redefine the mapping of Tex_InsertItemOnThisLine in this file in order to use e-acute and that works just fine. – S. Burt Oct 26 '15 at 20:03
  • Running grep -R Tex_Smart_QuoteOpen from the shell shows that variable being set only in the german and ngerman packages. I don't know how that might affect you. I tested that code before posting it and it worked for me. There might be an error in the call to search(). Try executing :echo search('\\usepackage.\{-}{csquotes}', 'wn') from the Vim command line while the current window contains a file that uses that package. The number echoed should be the line number of the usepackage statement. Make sure you enclose the pattern in single-quotes ('), not double-quotes ("). – garyjohn Oct 26 '15 at 20:36
  • It's not the package, but the vim-latex plugin that uses these variables. Currently, I manually set these in my .vimrc file (except, of course, when testing the solution above) and it works correctly. Using echo, I got the correct line number but get the default `` '' quotes... – S. Burt Oct 26 '15 at 20:57
  • What I meant by those being set in packages was these results from running grep -R Tex_SmartQuoteOpen in my ~/.vim directory: vim-latex/ftplugin/latex-suite/packages/german:let b:Tex_SmartQuoteOpen = '"'` and vim-latex/ftplugin/latex-suite/packages/ngerman:let b:Tex_SmartQuoteOpen = '"'`. – garyjohn Oct 26 '15 at 22:04

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.