1

My thesis is made up of multiple tex files and I'm constantly flicking between them so I created a shell script, load_files.sh:

#!/bin/bash

vim file1.tex file2.txt, ..., fileN.tex

This works fine but unfortunately because of the complex multi-file structure VIM's spell check doesn't work properly. I found that the answer from @Dominik solved the problem (Vim spellcheck not always working in .tex file. Check region in Vim ). However I don't want to run that command manually everytime I open the files and I don't want to put it into my tex.vim file because it is only my thesis that has this problem. So I tried to use the -c and the + flag in the shell script but it doesn't work (Vim - how to run a command immediately when starting vim? and How can I open vim with a particular line number at the top?) but it doesn't work. Here's what I've changed the vim line ni the shell script to:

  • vim +"syntax spell toplevel" file1.tex file2.txt, ..., fileN.tex
  • vim -c "syntax spell toplevel" file1.tex file2.txt, ..., fileN.tex

They open the files fine but when I turn the spell checker on the problem isn't fixed. FYI, if I open the files normally and run :syntax spell toplevel then the spell checker works.

Here is my response to @Conner because it is too long for a comment:

Thank you @Conner for the help. Are you saying that when I use the -c flag the command is only run on the first file and not all the files? This is definitely the problem but the solutions you give aren't able to help.

  • I stated in the question that I only wnted this change made to my thesis tex files and so did not want add the command to tex.vim.
  • If I create a local vimrc, say .thesis_vimrc and use the -u flag then if I want my plugins then I need to copy ~/.vimrc to .thesis_vimrc and then add syntax spell toplevel to it. Unfortunately Pathogen stops working (presumably because it needs all the plugins in that directory but I don't want to have multiple copies of all my plugins). I tried to source ~/.vimrc within .thesis_vimrc with so ~/.vimrc but I get the same Pathogen error.

  • I tried opening all the files and changing the spelling using syntax spell toplevel and then saved a new session using mksession. When I open the saved session the syntax spell toplevel is not changed.

  • Finally I tried to use modelines to adding set modeline and set modelines=1 to ~/.vimrc and add % vim: syntax spell toplevel to the first line of the files but then when I open those files, go to the buffers and turn on the spell checker I get the following error:

    syntax=tex Error detected while processing modelines: line 1: E518: Unknown option: toplevel

Surely it can't be this complicated, can anyone help please?

ojunk
  • 879
  • 8
  • 21
  • 1
    +1 for using vim to write your thesis! Check out pandoc and markdown if you'd like to have LaTeX options/output without writing in it. – Conner Dec 05 '18 at 17:16

1 Answers1

3

Using vim -c 'text here' does work for multi-word commands.

If you read the post you linked to you'll see:

enter image description here

which instructs you to write the command in your ~/.vim/after/syntax/tex.vim file. This way you don't have to execute it manually.

This can be achieved as simply as typing

echo "syntax spell toplevel" >> ~/.vim/after/syntax/tex.vim

If you read :help -c you'll notice:

-c {command} {command} will be executed after the first file has been read

There are a couple of alternative options you have here. If you only want these options for a specific project you could make a custom .vimrc such as .myprojectvimrc and specify it with -u. See :help -u.

You could also use vim sessions. See :help mksession and :help -S.

You could also use a modeline in your files. See :help modeline.

As a side note, instead of you using a script for your vim instantiation you could just type vim file*.tex.

Conner
  • 30,144
  • 8
  • 52
  • 73
  • A useful plugin for per-project vimrc files: https://github.com/embear/vim-localvimrc – SpoonMeiser Dec 05 '18 at 17:07
  • I'd totally use modeline for this. – Amadan Dec 06 '18 at 04:37
  • Modelines only support built-in options, not arbitrary Vim commands (like `:syntax spell`), for security reasons. There are plugins that extend modelines in this way, though. – Ingo Karkat Dec 06 '18 at 09:05
  • And with local vimrc plugins, it's not clear whether the command is issued in the correct sequence; i.e. after the tex syntax. So, your first recommendation is spot-on, the alternatives not. – Ingo Karkat Dec 06 '18 at 10:05