Vim, LaTeX, word-wrapping, and version control

被刻印的时光 ゝ 提交于 2019-12-03 12:12:42

No need to introduce weird editing policies: the git feature you are looking for is using git diff --color-words to review changes.

I think an alternative way is to change the width of the vim window like what I did usually:

  1. at first, put "set wrap" in the .vimrc to enable the "wrap" feature;

  2. 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.

  3. 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. :-)

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:

  • Whenever I go back to edit previous text the hard linebreak will be on the wrong place. It will either go beyond my 80 character limit, or it will be too far from my 80 character limit. Sure, I can fix this in Vim by using different variants of the gq operator (e.g. gqip, gqap, visual mode and then gq), but this is tedious, and I often miss my target area with gq. This is for me the biggest disadvantage with hard breaks. (If you do use gq to fix paragraphs you may want to use set nojoinspace to avoid getting annoying double spaces after every period.)
  • I often have Vim and the PDF open side-by-side. Whenever I want to edit something at a specific place in my PDF I just search for fragments of the text. You'd be surprised at how few places in a text you use two rather common words in a particular order, so this really works quite well. Unless you have hard breaks in the middle of the sentece that breaks between the words you're searching for. This delays my workflow rather frequently when using hard breaks.
  • I often navigate within senteces using f, F, t or T. This only works within the same line.
  • There's also the problem mentioned by the OP that Git doesn't handle hard breaked paragraphs very well. Sure, you can make the diff look nice nevertheless, so it's not really a big problem.

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).

This is how I solve the problem. First you may use vim command (in normal status) to search your tex file for the lines longer than 80 chars:

/\%81v.\+/

you may define it as a macro so as not to remember the commands. Then you type in the normal status

nF a

(that is, a little "n", a capital "F", a space " ", a little "a", an "Enter", and an ). This help you break your line softly after some word. You may record this five key strokes as a macro (howto: in normal status press "qa" to start recording and just do the steps "nF a" and then press Esc and "q" to stop. When playing, press "@a" because we have stored the macro into "a". To play the macro 100 times (it does not hurt if you play it too many times; it just automatically stops when finishing all the job) by pressing "100@a" in normal status)

Does this solve your problem?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!