是否有任何方法或插件可以实现双引号拆分长句子?

时间:2018-07-04 11:45:19

标签: vim

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

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

有什么方法可以自然实施吗?这是无聊的重复任务。

3 个答案:

答案 0 :(得分:3)

基本上您要做的就是在之后插入一个" +\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

答案 1 :(得分:2)

可能不小。无聊又重复吗?我们是否使用相同的编辑器? :)

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 :(得分:1)

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

" 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 needed