首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >是否有任何方法或插件来实现分裂长双引号句子?

是否有任何方法或插件来实现分裂长双引号句子?
EN

Stack Overflow用户
提问于 2018-07-04 11:45:18
回答 3查看 45关注 0票数 1

假设我们在代码中有这么长的字符串。

代码语言:javascript
复制
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.";

为了便于阅读,我想把上面的句子分开。

代码语言:javascript
复制
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." 

有什么办法自然地实现这一点吗?这是一项无聊的重复任务。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-07-04 12:05:55

很可能不是小事。无聊又重复?我们在使用同一个编辑器吗?:)

代码语言:javascript
复制
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)的任何其他地方回放。

票数 2
EN

Stack Overflow用户

发布于 2018-07-04 14:58:47

基本上,您所要做的就是在每个句点之后插入一个" +\n",而不是后面跟着一个引号(以保护最后一个句号)。考虑到游标在该行上,这可以很容易地在一个命令中完成:

代码语言:javascript
复制
:s/\v\.\s*"@!\zs/" +\r"/g

请注意,此命令还将保留句点后偶尔出现的空白。在你的问题中,你完全删除了它们。目前还不清楚这是否是需要的,但它似乎修改了最终的结果字符串。

一个问题是这个命令没有处理缩进。当然,使用一些标准的手动命令可以很容易地解决这个问题。但是,也可以使用以下策略在:s命令中执行此操作:

  • 获取当前正在使用getline('.')的行
  • 将第一个引号之前的内容与matchstr(<line>, '[^"]*')匹配
  • 使用strlen(<match>)获取匹配内容的长度
  • 使用repeat(' ', <length>)重复该长度的空格字符
  • 在早期的"替换中,在最终的" +\r"之前插入这些空格

全部合并(冗长):

代码语言:javascript
复制
:s/\v\.\s*"@!\zs/\="\" +\n".repeat(' ',strlen(matchstr(getline('.'),'[^"]*'))).'"'/g
票数 3
EN

Stack Overflow用户

发布于 2018-07-04 12:25:49

这不是一个完全自动化的解决方案,但该方案很简单,可以适用于许多类似的情况:

代码语言:javascript
复制
" 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)

或者,也许打字和记忆更简单:

代码语言:javascript
复制
" 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 needed
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51172907

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档