如何在 VIM 中 multi-line {} 中的导入函数?

How to multi-line the import functions in {} in VIM?

抱歉标题不好但是

有了这个代码,

import { amethod, methodb, methodc } from '../../utils/mockData';

这个怎么做的

import {
  amethod,
  methodb,
  methodc
} from '../../utils/mockData';

和VIM?我的意思是我怎样才能快速完成?

我做的是

  1. 转到第一种方法。
  2. 按回车键
  3. 转到方法的末尾..
  4. 按回车...
  5. 转到第二种方法。 6.... ...

VIM 太慢了。我们可以在 VIM 中快速做到这一点吗?我想我可以用我的鼠标更快:(

import { amethod, methodb, methodc } from '../...';
         ^

f ;r<CR>;.;.;.

并没有让我觉得特别慢。可能有点太重复了吧?

这里有一个稍微聪明一点的(但可能不是那个聪明)的方法:

ciB               " change in brackets
<CR><CR>          " insert two carriage returns
<Up>              " move up one line
<C-r>"            " insert previous content of brackets
<Esc>             " leave insert mode
:s/,/,\r/g<CR>    " put each symbol on its own line
=iB               " re-indent the content of brackets

为了方便可以映射:

nnoremap <key> ciB<CR><CR><Up><C-r>"<Esc>:s/,/,\r/g<CR>=iB

或者您可以寻找合适的插件来优雅地处理极端情况。

@romainl 有一个很好的答案,但您也可以在这种特殊情况下使用替换:

:%s/\([,{]\)/\n/g

我知道了

import {
    amethod,
    methodb,
    methodc
    } from '../../utils/mockData';

使用这个:

:%s/\v\{\zs( \w+,?)+ \ze}/\=substitute(submatch(0), " ", "\n\t", "g")


\v ............ very magic regex (avoid many backslashes)
{  ............ literal {
\zs ........... vim trick that marks the start of the pattern
(  ............ start of regex group 1
<Space> ....... literal space inside group 1
\w+ ........... one word or more
,?  ........... optional coma
+  ............ quantifier for the group (at least one)
<Space>
\ze ........... end of our vim search

替换函数有三个部分,就像一个正常的 vim 替换和 submatch(0) 对应于我们的正则表达式,因此我们在我们的正则表达式中替换一个 space 一个换行符和两个选项卡。

当光标在 line1 和 col1 上时,您可以在正常模式下按此键:

f 4@=';r^M'

然后按 ENTER。

注意:对于 ^M,您按 Ctrl-v 然后 Enter