Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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!

share|improve this question

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

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

2 Answers 2

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

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

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

" 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
share|improve this answer
    
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. –  Haroogan 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. –  Haroogan May 7 '13 at 22:44
    
Thank you so much for all your help! –  Kiraly Zoltan May 7 '13 at 22:47

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
share|improve this answer
    
Thank you, your time is greatly appreciated! –  Kiraly Zoltan May 7 '13 at 22:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.