英文:
Unable to remove a line feed with VIM
问题
在多个终端仿真器上使用vim或nano编辑文件时,它会向我的文件中添加一个换行符。
编辑前文件的xxd表示:
00001880: 5645 5574 4c53 3074 4c51 6f3d VEUtLS0tLQo=
编辑后保存但没有对文件进行更改(由我进行的更改)的文件的xxd表示:
00001880: 5645 5574 4c53 3074 4c51 6f3d 0a VEUtLS0tLQo=.
我以为你可以在vim中使用 :set list
然后删除换行符。但似乎并非如此。Vim在每行的末尾显示一个$符号,但我无法看到LF并将其删除。我还尝试了 :set binary
、:set ++ffs=unix
、sed -i 's/\r$//' 文件
和其他选项。我错过了什么?
英文:
When I edit a file in vim or nano on multiple terminal emulators it is adding a line feed to my files.
xxd of file before edit:
00001880: 5645 5574 4c53 3074 4c51 6f3d VEUtLS0tLQo=
xxd of file after an edit, save but no changes (made by me) to the file:
00001880: 5645 5574 4c53 3074 4c51 6f3d 0a VEUtLS0tLQo=.
I thought you could use :set list
in vim and then remove the line feed. That does not seem to be the case. Vim is displaying a $ at the end of each line, however I am unable to see the LF and remove it. I have also tried :set binary
, :set ++ffs=unix
, sed -i 's/\r$//' file
and other options. What am I missing here?
答案1
得分: 1
我认为你需要的选项是这个:
:set nofixendofline
你可以将这个选项包括在.vimrc
文件中:
" .vimrc 文件
autocmd Filetype <your-filetype> set nofixendofline
你可以在这里找到更多信息:
https://stackoverflow.com/a/16114535/4681320
英文:
I think that the option that you need is this
:set nofixendofline
you can include the option in the .vimrc
file
" .vimrc file
autocmd Filetype <your-filetype> set nofixendofline
you can find more information here
https://stackoverflow.com/a/16114535/4681320
答案2
得分: 0
这可能是因为Unix中文本文件的定义方式。更具体地说,一个POSIX "文本文件" 必须以换行符结尾(或者是一个空文件)。
引用POSIX的定义:
3.403 文本文件
一个包含字符组织成零个或多个行的文件。[...]
3.206 行
一个由零个或多个非<newline>字符组成并以终止的<newline>字符结尾的序列。
也就是说,每当Vim保存文件时,它会追加一个换行符。
另请参阅:https://stackoverflow.com/q/729692/2057969
正如一位评论者所指出的,可以在这里讨论解决方案:https://stackoverflow.com/q/1050640/2057969
英文:
This is probably because of the way textfiles are defined in Unix. More specifically, a POSIX "text file" must end in a newline (or be an empty file).
To quote the POSIX definition:
> ###### 3.403 Text File
> A file that contains characters organized into zero or more lines. [...]
> ###### 3.206 Line
> A sequence of zero or more non- <newline> characters plus a terminating <newline> character.
That is, whenever Vim saves the file, it appends a newline.
See also: https://stackoverflow.com/q/729692/2057969
As a commenter points out, solutions are discussed here: https://stackoverflow.com/q/1050640/2057969
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论