2012-03-21 67 views
5

計算縮進級別時,考慮寫一個javadoc風格的註釋,其中包括一個縮進列表(當expandtab設置和softtabstop=2)忽略註釋之後的空間:如何在Vim中

/** 
* First line: 
* - Indented text 
*/ 

目前,打字First line:擊中後返回,Vim將正確插入*<space>。但是,當我點擊標籤頁來縮進第二行時,只會插入一個的空格而不是兩個。

是否可以解決這個問題,所以*之後的空格在縮進計算過程中會被忽略?

+0

由於您將選項卡設置爲2空格,因此會考慮第一個空格。我建議你採取當前的設置,然後添加一個空間。按回車,你應該停在你想要的位置(* + 3個空格)。 – 2012-04-12 03:58:30

回答

1

我仍然是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 
+1

太糟糕了[美化](http://code.google.com/p/google-code-prettify/)不支持VimScript。 – 2012-09-05 01:20:39