VIM 编辑器:文件编码和行尾

0 linux vim textmate

保存文件时,这些是我在 TextMate (Mac OS X) 中的默认设置:

文件编码:UTF8(推荐)行尾:LF(推荐)

如何设置 VIM 以保存与 TextMate 具有相同文件编码和行尾的文件?我将不胜感激任何建议。谢谢!

Ale*_*aev 5

" 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
Run Code Online (Sandbox Code Playgroud)

注意:最后一行的优点是两种格式都在 Vim 缓冲区中正确显示。例如,如果您删除dosfrom fileformats,那么dos从现在起您将在 Vim 中打开的所有文件都将^M在行尾处被符号弄得乱七八糟。这^M不是别的,但\r在这种情况下,Vim 将无法正确解释。因此,强烈建议保持fileformats如上图所示。不用担心,您创建的任何新文件都将unix默认使用格式(如上面的评论中所述)。

如果您遇到一些具有dos格式的文件并想将其转换为unix.

:set ff=unix
Run Code Online (Sandbox Code Playgroud)