Bou*_*ley 238 vim settings newline
所以我在PHP商店工作,我们都使用不同的编辑器,我们都必须在Windows上工作.我使用vim,商店里的每个人都在抱怨每当我编辑一个文件时,底部都会有一个换行符.我四处搜索,发现这是vi&vim的记录行为......但我想知道是否有某种方法可以禁用此功能.(如果我可以为特定的文件扩展名禁用它,那将是最好的).
如果有人知道这一点,那就太好了!
gcb*_*gcb 336
即使文件最后已用新行保存:
vim
并且一旦进入vim:
:set nofixendofline
Run Code Online (Sandbox Code Playgroud)
完成.
或者你可以用vim打开文件 vim
另一种选择:
:set noeol
:wq
Run Code Online (Sandbox Code Playgroud)
对于vim 7.4+,你也可以使用(最好是你的.vimrc):
:set binary
:set noeol
:wq
Run Code Online (Sandbox Code Playgroud)
(感谢罗泽轩的最新消息!)
too*_*php 23
将以下命令添加到.vimrc以转换行尾选项:
autocmd FileType php setlocal noeol binary fileformat=dos
Run Code Online (Sandbox Code Playgroud)
但是,PHP本身将忽略最后一行 - 它应该不是问题.我几乎可以肯定,在你的情况下,还有一些东西正在添加最后一个换行符,或者可能有一个混合使用windows/unix行结束类型(\n或\r\n等).
更新:
另一种解决方案可能是将此行添加到.vimrc中:
set fileformats+=dos
Run Code Online (Sandbox Code Playgroud)
Ami*_*mir 17
如果您使用Git进行源代码管理,还有另一种方法可以解决这个问题.受到答案的启发,我编写了自己的过滤器,用于gitattributes文件.
要安装此过滤器,请将其保存noeol_filter在您的过滤器中$PATH,使其成为可执行文件,然后运行以下命令:
git config --global filter.noeol.clean noeol_filter
git config --global filter.noeol.smudge cat
Run Code Online (Sandbox Code Playgroud)
要仅为自己开始使用过滤器,请在以下行中添加以下行$GIT_DIR/info/attributes:
*.php filter=noeol
Run Code Online (Sandbox Code Playgroud)
.php无论Vim做什么,这都将确保您不在文件中的eof中提交任何换行符.
现在,剧本本身:
#!/usr/bin/python
# a filter that strips newline from last line of its stdin
# if the last line is empty, leave it as-is, to make the operation idempotent
# inspired by: https://stackoverflow.com/questions/1654021/how-can-i-delete-a-newline-if-it-is-the-last-character-in-a-file/1663283#1663283
import sys
if __name__ == '__main__':
try:
pline = sys.stdin.next()
except StopIteration:
# no input, nothing to do
sys.exit(0)
# spit out all but the last line
for line in sys.stdin:
sys.stdout.write(pline)
pline = line
# strip newline from last line before spitting it out
if len(pline) > 2 and pline.endswith("\r\n"):
sys.stdout.write(pline[:-2])
elif len(pline) > 1 and pline.endswith("\n"):
sys.stdout.write(pline[:-1])
else:
sys.stdout.write(pline)
Run Code Online (Sandbox Code Playgroud)
Tim*_*gan 11
我没有尝试过这个选项,但是在vim帮助系统中给出了以下信息(即帮助eol):
'endofline' 'eol' boolean (default on)
local to buffer
{not in Vi}
When writing a file and this option is off and the 'binary' option
is on, no <EOL> will be written for the last line in the file. This
option is automatically set when starting to edit a new file, unless
the file does not have an <EOL> for the last line in the file, in
which case it is reset.
Run Code Online (Sandbox Code Playgroud)
通常,您不必设置或重置此选项.当'binary'关闭时,在写入文件时不使用该值.当'binary'打开时,它用于记住文件中最后一行的a的存在,这样当您编写文件时,可以保留原始文件中的情境.但是如果你愿意,你可以改变它.
您可能对前一个问题的答案感兴趣:" 为什么文件应以换行符结尾 ".
我在Vim wiki上添加了一个类似(虽然不同)问题的提示:
http://vim.wikia.com/wiki/Do_not_auto-add_a_newline_at_EOF
好的,你在Windows上使事情变得复杂;)
由于'binary'选项重置了'fileformat'选项(并且用'binary'设置写入总是用unix行结尾写),让我们拿出大锤子并在外部执行!
如何为BufWritePost事件定义自动命令(:help autocommand)?每次编写整个缓冲区后都会执行此自动命令.在这个自动命令中调用一个小的外部工具(php,perl或任何脚本)来剥离刚刚写入的文件的最后一个换行符.
所以这看起来像这样,并将进入你的.vimrc文件:
autocmd! "Remove all autocmds (for current group), see below"
autocmd BufWritePost *.php !your-script <afile>
Run Code Online (Sandbox Code Playgroud)
如果这是您第一次处理自动命令,请务必阅读有关自动命令的整个vim文档.有一些注意事项,例如,建议您删除.vimrc中的所有autocmds,以防.vimrc可能多次获取源代码.
我已经用Perl和Python后处理实现了Blixtor的建议,要么在Vim中运行(如果它是用这种语言支持编译的话),要么是通过外部Perl脚本.它可以在vim.org上作为PreserveNoEOL插件使用.
从 v7.4 开始vim您可以使用
:set nofixendofline
Run Code Online (Sandbox Code Playgroud)
这里有一些有关该更改的信息:http://ftp.vim.org/vim/patches/7.4/7.4.785。
| 归档时间: |
|
| 查看次数: |
85047 次 |
| 最近记录: |