8

I've noticed that git seems to use different vim settings any time I'm writing a commit message. I have the git+svn install off Macports, and I've checked the $MYVIMRC variable: it's set to the correct file. Still, every time I go to commit a message I have a restriction on 80 characters per line, case sensitive search, and none of the plugins I've installed.

It's probably something silly. Would appreciate a pointer as to what it is.

EDIT: Actually I just checked: my plugins work. It's only the column width of 80 chars that miraculously comes alive when I type out commit messages.

Andres Riofrio
  • 9,851
  • 7
  • 40
  • 60
dmkc
  • 1,161
  • 1
  • 15
  • 22
  • Are you sure your editor is set to Vim? Git could be using a different Vi implementation. – strager Aug 11 '10 at 15:03
  • Actually I just checked: my plugins work. It's only the column width of 80 chars that miraculously comes alive when I type out commit messages. And yes, my core.editor is the same vim I'm using for everything else. – dmkc Aug 11 '10 at 15:10
  • 2
    This does *not* belong on superuser. Read the FAQ: SO is the place for questions about "software tools commonly used by programmers". Who commonly uses git and vim? – Cascabel Aug 11 '10 at 15:23
  • @mitjak: Sorry, that was directed at the mod who voted to migrate this to superuser. Forgot you can't see that! – Cascabel Aug 11 '10 at 15:27
  • Thanks for the responses! You guys are all correct! I don't know which answer to mark heh. – dmkc Aug 11 '10 at 15:28
  • @mitjak: Completely objectively of course, I'd say jamessan's or mine. Thomas missed the ftplugin, jamessan was first to guess the exact cause, and I know why the ftplugin does what it does. – Cascabel Aug 11 '10 at 15:31
  • As I stated over at http://stackoverflow.com/questions/8710285/git-commit-error-saving-vimfiles - if you're using a locally compiled vim by default, git may not be using it. Check that the `:version` is the same on both. You can force git yo use a specific version with something like `git config --global core.editor '~/bin/vim'` – naught101 Jun 26 '12 at 05:05

4 Answers4

15

That's not a bug, it's a feature!

Vim knows about a lot of filetypes - including git commits (and interactive rebases, and config...). There are syntax definitions and ftplugins (filetype-activated plugins) for each of these. One of the settings in the commit ftplugin is textwidth=72. This is done so that the output of git log will look good in a standard-width terminal. If you really want to change it, you could go edit the plugin, but I'd really recommend keeping it.

The plugin should be in <vim-directory>/vimXX/ftplugin/gitcommit.vim. The XX is the version number, e.g. 72 for version 7.2, and the leading component is generally something like /usr/share/vim.

P.S. The plugin also defines a command DiffGitCached, which will open the diff to be committed in a preview window. Handy!

Cascabel
  • 479,068
  • 72
  • 370
  • 318
  • 4
    If you want to override the setting, then you should do so in `~/.vim/after/ftplugin/gitcommit.vim`, not by editing the system file. See http://vimdoc.sourceforge.net/htmldoc/options.html#after-directory – jamessan Aug 11 '10 at 15:57
  • @jamessan: True. Fortunately in this case you don't have to worry about figuring out what value to restore the option to, as the ftplugin only modifies it if it's set to zero. (I hadn't read the plugin carefully enough to notice that the first time, and thought there'd be no way to restore.) – Cascabel Aug 11 '10 at 16:01
  • @Jefromi: What would need to be restored? – jamessan Aug 11 '10 at 16:12
  • @jamessan: It's not a boolean. Suppose you set it to 100, and the plugin sets it to 72. How would would the after ftplugin know what to set it back to? (But like I said, not a problem in this case.) – Cascabel Aug 11 '10 at 16:14
  • @jamessan Strange: http://funkyimg.com/u2/405/385/Screen_shot_2010-08-11_at_12_18_13_PM.png – dmkc Aug 11 '10 at 16:19
  • The ftplugin doesn't have to set it back to anything. Since it's properly using `setlocal` instead of `set` and `'textwidth'` is a buffer-local option, the change only affects the current buffer. There's also the convention of setting up an "undo" string that will be evaluated when undoing changing filetype. Plus, the whole point of the after directory is to override particular settings that are being made in the system runtime files. – jamessan Aug 11 '10 at 16:25
  • @jamessan: I know that's the point of it. It's just awkward to have to maintain two copies of a setting, one in your vimrc and one as an override. The point here is that you want to override it to the *same* thing as it was previously set to by your vimrc. (But again, it's moot in this case, because the only thing it could've been was 0.) – Cascabel Aug 11 '10 at 17:08
  • Ah, I explicitly don't set `textwidth` in my vimrc because of this behavior. Doing so precludes, e.g., the mail ftplugin from setting `textwidth` to 72 since it would defer to the setting from the vimrc. Instead, I put filetype/project-specific `textwidth` settings in the relevant after/ftplugin or project autocmd. – jamessan Aug 11 '10 at 17:22
  • @jamessan: Yeah, that's a good approach. I doubt there are a ton of people who do explicitly set it in their vimrc, but I'm just obsessive about covering all of the cases. – Cascabel Aug 11 '10 at 17:27
4

:verbose set textwidth? formatoptions? will tell you the values of these option and what script last set them. Text is only hard-wrapped while typing if 'textwidth' is non-zero and 'formatoptions' contains the t setting. It's likely that the gitcommit filetype plugin (ftplugin/gitcommit.vim) is changing one or both of these options because you have filetype plugins enabled (:filetype shows plugin:ON).

jamessan
  • 41,569
  • 8
  • 85
  • 85
1

Partial answer, maybe helpful...

According to ps aux, git starts vim with this command:

vim .git/COMMIT_EDITMSG

This triggers the syntax mode gitcommit, which on my Ubuntu system lives in

/usr/share/vim/vimcurrent/syntax/gitcommit.vim

and is loaded from

/usr/share/vim/vimcurrent/filetype.vim
Thomas
  • 174,939
  • 50
  • 355
  • 478
1

FWIW, I had a similar problem -- Vim was disabling my textwidth setting in .txt files because one of the filetype plugins being called had the line "formatoptions -=t"

Instead of editing the plugins (which made me uncomfortable), I circumvented the problem by adding the line ":filetype plugins off" to my ~/.vimrc file. The solution was detailed here - http://peox.net/articles/vimconfig.html

(I originally thought the problem was git-related, but after testing in a non-git directory I found it was due to the .txt file extension.)

Cody Hess
  • 1,777
  • 15
  • 19