在Cygwin上使用Vim:警告vimrc正在使用Windows换行符

11

我刚在我的电脑上安装了cygwin,并且正在设置一切。

我在cygwin home目录和windows home目录之间创建了一个.vimrc的软链接,但是vim读取.vimrc文件时遇到了一些困难,因为它使用的是Windows行结束符。

是否有一种方法可以告诉vim我的vimrc文件使用的是Windows行结束符?

以下是一些输出。

~ $ ls -la
total 43
drwxr-xr-x+ 1 Jake None    0 Mar 30 10:11 .
drwxrwxrwt+ 1 Jake root    0 Mar 30 09:33 ..
-rw-------  1 Jake None  504 Mar 30 10:10 .bash_history
-rwxr-xr-x  1 Jake None 1103 Mar 29 14:24 .bash_profile
-rwxr-xr-x  1 Jake None 5737 Mar 30 10:06 .bashrc
-rwxr-xr-x  1 Jake None 1461 Mar 29 14:24 .inputrc
-rw-------  1 Jake None   57 Mar 30 10:05 .lesshst
-rwxr-xr-x  1 Jake None 8945 Mar 30 09:52 .mkshrc
-rwxr-xr-x  1 Jake None  792 Mar 29 14:24 .profile
-rw-------  1 Jake None 1087 Mar 30 10:11 .viminfo
lrwxrwxrwx  1 Jake None   29 Mar 30 10:05 .vimrc -> /cygdrive/c/Users/Jake/_vimrc
~ $
~ $ vim
Error detected while processing /home/Jake/.vimrc:
line    2:
E492: Not an editor command: ^M
line    3:
E492: Not an editor command: ^M
line    4:
E492: Not an editor command: ^M
line    5:
E492: Not an editor command: ^M
line   11:
E492: Not an editor command: ^M
line   14:
E492: Not an editor command: ^M
line   16:
E492: Not an editor command: ^M
line   20:
E492: Not an editor command: ^M
line   21:
E492: Not an editor command: ^M
line   22:
E492: Not an editor command: ^M
line   27:
E474: Invalid argument: backspace=indent,eol,start^M
...

Cygwin 有一个实用程序可以帮助您,叫做 d2u。 - johnny
4个回答

15

在cygwin中输入以下命令

dos2unix.exe _vimrc

不错,尽管我在cygwin中遇到了一个错误,因为路径上没有cygwin1.dll,但这解决了那个问题。 - enkor

5

如果你启用了 modeline,你可以将下面这行作为你的 vimrc 文件的第一行:

" vim: se ff=unix :

当然,第一次需要手动修复它(:se ff=dos|w!)。
这样,您就不必担心警告,因为您始终处于该文件的正确模式中。
如果您想要将其应用于所有文件,请查看自动命令(AuCommand,例如BufEnter)。如果您只想始终查看文件格式,则可以将其包含在标尺或状态行中。
:he 'rulerformat'
:he 'statusline'

简单的状态栏

:set statusline='%{&fileformat}'

HTH

可能是 "希望能帮到你" 的缩写(Hope This Helps)。

P.S. 如果您的文件需要更多修复,因为它已经混合了行尾,您可以

:%s/^M$//g

一定要加上美元符号,否则会破坏映射关系。在 Windows 上输入 ^M 时,请使用 Ctrl-QEnter(在 Linux 上使用 Ctrl-VEnter),前提是使用默认配置。

添加了更多有关修复混合行尾文件的信息。如果启用modeline,则这是一次性操作。 - sehe

1

我在我的状态栏中有这个:

hi User9 term=reverse cterm=bold ctermbg=12 gui=bold guibg=Red
set statusline=...
set statusline+=\ %9*%{&ff=='unix'?'':&ff.'\ format'}%*

这样做的作用是,如果我加载具有dos行结束符的文件,则在我的状态栏中会出现漂亮的红色“dos”。


1

从cygwin shell中,您可以输入以下命令:

dos2unix.exe .vimrc

这将把您的文件转换为Unix格式。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接