When saving files, these are my default settings in TextMate (Mac OS X):

File Encoding: UTF8 (recommended) Line Endings: LF (recommended)

How can I set VIM to save files with the same file encoding and line endings as TextMate? I would be grateful for any suggestion. Thank you!

migrated from stackoverflow.com May 8 '13 at 2:08

This question came from our site for professional and enthusiast programmers.

" Stick with the UTF-8 encoding.
if has('multi_byte')
  " Encoding used for the terminal.
  if empty(&termencoding)
    let &termencoding = &encoding

  " Encoding used in buffers, registers, strings in expressions, "viminfo"
  " file, etc.
  set encoding=utf-8

  " Encoding used for writing files.
  setglobal fileencoding=utf-8

" Use both Unix and DOS file formats, but favor the Unix one for new files.
set fileformats=unix,dos

NOTE: The merit of the last line is that both formats are displayed correctly in Vim buffer. For example, if you'd remove dos from fileformats, then all the dos files that you would open in Vim from now on would be cluttered with ^M symbols at line endings. This ^M is nothing else, but \r which Vim, in this case, would fail to interpret properly. Therefore, it is strongly recommended to keep fileformats as shown above. Don't worry, any new files that you create will be using unix format by default (as stated in the comment above).

If you encounter some file with dos format and want to convert it into unix, then type the following:

:set ff=unix
  • Thanks, but what does that IF condition do? is it necessary? – Kiraly Zoltan May 7 '13 at 22:01
  • It is checking whether Vim was compiled with multibyte feature (which includes Unicode support and more). You can type :version and search for +multi_byte. If it is present there, it means that Vim was compiled with multibyte feature, and this check will certainly pass. On the other hand, if you see -multi_byte there, it means the opposite (Unicode is not supported), and accordingly this check won't pass, which in turn prevents Vim from crashing/barking when loading your .vimrc. – Alexander Shukaev May 7 '13 at 22:19
  • Yes, I have that option (+multi_byte). Is it recommended to set both encoding and fileencoding to utf-8? I don't really understand the difference between them. When I create a file which one is used? And which one when I save it. Thank you! – Kiraly Zoltan May 7 '13 at 22:34
  • Short comments already give you quite some information on the actual purpose of both encoding and fileencoding. I have no intention to rewrite Vim documentation here. :h encoding and :h fileencoding are your friends. – Alexander Shukaev May 7 '13 at 22:44

The line endings are determined by the 'fileformat' (actually used) and 'fileformats' (what gets detected) options. If there's unix in there, you're fine; that represents <LF>. You can use :help on any of these to read more.

Your 'encoding' should be set to utf-8, and that value should also appear in 'fileencodings' (it does by default).

You can check the settings with (note the tailing ? after the option name):

:set encoding?

To make permanent changes, put the :set ... commands into your ~/.vimrc.

Note that you can also always override the settings, e.g.

:edit ++ff=unix ++enc=utf-8 myfile

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy