vim 格式化代码之长行换行
2013-01-10 15:17
330 查看
为了让长行能自动换行,首先在~/.vimrc中 设置了textwidth=72,
然后gq=G 或者 gggqG,
发现最大的问题就是是把好几行拼接起来,实现换行了。
比如:
557
00:47:39,487 --> 00:47:42,453
I will have to complete some procedures,
and I asked you to check out what they are for me
gg=G之后:
557 00:47:39,487 --> 00:47:42,453 I will
have to complete some procedures, and I
asked you to check out what they are for
me
设置formatoptions+=w就可以了。
这个链接说的很清楚了:
http://stackoverflow.com/questions/1318470/change-wrap-width-in-a-text-file-using-vim
问题:
I want to format srt subtitle text files to avoid wrapping problems on my media player.
I need to set a line wrap width to a number of characters e.g. 43
I can do this with Editplus, its a built in function and works well. The reason I want to do it in Vim, firstly Editplus is only available on the PC and the secondly Vim is badass.
I have found the following solution on the net..
:set tw=43
gggqG
It does work, but not exactly how I want it.
E.g.
I have this text:
557
00:47:39,487 --> 00:47:42,453
I will have to complete some procedures,
and I asked you to check out what they are for me
after I format it, I get:
557 00:47:39,487 --> 00:47:42,453 I will
have to complete some procedures, and I
asked you to check out what they are for
me
It seems to ignore line breaks/CRs. As you can see the "I will" has been added to the first line.
How do I get it to not ignore line breaks?
EDIT: apoligies about the formatting, first time using stackoverflow!
解决办法:
You could use the whitespace option of
wrap end in whitespace.
The third line adds a space on the end of any line starting with a letter and
joining lines that don't end in spaces.
See:
Edit in response to comments
This translates to:
The global (
will last for the whole session. If you want those options to only be used on the current buffer, use
参考:
http://blog.sina.com.cn/s/blog_8d05143b0100yb2p.html
然后gq=G 或者 gggqG,
发现最大的问题就是是把好几行拼接起来,实现换行了。
比如:
557
00:47:39,487 --> 00:47:42,453
I will have to complete some procedures,
and I asked you to check out what they are for me
gg=G之后:
557 00:47:39,487 --> 00:47:42,453 I will
have to complete some procedures, and I
asked you to check out what they are for
me
设置formatoptions+=w就可以了。
这个链接说的很清楚了:
http://stackoverflow.com/questions/1318470/change-wrap-width-in-a-text-file-using-vim
问题:
I want to format srt subtitle text files to avoid wrapping problems on my media player.
I need to set a line wrap width to a number of characters e.g. 43
I can do this with Editplus, its a built in function and works well. The reason I want to do it in Vim, firstly Editplus is only available on the PC and the secondly Vim is badass.
I have found the following solution on the net..
:set tw=43
gggqG
It does work, but not exactly how I want it.
E.g.
I have this text:
557
00:47:39,487 --> 00:47:42,453
I will have to complete some procedures,
and I asked you to check out what they are for me
after I format it, I get:
557 00:47:39,487 --> 00:47:42,453 I will
have to complete some procedures, and I
asked you to check out what they are for
me
It seems to ignore line breaks/CRs. As you can see the "I will" has been added to the first line.
How do I get it to not ignore line breaks?
EDIT: apoligies about the formatting, first time using stackoverflow!
解决办法:
You could use the whitespace option of
formatoptionsand make the lines you want to
wrap end in whitespace.
:set tw=43 :set fo+=w :g/^\a/s/$/ / gggqG
The third line adds a space on the end of any line starting with a letter and
fo+=wstops
gqfrom
joining lines that don't end in spaces.
See:
:help fo-table :help 'formatoptions' :help gq :help :g
Edit in response to comments
:g/^\a/s/$/ /
This translates to:
:g/ " Search the file ^\a " For lines starting (^) with an alphabetic character (\a - equivalent to [A-Za-z]) / " Then on each line that the regexp matches (i.e. each line starting with an alphabetic character) s/ " Substitute... $ " The end of line (zero-width match at the end of the line) / / " With a space (slashes are delimiters)
The global (
:g) command will only operate on the current file, but the
textwidthand
formatoptionslines
will last for the whole session. If you want those options to only be used on the current buffer, use
:setlocalinstead:
:setlocal tw=43 :setlocal fo+=w :help :setlocal
参考:
http://blog.sina.com.cn/s/blog_8d05143b0100yb2p.html
相关文章推荐
- vim 格式化当前代码
- VIM格式化代码
- vim格式化python代码
- MyEclipse中防止代码格式化时出现换行的情况的设置
- 如何设置eclipse格式化代码时不自动换行
- 【转】vim格式化C代码
- vim格式化代码
- VIM自动格式化C代码
- vim 格式化代码
- vim格式化python代码
- MyEclipse中防止代码格式化时出现换行的情况的设置
- vim格式化代码实际上就是 "缩进代码", 命令是等号=
- vim下格式化代码
- vim 快速格式化代码
- Eclipse格式化代码时不换行
- VIM格式化C代码
- vim中自动格式化代码
- 用vim格式化代码(转)
- vim 添加php自动补全 并格式化代码
- Eclipse以及MyEclipse中防止代码格式化时出现换行的情况