[英]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? 我的意思是我怎樣才能快速做到?
我要做的是
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/\([,{]\)/\1\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) 對應於我們的正則表達式,因此我們在正則表達式中用一個空格代替一個換行符和兩個制表符。
將光標放在 line1 和 col1 上,您可以在正常模式下按此:
f 4@=';r^M'
然后按 ENTER。
注意:對於^M
您按Ctrl-v
然后Enter
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.