8

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?

10

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

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

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.

  • 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) ;-) – Martin Tournoij Oct 19 '15 at 19:38

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.