88

Using Git, my editor for commits is Vim. I'm constantly doing a poor job of remembering to wrap lines and so I get long lines (see this answer for example). Is there a way using some git config or .vimrc magic that I can get Vim to automatically wrap lines at 72 columns?

Related question.

5
  • The question you link to suggests setting the 'textwidth' option in your .vimrc. Does that not meet your needs?
    – Josh Lee
    Jun 13, 2012 at 21:01
  • Perhaps stackoverflow.com/questions/5602905/auto-wrap-long-lines-in-vim is what you're looking for
    – Rem.co
    Jun 13, 2012 at 21:01
  • @JoshLee -- I was under the impression that would globally set the maximum width. I only want to set the width when in a git-commit message.
    – mgilson
    Jun 13, 2012 at 21:12
  • @RemcoOverdijk -- Ironically enough, that was the link I tried to post as a related question. I don't know how I got the other link (actually, when I followed it was the first time I remember seeing it)... Anyway, to be clear I only want to force wrapping when in a git commit and I'm not an expert in .vimrc formatting (It all looks like a foreign language to me).
    – mgilson
    Jun 13, 2012 at 21:16
  • Vim unintentionally taught me about proper commit message formatting practices via its syntax highlighting. Linking this here: Git Commit Messages: 50/72 Formatting.
    – joeljpa
    Nov 10 at 12:19

7 Answers 7

110

Add this to your .vimrc:

au FileType gitcommit setlocal tw=72
7
  • 16
    That setting should already exist in .../vim73/ftplugin/gitcommit.vim unless you've set your own textwidth.
    – idbrii
    Aug 2, 2012 at 0:49
  • 1
    This only works if formatoption t is set. Use :set fo? to check an :set fo+=t to correct.
    – Henning
    Mar 19, 2016 at 23:54
  • 1
    To use this solution, you will need :filetype on in your .vimrc too. This enables vim to check filetypes and see you are editting a gitcommit file in this situation. @Henning is also correct you will need to have a fo with t included, to wrap on textwidth. However, as I understand it this is default—but still a point of failure.
    – Novice C
    Aug 28, 2016 at 1:16
  • 16
    I had to say filetype plugin on in my .vimrc Sep 28, 2016 at 19:29
  • 2
    This did not work for me. Mar 16, 2022 at 22:14
29

Add this to your .vimrc:

filetype indent plugin on

From here.

4
  • 1
    This is the only thing that works for me, Vim 7.3 on os x 10.9 May 13, 2014 at 16:48
  • Only thing that worked for my on Vim 7.4, OSX Sierra Feb 20, 2017 at 19:17
  • This worked for me immediately on VI improved 8.2 on opensuse tumbleweed Feb 15, 2022 at 7:44
  • 1
    Just to explain this answer a little bit: indent and plugin relates to filetype setting, which is turned on. vim with filetype on detects filetype and can upload some specific settings to it (like indent configurations and plugins for that filetype). vim is shipped with such git helpers since some version. For more info execute :help :filetype-overview. PS. syntax on option may not be set by default (eg. to colorize message title with limit); adding it in .vimrc won't hurt.
    – amordo
    Nov 24, 2022 at 9:48
25

While the other answers solve this problem perfectly well, I highly recommend you install Tim Pope's fugitive.vim.

fugitive.vim is an excellent plugin that brings Git functionality to Vim. It has a whole host of features that aren't relevant to your question, but are worth checking out. However, it can also help you remember to keep your commit messages the proper length:

Sample commit message within Vim on fugitive

2
  • 5
    Git/vim appear to do the syntax highlighting without any plugin for me. Mar 13, 2015 at 11:28
  • 1
    I have this installed but it doesn't wrap in 72 characters the commit message Nov 13, 2019 at 20:00
16

2018 Update - Update vim

If you update vim, it will automatically highlight the first 50 characters of your title and wrap lines at 72 characters. It knows that you're editing a git commit file.


Mac users can use Homebrew:

brew install vim

If you still aren't seeing the syntax highlighting, make sure you have it enabled:

You need to have following settings in .vimrc file as per arch linux documentation

filetype plugin on
syntax on
1
  • I use vim v8.2 (macos) and it has no wrapping git message body lines feature. It only highlights the title and leaves the body text without anything. It's needed to explicitly use filetype indent plugin on (syntax on won't be redundant too) setting in .vimrc.
    – amordo
    Nov 24, 2022 at 9:53
4

In addition to other answers, use gqip to reformat a paragraph while editing.

See Vim Tips Wiki: Automatic formatting of paragraphs

2
  • this is an immensely useful tip! Thank you!
    – A. Blesius
    Sep 22, 2020 at 9:25
  • The top two answers did not work for me but this did. gqq is shorter and is for just the current line.
    – joeljpa
    Nov 13 at 6:00
2

Here's a git hook for auto-wrapping that will work with any editor: https://github.com/surabhigupta/AutoWrapSeventyTwo

1

Several of the options from the earlier posts work, except I noticed inconsistencies between different systems.

Fedora 28 (recently upgraded from F26) was easy once I realised :version inside git-commit/git-tag showed it was pointing to .virc files (weird*) so I simply copied my ~/.vimrc into ~/.virc [except, see below].

macOS 10.13.4 with vim 8.0 from brew works just fine off /usr/share/vim/vim80/ftplugin/gitcommit.vim according to :verbose :set tw=?.

CentOS 7.4 with vim 7.4 (git version 1.8.3.1) for some reason though didn't appear to be making use of the textwidth line in its supplied gitcommit.vim so I went for a quick and dirty workaround (to save me from dealing with multiple files) in ~/.vimrc:

nmap <F2> :set textwidth=72<CR>
inoremap <F2> <Esc>:set textwidth=72<CR>a

That seems to work well enough and is easy to remember - I mostly only pull from there anyway and have kind of given up messing around any more with the old versions of git and vim.

Otherwise, I (temporarily) went for Chip Hogg's suggestion after Abe Voelker's answer: autocmd FileType gitcommit setlocal textwidth=72

I don't think it makes much of a difference inside git-commit but it's possibly better to be safe (especially if that line ends up getting copied throughout a vimrc). Filetype is of course set to on, as it is in many example vimrcs.

* However, I was still curious as to why vim's ruler wasn't showing, so I looked at :help ruler which informs +cmdline_info (displayed after :version) needs to be set at compile time. Running :ver in vim outside of git-commit revealed different settings and a different compiled time, suggesting that git was perhaps calling the system copy of vim instead of the user one.

So what I should have done at the beginning to all this was run git config --global core.editor "vim" except I didn't because I had assumed it was a redundant step. Doing this first on every git installation might save a lot of hassle from the start!

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.