Alex Hirzel
Alex Hirzel

Reputation: 1814

Setting Vim options depending on file name

Pentadactyl allows you to use an external text editor for <textarea> and <input type="text"> elements on websites. I use GVim for this. This functionality is very useful but drives me to a question. I frequently want to wrap the text after editing, so my workflow is as follows:

  1. CTRL+I to launch GVim
  2. Use :set tw=72
  3. Perform my text manipulation
  4. Use gq to re-wrap areas that require it
  5. :wq

I would like to skip step #2 without affecting other editing in [G]Vim (i.e. I don't want to outright set tw=72 in .[g]vimrc for all files). What is the best way to :set tw=72 in this specific circumstance on Linux for example, where Pentadactyl always launches $EDITOR on /tmp/pentadactyl.txt?

Upvotes: 4

Views: 998

Answers (1)

pb2q
pb2q

Reputation: 59617

Add an autocmd for this file to your .vimrc:

autocmd BufRead  /tmp/pentadactyl.txt  setlocal tw=72

This adds a hook that automatically does setlocal tw=72 whenever you're starting to edit a buffer after reading the given filename.

See :help autocmd and :help autocmd-events for more information.

Similarly, if your step #4 is predictable, i.e. you're always reformatting the entire file, you could add an autocmd for BufWrite.

Upvotes: 7

Related Questions