14

I want all of my files ending in .tex to have line length 80. So whenever I open a .tex file in vim I type :set tw=79. What would I put in my .vimrc to automate this?

What if I wanted it to work with a specific list of filetypes, say, .md, .txt, and .tex?

2 Answers 2

13

Instead of checking the extension and setting the textwidth accordingly you could simply create a tex.vim (the name must match the filetype) file in .vim/ftplugin/ or for Windows $HOME/vimfiles/ftplugin/ and set textwidth there. This will help you to keep your .vimrc clean.

Your .vim/ftplugin/tex.vim file would look like:

setlocal textwidth=79

As 8bittree said in the comments you also have to enable filetype plugins in your vimrc (I woudln't know why anybody would not enable it):

filetype plugin on

Read this for more information: http://vim.wikia.com/wiki/Keep_your_vimrc_file_clean

1
  • 2
    You'll also want to make sure filetype plugins are enabled in your .vimrc with filetype plugin on.
    – 8bittree
    Oct 19, 2015 at 15:54
12

In ~/.vimrc you can add the following line:

au BufReadPost,BufNewFile *.md,*.txt,*.tex setlocal tw=79

For extra commands, separate them by |.

This will set your settings right after reading the file, or opening a new file into the buffer for specific extensions.

1
  • 2
    set can take any number of settings. Something like set tw=79 nowrap formatoptions+=j will work. No need for a | (unless you want to call a different command, of course) ;-) Oct 19, 2015 at 19:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.