ojunk
ojunk

Reputation: 899

VIM: Perform a multi-word command when opening multiple files

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:

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.

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

Upvotes: 1

Views: 196

Answers (1)

Conner
Conner

Reputation: 31040

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.

Upvotes: 3

Related Questions