Vim、LaTeX、自动换行和版本控制
我正在 vim 中编写 LaTeX 文档,并且将其硬包装为 80 个字符,以便于阅读。但是,这会导致版本控制中跟踪更改出现问题。例如,在本文开头插入“Lorem ipsum”:
1 Dolor sat amet,consectetur adipiscing elit。菜豆 2 quis porta。 Aenean 前庭 magna vel purus laoreet atmolestie Massa 3 沉思。前庭前庭, mauris nec convallis ultrices, Tellus sapien 4 ullamcorper elit、dignissim consectetur justotellus et nunc。
结果是:
1 Lorum ipsum dolor sat amet,consectetur adipiscing elit。必比菜 2 莱博尔蒂斯 quis porta。 Aenean 前庭 magna vel purus laoreet at 3 骚扰马萨苏西皮特。前庭前庭, mauris nec convallis ultrices, 4 Tellus sapien ullamcorper elit,dignissim consectetur justotellus et nunc。
当我在 git 中查看这一更改时,它告诉我该段落的所有行都由于换行而发生了更改,即使只发生了一个语义更改。解决这个问题的一种方法是让每个句子独占一行。这在渲染的文档中看起来是相同的,但现在源代码更难阅读,因为每一行的行长度都有很大不同:
1 Lorum ipsum dolor sat amet,consectetur adipiscing elit。 2 菜豆 (Phasellus bibendum lobortis lectus quis porta)。 3 Aenean前庭magna vel purus laoreet atmolestie Massa Suscipit。 4 前庭前庭、mauris nec convallis ultrices、tellus sapien ullamcorper elit、dignissim consectetur justotellus et nunc。
(如果我在 80 处进行软换行,事情看起来仍然很糟糕,只是以不同的方式。)
是否可以将我的文本存储在磁盘上,每句话一个换行符,但在 vim 中显示和编辑它,就好像每个段落的文本是一行长长的文字,80 个字符?我认为它需要一些 vim-foo 而不是调整 git 或 LaTeX。
I'm writing a LaTeX document in vim, and I have it hard wrapping at 80 characters to make reading easier. However, this causes problems with tracking changes with in version control. For example, inserting "Lorem ipsum" at the beginning of this text:
1 Dolor sit amet, consectetur adipiscing elit. Phasellus bibendum lobortis lectus 2 quis porta. Aenean vestibulum magna vel purus laoreet at molestie massa 3 suscipit. Vestibulum vestibulum, mauris nec convallis ultrices, tellus sapien 4 ullamcorper elit, dignissim consectetur justo tellus et nunc.
results in:
1 Lorum ipsum dolor sit amet, consectetur adipiscing elit. Phasellus bibendum 2 lobortis lectus quis porta. Aenean vestibulum magna vel purus laoreet at 3 molestie massa suscipit. Vestibulum vestibulum, mauris nec convallis ultrices, 4 tellus sapien ullamcorper elit, dignissim consectetur justo tellus et nunc.
When I review this change in git, it tells me that all the lines of the paragraph have changed because of the wrapping, even though only one semantic change has occurred. One way around this problem is to have every sentence on its own line. This looks the same in the rendered document, but the source now is harder to read, because each line has quite a different line length:
1 Lorum ipsum dolor sit amet, consectetur adipiscing elit. 2 Phasellus bibendum lobortis lectus quis porta. 3 Aenean vestibulum magna vel purus laoreet at molestie massa suscipit. 4 Vestibulum vestibulum, mauris nec convallis ultrices, tellus sapien ullamcorper elit, dignissim consectetur justo tellus et nunc.
(If I soft wrap at 80, things still look bad, just in a different way.)
Is it possible to have my text on disk with one newline per sentence, but display and edit it in vim as if the text of each paragraph was one long line, soft wrapped at 80 characters? I assume it requires some vim-foo rather than tweaking git or LaTeX.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
无需引入奇怪的编辑策略:您正在寻找的 git 功能是使用 git diff --color-words 来查看更改。
No need to introduce weird editing policies: the
git
feature you are looking for is usinggit diff --color-words
to review changes.我认为另一种方法是像我通常所做的那样改变vim窗口的宽度:
首先,在.vimrc中放置“set wrap”以启用“wrap”功能;
为了在虚拟终端中运行 vim,我总是将终端的窗口宽度设置为 80 个字符(如“urxvt -geometry 80x38”)。因此,每当我在虚拟终端中的 vim 中编辑文件时,当一行包含超过 80 个字符时,它会自动换行。
如果您更喜欢 gvim(gtk-vim、gnome-vim),您可以通过在 .gvimrc 中添加一行来设置 gvim 窗口的大小,例如“setlines=38columns=80”。
希望有帮助。 :-)
I think an alternative way is to change the width of the vim window like what I did usually:
at first, put "set wrap" in the .vimrc to enable the "wrap" feature;
For running vim in a virtual terminal, I always set the terminal's window width as 80 characters (like "urxvt -geometry 80x38"). Thus, anytime I edit a file in vim in the virtual terminal, it will automatically wrap when a line contains more than 80 characters.
If you prefer the gvim (gtk-vim, gnome-vim), you can just set the size of the gvim window by adding a line in .gvimrc, like "set lines=38 columns=80".
Hope that helps. :-)
我自己也一直在想这样的事情,最后我不得不尝试几种不同的方法来看看哪些方法有效。我绝对不满意的一件事是硬换行符(可以通过 set textwidth=80 来实现),原因如下:
gq
运算符的不同变体(例如gqip
、gqap
、可视模式,然后gq
),但这很乏味,而且我经常用gq
错过目标区域。这对我来说是硬休息的最大缺点。 (如果您确实使用gq
来修复段落,您可能需要使用set nojoinspace
来避免在每个句号后出现烦人的双空格。)f
、F
、t
或T
在句子中导航。这只适用于同一行。对我来说,使用软中断(例如每行一个句子或一个段落)并不是我为适应 Git 而采用的“奇怪的编辑策略”。这是我用来促进编辑时方便和高效的一种(也许很奇怪)编辑策略。特别是在单词中间出现的软换行符,我同意看起来很难看,但可以使用
set linebreak
将其关闭。此外,如果您每行使用一个段落,您应该获得与硬中断相同的“外观”(使用
gq
正确维护),并且您可以使用以下命令从一个句子导航到另一个句子:(
和)
。每行一个句子,右边缘会更加粗糙,但具有 Git 和 Vim 每行一个句子所提供的优势。两者都尝试一下,看看你更喜欢哪一个(我自己还没有决定)。I've been wondering about things like this myself, and in the end I just had to try a few different approaches to see what works. One thing I'm definitely not happy with is hard breaks (which can be acheived by
set textwidth=80
), for several reasons:gq
operator (e.g.gqip
,gqap
, visual mode and thengq
), but this is tedious, and I often miss my target area withgq
. This is for me the biggest disadvantage with hard breaks. (If you do usegq
to fix paragraphs you may want to useset nojoinspace
to avoid getting annoying double spaces after every period.)f
,F
,t
orT
. This only works within the same line.For me, using soft breaks (and having for instance one sentence or paragraph per line) is not a "weird editing policy" I adopt to suit Git. It's a (perhaps weird) editing policy I use to facilitate convenience and efficiency while editing. In particular the soft line breaks occuring in the middle of words I agree look ugly, but that can be turned off using
set linebreak
.If, in addition, you use one paragraph per line, you should get about the same "look" as with hard breaks (which is properly maintained with
gq
), and you can navigate from sentence to sentence with(
and)
. With one sentence per line you will get a more ragged right edge, but with the advantages Git and Vim offers by having one sentence per line. Try both and see what you prefer (I haven't decided yet myself).