一千萬個為什麽

搜索

反轉一組段落

是否有一種很好的方法來反轉一組段落(通過光標運動 {} 可導航)?我的實際用例是顛倒乳膠代碼塊的順序(即,將簡歷中的作業順序從最舊到最新轉換為最新到最舊)。

示例代碼:

\begin{rSubsection}{Example 1}{}{Redacted}{}
\item Stuff I did
\item Also did other things
\end{rSubsection}

\begin{rSubsection}{Example 2}{}{Redacted}{}
\item Stuff I did
\item Also did other things
\end{rSubsection}

\begin{rSubsection}{Example 3}{}{Redacted}{}
\item Stuff I did
\item Also did other things
\end{rSubsection}

\begin{rSubsection}{Example 4}{}{Redacted}{}
\item Stuff I did
\item Also did other things
\end{rSubsection}

最佳答案

另一個解決方案:

:$put _ | g/Example [2-4]/normal! dapggP

一線解決方案:

:$pu _ | 16,20m0 | 16,20m5 | 16,20m10

我已經考慮了很多,直到達到這個優雅的解決方案(上面的一個班輪)。在這個解決方案中,我們在最後:$ pu _ 添加一個新的lind,然後,我們得到最後一行並將它們分別移動到0,5和10行

另一種方式:

:$pu _ | normal gg
:16,$m.-1 | normal j
[email protected]:

(舊解決方案)此解決方案將反轉塊順序

:$put _ ..................... add a new line at the end
4k .........................  move to the begining of {example 4}
5:m-0 ......................  move next 5 lines to line zero
}}j ........................  move to {example 3}
5:m-11 .....................  move block to 11 lines before
}j .........................  move to the {example 2}
5:m-6 ......................  finishes the magic

Notice: When you type 5:m-0 for example vim will show :.,.+4m-0

OBS: if this block is inside a bigger one you have to use your Relative numbers to do the trick :set nu rnu

如果你想映射移動塊

"" Move lines in all modes 
vnoremap J :m '>+1gv=gv
vnoremap K :m '<-2gv=gv
inoremap  :m+==gi
inoremap  :m-2==gi
nnoremap  :m+
nnoremap  :m-2

轉載註明原文: 反轉一組段落