最近我需要一种更快的方法来根据通用字符(通常=
)格式化相似的代码行。例如,我想格式化这个:
myVar = getMyVar();
myLongerVar = getMyLongerVar();
myYetLongerVar = getMyYetLongerVar();
进入那个:
myVar = getMyVar();
myLongerVar = getMyLongerVar();
myYetLongerVar = getMyYetLongerVar();
然后我写了以下映射:
" query used to find the common character. In this case i'm setting it to "find the ="
let g:defformatquery = "f="
" set current line as having the longer size till the common character
nnoremap <Leader>gm 0
\:execute "normal " . g:defformatquery<CR>
\:let b:epos = getpos(".")[2]<CR>
" format current line according to the position acquired above
nnoremap <Leader>g= 0
\:execute "normal " . g:defformatquery<CR>hvgeld
\:execute "normal " . (b:epos - getpos(".")[2]) . "i "<CR>
要使用它们,我必须执行这些步骤(假设,
是我的<Leader>
):
- 将光标定位在
=
符号前面较长文本的行中(提供的示例中的第三行,myYetLongerVar
) - 按:
,gm
- 对于我要格式化的每一行,将光标放在那里并按
,g=
虽然这可行,但过程有点慢。我想创建一个可以一次格式化整个选定区域的函数。然后我可以为该功能创建一张地图。
有任何想法吗?