Search code examples
gitgovimfile-format

Line-endings in go-language source files using vim and git on MS-Windows


The situation

I am writing go language applications on Windows 10. I use vim to edit my source files. I use git for version control.

The go language comes with some unusually rigid prescriptions for source file formatting. I have decided my life will be easier if I go along with this.

The problem

the go fmt command is useful for sorting imports, lining up columns and other things. I'm inclined to use it prior to checkin and at other times.

The go fmt command changes line endings to lf. This causes both git and vim to issue warnings.

My solution?

Moved into an "Answer" after 3 months because no other answers appeared and it's probably better for other people with a similar problem to see in a search result that this question has 1 Answer rather than 0 Answers

My question.

Is my line-ending solution optimal or have I missed something that may bite me later?


Solution

  • My solution.

    To eliminate the warnings I configured vim and git to work the way golang likes.

    git

    The following command stops git from trying to do what is normally the right thing: standard line-endings in repo, platform line-endings on each developers working directory, convert as needed.

    git config core.autocrlf false
    

    Now git won't change lf to crlf on checkout or bleat about line-endings.

    vim

    In _vimrc

    au FileType go setl ts=3 sw=3 nowrap nu syntax=go ruler fileformat=unix
    

    The fileformat=unix seems to keep vim complaint-free regarding line-endings that are not native to the platform.


    Footnote

    3 months after posting the above question I haven't come across any drawbacks or problems - at least not the way I use go, vim and git.