假设我们在代码中有这么长的字符串。
strs = "Small is beautiful. Make each program do one thing well. Build a prototype as soon as possible. Choose portability over efficiency. Store data in flat text files. Use software leverage to your advantage.Use shell scripts to increase leverage and portability.Avoid captive user interfaces.Make every program a filter.";为了便于阅读,我想把上面的句子分开。
strs = "Small is beautiful." +
"Make each program do one thing well." +
"Build a prototype as soon as possible." +
"Choose portability over efficiency." +
"Store data in flat text files." +
"Use software leverage to your advantage." +
"Use shell scripts to increase leverage and portability." +
"Avoid captive user interfaces.Make every program a filter." 有什么办法自然地实现这一点吗?这是一项无聊的重复任务。
发布于 2018-07-04 12:05:55
很可能不是小事。无聊又重复?我们在使用同一个编辑器吗?:)
f"s<CR><Esc> " Find the quote, replace with newline
$,C<CR><Esc> " remove the last quote so it's not in the way
km' " go down to the first line and set a jump mark
:s/\. \?/&<C-V><Enter>/g<CR> " split into sentences
kV'' " select all the sentences by jumping to our mark
:s/.*/"&" +/<CR> " surround them with quotes, add the plus sign
$xx " the last one won't need a plus sign
gv>. " select the sentences again, and indent them twice
kJ " join the variable declaration这是一个定义很好的一系列操作,您可以保存在宏中(例如,qz开始录制,q停止录制),然后在需要相同处理(@z)的任何其他地方回放。
发布于 2018-07-04 14:58:47
基本上,您所要做的就是在每个句点之后插入一个" +\n",而不是后面跟着一个引号(以保护最后一个句号)。考虑到游标在该行上,这可以很容易地在一个命令中完成:
:s/\v\.\s*"@!\zs/" +\r"/g请注意,此命令还将保留句点后偶尔出现的空白。在你的问题中,你完全删除了它们。目前还不清楚这是否是需要的,但它似乎修改了最终的结果字符串。
一个问题是这个命令没有处理缩进。当然,使用一些标准的手动命令可以很容易地解决这个问题。但是,也可以使用以下策略在:s命令中执行此操作:
getline('.')的行matchstr(<line>, '[^"]*')匹配strlen(<match>)获取匹配内容的长度repeat(' ', <length>)重复该长度的空格字符"替换中,在最终的" +\r"之前插入这些空格全部合并(冗长):
:s/\v\.\s*"@!\zs/\="\" +\n".repeat(' ',strlen(matchstr(getline('.'),'[^"]*'))).'"'/g发布于 2018-07-04 12:25:49
这不是一个完全自动化的解决方案,但该方案很简单,可以适用于许多类似的情况:
" 1. Search
/\.\s* " find all sentence ends and go to the first of them
" 2. Start recording
qa " start recording macro a
" 3. Edit
dw " delete till begin of sentence
i " insert mode
." +<CR><TAB><ESC> " insert ." +<CR><TAB> and return to normal mode
" 4. Goto next occurrence
n " goto next sentence
" 5. End recording
q " end of macro recording
" 6. Repeat
@a " repeat macro 1 time
3@a " repeat macro 3 times (or replace 3 with a bigger number)或者,也许打字和记忆更简单:
" 1. Search
/\.\s* " find all sentence ends and go to the first of them
" 2. Edit
caw " delete till begin of sentence and switch to insert mode
." +<CR><TAB><ESC> " insert ." +<CR><TAB> and return to normal mode
" 3. Goto next occurrence and repeat the last change
n. " goto next sentence and repeat the last change
" Repeat 3. as many times as neededhttps://stackoverflow.com/questions/51172907
复制相似问题