我在一家PHP商店工作,我们都使用不同的编辑器,我们都必须在Windows上工作。我使用vim,但是店里的每个人都在抱怨,每当我编辑一个文件时,在底部有一个换行符。我四处搜索,发现这是vi & vim的记录行为…但我想知道是否有办法禁用这个功能。(这将是最好的,如果我可以禁用特定的文件扩展名)。
如果有人知道这事,那就太好了!
我在一家PHP商店工作,我们都使用不同的编辑器,我们都必须在Windows上工作。我使用vim,但是店里的每个人都在抱怨,每当我编辑一个文件时,在底部有一个换行符。我四处搜索,发现这是vi & vim的记录行为…但我想知道是否有办法禁用这个功能。(这将是最好的,如果我可以禁用特定的文件扩展名)。
如果有人知道这事,那就太好了!
当前回答
如果使用Git进行源代码控制,还有另一种方法。受到这里答案的启发,我编写了自己的筛选器,用于gitattributes文件。
要安装这个过滤器,将其保存为noeol_filter在你的$PATH的某处,使其可执行,并运行以下命令:
git config --global filter.noeol.clean noeol_filter
git config --global filter.noeol.smudge cat
要开始仅为自己使用过滤器,在$GIT_DIR/info/attributes中放入以下一行:
*.php filter=noeol
这将确保无论Vim做什么,都不会在.php文件的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)
其他回答
我发现这个vimscript插件对这种情况很有帮助。
Plugin 'vim-scripts/PreserveNoEOL'
或者在github上阅读更多
我已经用Perl和Python后处理实现了Blixtor的建议,要么在Vim内部运行(如果它是用这种语言支持编译的),要么通过外部Perl脚本运行。它可以在vim.org上作为PreserveNoEOL插件使用。
尝试添加.vimrc
set binary
对于vim 7.4+,你可以使用(最好是在你的.vimrc上)(感谢罗泽轩的最后一点新闻!):
:set nofixendofline
现在关于vim的旧版本。
即使文件已经保存,在最后添加新行:
Vim -b文件 一旦进入vim:
:set noeol
:wq
完成了。
或者,您可以在vim中使用:e++ bin文件打开文件
还有另一种选择:
:set binary
:set noeol
:wq
查看更多细节在为什么我需要vim在二进制模式为'noeol'工作?
可以从vim v7.4开始使用
:set nofixendofline
这里有一些关于这一变化的信息:http://ftp.vim.org/vim/patches/7.4/7.4.785。