Vim on Linux 保存以 CRLF 结尾的文件

Vim on Linux saves files with CRLF ending

我已经使用 vim 一段时间了,这是我第一次遇到这个问题。

当我打开 vim 时它抱怨我的 vimrc 文件中有 CRLF 结尾(错误:不是编辑器命令 ^M)但是当我使用 [=57= 打开文件时] 我在任何地方都看不到 ^M。 fileformat 设置为 unixffsunix,dos

这是一张截图(抱歉,我刚刚创建了我的帐户,所以我不能 post 截图): http://i.stack.imgur.com/3PjM8.png

当我在 vimrc 上使用 unix2dos 并打开 vim 时,我仍然有 CRLF 错误,但这次错误明显减少了,我仍然没有看到 ^M行尾。 fileformat 设置为 dosffsunix,dos

这是一张截图(抱歉,我刚刚创建了我的帐户,所以我不能 post 截图): http://i.stack.imgur.com/c74RE.png

我尝试过使用 dos2unix 和 unix2dos,使用 vim 设置 ffffs,使用 gedit 和 Sublime Text 3,甚至让我的旧 vim来自 GitHub 的 rc 文件。我还尝试降级 vim 并在将其从我的系统中完全删除后重新安装。我在 Arch Linux(i686 gvim 包)上使用 Vim 7.4.778。

目前我的 .vimrc 是这样的:

"NeoBundle Scripts-----------------------------
if has('vim_starting')
  if &compatible
    set nocompatible               " Be iMproved
  endif

  " Required:
  set runtimepath+=~/.vim/bundle/neobundle.vim/
endif

" Required:
call neobundle#begin(expand('~/.vim/bundle'))

" Let NeoBundle manage NeoBundle
" Required:
NeoBundleFetch 'Shougo/neobundle.vim'

" Add or remove your Bundles here:
NeoBundle 'Shougo/neosnippet.vim'
NeoBundle 'Shougo/neosnippet-snippets'
NeoBundle 'tpope/vim-fugitive'
NeoBundle 'ctrlpvim/ctrlp.vim'
NeoBundle 'flazz/vim-colorschemes'

" You can specify revision/branch/tag.
NeoBundle 'Shougo/vimshell', { 'rev' : '3787e5' }

" Required:
call neobundle#end()

" Required:
filetype plugin indent on

" If there are uninstalled bundles found on startup,
" this will conveniently prompt you to install them.
NeoBundleCheck
"End NeoBundle Scripts-------------------------

这是 od -c .vimrc 给我的:

0000000   "   N   e   o   B   u   n   d   l   e       S   c   r   i   p
0000020   t   s   -   -   -   -   -   -   -   -   -   -   -   -   -   -
0000040   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -  \n
0000060   i   f       h   a   s   (   '   v   i   m   _   s   t   a   r
0000100   t   i   n   g   '   )  \n           i   f       &   c   o   m
0000120   p   a   t   i   b   l   e  \n                   s   e   t    
0000140   n   o   c   o   m   p   a   t   i   b   l   e                
0000160                                               "       B   e    
0000200   i   M   p   r   o   v   e   d  \n           e   n   d   i   f
0000220  \n  \n           "       R   e   q   u   i   r   e   d   :  \n
0000240           s   e   t       r   u   n   t   i   m   e   p   a   t
0000260   h   +   =   /   h   o   m   e   /   a   d   m   n   u   n   p
0000300   w   n   d   /   .   v   i   m   /   b   u   n   d   l   e   /
0000320   n   e   o   b   u   n   d   l   e   .   v   i   m   /  \n   e
0000340   n   d   i   f  \n  \n   "       R   e   q   u   i   r   e   d
0000360   :  \n   c   a   l   l       n   e   o   b   u   n   d   l   e
0000400   #   b   e   g   i   n   (   e   x   p   a   n   d   (   '   /
0000420   h   o   m   e   /   a   d   m   n   u   n   p   w   n   d   /
0000440   .   v   i   m   /   b   u   n   d   l   e   '   )   )  \n  \n
0000460   "       L   e   t       N   e   o   B   u   n   d   l   e    
0000500   m   a   n   a   g   e       N   e   o   B   u   n   d   l   e
0000520  \n   "       R   e   q   u   i   r   e   d   :  \n   N   e   o
0000540   B   u   n   d   l   e   F   e   t   c   h       '   S   h   o
0000560   u   g   o   /   n   e   o   b   u   n   d   l   e   .   v   i
0000600   m   '  \n  \n   "       A   d   d       o   r       r   e   m
0000620   o   v   e       y   o   u   r       B   u   n   d   l   e   s
0000640       h   e   r   e   :  \n   N   e   o   B   u   n   d   l   e
0000660       '   S   h   o   u   g   o   /   n   e   o   s   n   i   p
0000700   p   e   t   .   v   i   m   '  \n   N   e   o   B   u   n   d
0000720   l   e       '   S   h   o   u   g   o   /   n   e   o   s   n
0000740   i   p   p   e   t   -   s   n   i   p   p   e   t   s   '  \n
0000760   N   e   o   B   u   n   d   l   e       '   t   p   o   p   e
0001000   /   v   i   m   -   f   u   g   i   t   i   v   e   '  \n   N
0001020   e   o   B   u   n   d   l   e       '   c   t   r   l   p   v
0001040   i   m   /   c   t   r   l   p   .   v   i   m   '  \n   N   e
0001060   o   B   u   n   d   l   e       '   f   l   a   z   z   /   v
0001100   i   m   -   c   o   l   o   r   s   c   h   e   m   e   s   '
0001120  \n  \n   "       Y   o   u       c   a   n       s   p   e   c
0001140   i   f   y       r   e   v   i   s   i   o   n   /   b   r   a
0001160   n   c   h   /   t   a   g   .  \n   N   e   o   B   u   n   d
0001200   l   e       '   S   h   o   u   g   o   /   v   i   m   s   h
0001220   e   l   l   '   ,       {       '   r   e   v   '       :    
0001240   '   3   7   8   7   e   5   '       }  \n  \n   "       R   e
0001260   q   u   i   r   e   d   :  \n   c   a   l   l       n   e   o
0001300   b   u   n   d   l   e   #   e   n   d   (   )  \n  \n   "    
0001320   R   e   q   u   i   r   e   d   :  \n   f   i   l   e   t   y
0001340   p   e       p   l   u   g   i   n       i   n   d   e   n   t
0001360       o   n  \n  \n   "       I   f       t   h   e   r   e    
0001400   a   r   e       u   n   i   n   s   t   a   l   l   e   d    
0001420   b   u   n   d   l   e   s       f   o   u   n   d       o   n
0001440       s   t   a   r   t   u   p   ,  \n   "       t   h   i   s
0001460       w   i   l   l       c   o   n   v   e   n   i   e   n   t
0001500   l   y       p   r   o   m   p   t       y   o   u       t   o
0001520       i   n   s   t   a   l   l       t   h   e   m   .  \n   N
0001540   e   o   B   u   n   d   l   e   C   h   e   c   k  \n   "   E
0001560   n   d       N   e   o   B   u   n   d   l   e       S   c   r
0001600   i   p   t   s   -   -   -   -   -   -   -   -   -   -   -   -
0001620   -   -   -   -   -   -   -   -   -   -   -   -   -  \n  \n
0001637

这是 hexdump -C .vimrc 给我的:

00000000  22 4e 65 6f 42 75 6e 64  6c 65 20 53 63 72 69 70  |"NeoBundle Scrip|
00000010  74 73 2d 2d 2d 2d 2d 2d  2d 2d 2d 2d 2d 2d 2d 2d  |ts--------------|
00000020  2d 2d 2d 2d 2d 2d 2d 2d  2d 2d 2d 2d 2d 2d 2d 0a  |---------------.|
00000030  69 66 20 68 61 73 28 27  76 69 6d 5f 73 74 61 72  |if has('vim_star|
00000040  74 69 6e 67 27 29 0a 20  20 69 66 20 26 63 6f 6d  |ting').  if &com|
00000050  70 61 74 69 62 6c 65 0a  20 20 20 20 73 65 74 20  |patible.    set |
00000060  6e 6f 63 6f 6d 70 61 74  69 62 6c 65 20 20 20 20  |nocompatible    |
00000070  20 20 20 20 20 20 20 20  20 20 20 22 20 42 65 20  |           " Be |
00000080  69 4d 70 72 6f 76 65 64  0a 20 20 65 6e 64 69 66  |iMproved.  endif|
00000090  0a 0a 20 20 22 20 52 65  71 75 69 72 65 64 3a 0a  |..  " Required:.|
000000a0  20 20 73 65 74 20 72 75  6e 74 69 6d 65 70 61 74  |  set runtimepat|
000000b0  68 2b 3d 2f 68 6f 6d 65  2f 61 64 6d 6e 75 6e 70  |h+=~|
000000c0  77 6e 64 2f 2e 76 69 6d  2f 62 75 6e 64 6c 65 2f  |wnd/.vim/bundle/|
000000d0  6e 65 6f 62 75 6e 64 6c  65 2e 76 69 6d 2f 0a 65  |neobundle.vim/.e|
000000e0  6e 64 69 66 0a 0a 22 20  52 65 71 75 69 72 65 64  |ndif.." Required|
000000f0  3a 0a 63 61 6c 6c 20 6e  65 6f 62 75 6e 64 6c 65  |:.call neobundle|
00000100  23 62 65 67 69 6e 28 65  78 70 61 6e 64 28 27 2f  |#begin(expand('/|
00000110  68 6f 6d 65 2f 61 64 6d  6e 75 6e 70 77 6e 64 2f  |~/|
00000120  2e 76 69 6d 2f 62 75 6e  64 6c 65 27 29 29 0a 0a  |.vim/bundle'))..|
00000130  22 20 4c 65 74 20 4e 65  6f 42 75 6e 64 6c 65 20  |" Let NeoBundle |
00000140  6d 61 6e 61 67 65 20 4e  65 6f 42 75 6e 64 6c 65  |manage NeoBundle|
00000150  0a 22 20 52 65 71 75 69  72 65 64 3a 0a 4e 65 6f  |." Required:.Neo|
00000160  42 75 6e 64 6c 65 46 65  74 63 68 20 27 53 68 6f  |BundleFetch 'Sho|
00000170  75 67 6f 2f 6e 65 6f 62  75 6e 64 6c 65 2e 76 69  |ugo/neobundle.vi|
00000180  6d 27 0a 0a 22 20 41 64  64 20 6f 72 20 72 65 6d  |m'.." Add or rem|
00000190  6f 76 65 20 79 6f 75 72  20 42 75 6e 64 6c 65 73  |ove your Bundles|
000001a0  20 68 65 72 65 3a 0a 4e  65 6f 42 75 6e 64 6c 65  | here:.NeoBundle|
000001b0  20 27 53 68 6f 75 67 6f  2f 6e 65 6f 73 6e 69 70  | 'Shougo/neosnip|
000001c0  70 65 74 2e 76 69 6d 27  0a 4e 65 6f 42 75 6e 64  |pet.vim'.NeoBund|
000001d0  6c 65 20 27 53 68 6f 75  67 6f 2f 6e 65 6f 73 6e  |le 'Shougo/neosn|
000001e0  69 70 70 65 74 2d 73 6e  69 70 70 65 74 73 27 0a  |ippet-snippets'.|
000001f0  4e 65 6f 42 75 6e 64 6c  65 20 27 74 70 6f 70 65  |NeoBundle 'tpope|
00000200  2f 76 69 6d 2d 66 75 67  69 74 69 76 65 27 0a 4e  |/vim-fugitive'.N|
00000210  65 6f 42 75 6e 64 6c 65  20 27 63 74 72 6c 70 76  |eoBundle 'ctrlpv|
00000220  69 6d 2f 63 74 72 6c 70  2e 76 69 6d 27 0a 4e 65  |im/ctrlp.vim'.Ne|
00000230  6f 42 75 6e 64 6c 65 20  27 66 6c 61 7a 7a 2f 76  |oBundle 'flazz/v|
00000240  69 6d 2d 63 6f 6c 6f 72  73 63 68 65 6d 65 73 27  |im-colorschemes'|
00000250  0a 0a 22 20 59 6f 75 20  63 61 6e 20 73 70 65 63  |.." You can spec|
00000260  69 66 79 20 72 65 76 69  73 69 6f 6e 2f 62 72 61  |ify revision/bra|
00000270  6e 63 68 2f 74 61 67 2e  0a 4e 65 6f 42 75 6e 64  |nch/tag..NeoBund|
00000280  6c 65 20 27 53 68 6f 75  67 6f 2f 76 69 6d 73 68  |le 'Shougo/vimsh|
00000290  65 6c 6c 27 2c 20 7b 20  27 72 65 76 27 20 3a 20  |ell', { 'rev' : |
000002a0  27 33 37 38 37 65 35 27  20 7d 0a 0a 22 20 52 65  |'3787e5' }.." Re|
000002b0  71 75 69 72 65 64 3a 0a  63 61 6c 6c 20 6e 65 6f  |quired:.call neo|
000002c0  62 75 6e 64 6c 65 23 65  6e 64 28 29 0a 0a 22 20  |bundle#end().." |
000002d0  52 65 71 75 69 72 65 64  3a 0a 66 69 6c 65 74 79  |Required:.filety|
000002e0  70 65 20 70 6c 75 67 69  6e 20 69 6e 64 65 6e 74  |pe plugin indent|
000002f0  20 6f 6e 0a 0a 22 20 49  66 20 74 68 65 72 65 20  | on.." If there |
00000300  61 72 65 20 75 6e 69 6e  73 74 61 6c 6c 65 64 20  |are uninstalled |
00000310  62 75 6e 64 6c 65 73 20  66 6f 75 6e 64 20 6f 6e  |bundles found on|
00000320  20 73 74 61 72 74 75 70  2c 0a 22 20 74 68 69 73  | startup,." this|
00000330  20 77 69 6c 6c 20 63 6f  6e 76 65 6e 69 65 6e 74  | will convenient|
00000340  6c 79 20 70 72 6f 6d 70  74 20 79 6f 75 20 74 6f  |ly prompt you to|
00000350  20 69 6e 73 74 61 6c 6c  20 74 68 65 6d 2e 0a 4e  | install them..N|
00000360  65 6f 42 75 6e 64 6c 65  43 68 65 63 6b 0a 22 45  |eoBundleCheck."E|
00000370  6e 64 20 4e 65 6f 42 75  6e 64 6c 65 20 53 63 72  |nd NeoBundle Scr|
00000380  69 70 74 73 2d 2d 2d 2d  2d 2d 2d 2d 2d 2d 2d 2d  |ipts------------|
00000390  2d 2d 2d 2d 2d 2d 2d 2d  2d 2d 2d 2d 2d 0a 0a     |-------------..|
0000039f

正在将大量评论转化为答案。

很好奇。您已经涵盖了很多基础,但显然仍然存在问题 — 如果没有,Vim 不会抱怨。您是否看过 od -c .vimrc 的输出——输出中是否出现了 \r?您是否检查过(例如,故意在顶部放置一个语法错误)Vim 正在查看您期望的文件 — 它是否正在查看与您正在检查的文件不同的文件?

  • 我观察到错误消息中的行号大多与您的 .vimrc 文件中的行号不匹配,这表明 Vim 正在查看与您的文件不同的文件正在看。

I updated my question with the outputs for od -c .vimrc & hexdump -C .vimrc.

od -c 输出不包含任何回车 returns。但是,它也不是第一组错误消息的文件。例如,您的错误消息列表第 31

E15: Invalid expression: exists('g:loaded_neobundle') &&

但是您显示的 .vimrc 中的第 31 行是

" Required:

因此,您无法通过修改 .vimrc 来解决问题的原因是导致问题的不是该文件。您需要以某种方式找出正在读取的文件(根据错误消息,一个文件有 374 行或更多行),并修复这些文件。换句话说,你找错地方了。

如果你想不出更好的方法,那就试试:

strace -o /tmp/vim.strace vim ~/.vimrc

并从 vim 退出。然后在 /tmp/vim.strace 中查找 open 调用。它成功打开的一个或多个文件可能是导致悲伤的文件。当我这样做时,通过一些仔细的 grep 调整,我得到了 /etc/vimrc/usr/share/vim/vim70/ 下的许多文件,以及 ~/.viminfo~/.vimrc.

Can you please post what grep tweaks you used? I opened /tmp/vim.strace in Sublime Text and got 305 matches for "open". Another thing is that I know the error message says "line 374" but I checked and was only able to see 38 lines in my .vimrc

我用过:

grep open /tmp/vim.strace | grep -v '= -1 ENO' | grep -v '"."' |
    grep -o '".*"' | sort -u

它并不优雅,但它确实有效。第一个grep去掉失败的(ENOTDIRENOENT);第二个删除当前目录的打开(数量惊人);第三个只选择双引号之间的字符串; sort 为您提供每个名称的副本。不要费心查看 .viminfo 文件;它们是二进制数据,既不相关也不可读。同样,您可以忽略共享库和 terminfo 和区域设置文件等。 –

您也可以简单地在 /usr/share/vim 下查找最近更改的文件。例如:

find /usr/share/vim -type f -mtime -30

用于在过去 30 天左右更改的文件。如果找到它们,您可能想担心它们为何被更改。

I got a bunch of files with grep. How can I find out which one is causing the issue? I did find /usr/share/vim -type f -mtime -7 as this error started happening at the end of last week which gave me /usr/share/vim/vimfiles/syntax/HGAnnotate.vim. Is this file the problematic one?

如果该文件在 grepfind 的列表中,那么可能。如果没有,请查看与 NeoBundle 相关的任何内容。否则,尝试对文件使用 file 命令,看看是否有任何文件被标记为具有 CRLF、DOS 或类似的行结尾。如果什么都没有出现,则尝试对它们进行 od -c 并在输出中查找反斜杠-r (\r)。

I found out that the problem was with the NeoBundle files and replaced it with my old GitHub backup. Everything works now.

Unfortunately after installing all my plugins via NeoBundle from my old .vimrc, I still get ^M errors. However this time I can access the NeoBundle plugin from vim.

在这个阶段,您需要重复或继续练习,找到我已经描述过的具有 DOS 样式 CRLF 行结尾的文件。如果 vim 运行,您可以使用您已经知道的技术修复文件,也在 How to convert the ^M linebreak to normal linebreak in a file that is opened in vim?.

中描述

最后,user5187937 报告:

This fixed everything for me: git config --global core.autocrlf input Apparently git was converting all my plugins to CRLF format even though I had never changed my core.autocrlf setting.