考虑编写一个 JavaDoc 样式的注释,其中包含一个缩进列表(何时expandtab
设置和softtabstop=2
):
/**
* First line:
* - Indented text
*/
目前,在输入First line:
和点击之后return,Vim 会正确插入*<space>
. 但是,当我点击tab缩进第二行时,只会插入一个空格而不是两个。
是否可以解决此问题,因此*
在缩进计算期间将忽略后面的空格?
考虑编写一个 JavaDoc 样式的注释,其中包含一个缩进列表(何时expandtab
设置和softtabstop=2
):
/**
* First line:
* - Indented text
*/
目前,在输入First line:
和点击之后return,Vim 会正确插入*<space>
. 但是,当我点击tab缩进第二行时,只会插入一个空格而不是两个。
是否可以解决此问题,因此*
在缩进计算期间将忽略后面的空格?
我仍然是 VimScript 的初学者,但我为你准备了这个。试一试,让我知道你的想法。
function AdjustSoftTabStop()
" Only act if we are in a /* */ comment region
if match(getline('.'), '\s*\*') == 0
" Compensate for switching out of insert mode sometimes removing lone
" final space
if match(getline('.'), '\*$') != -1
" Put back in the space that was removed
substitute/\*$/\* /
" Adjust position of the cursor accordingly
normal l
endif
" Temporary new value for softtabstop; use the currect column like a
" base to extend off of the normal distance
let &softtabstop+=col('.')
endif
endfunction
function ResetSoftTabStop()
" Note that you will want to change this if you do not like your tabstop
" and softtabstop equal.
let &softtabstop=&tabstop
endfunction
" Create mapping to call the function when <TAB> is pressed. Note that because
" this is mapped with inoremap (mapping in insert mode disallowing remapping of
" character on the RHS), it does not result in infinite recursion.
inoremap <TAB> <ESC>:call AdjustSoftTabStop()<CR>a<TAB><ESC>:call ResetSoftTabStop()<CR>a