2011-03-02 34 views
7

我使用雷泰公司的Vim僅空間

:set noet|retab! 

currentling但我遇到的是它的更換4個空格所有實例的標籤在整個文件中的問題。 我需要vim只在行的開始處替換4個空格的實例。

如果我刪除!在retab結束時,空間不會被替換。

我使用自定義功能,有人創建嘗試:

" Retab spaced file, but only indentation 
command! RetabIndents call RetabIndents() 

" Retab spaced file, but only indentation 
func! RetabIndents() 
    let saved_view = winsaveview() 
    execute '%[email protected]^\(\{'.&ts.'}\)\[email protected]\=repeat("\t", len(submatch(0))/'.&ts.')@' 
    call winrestview(saved_view) 
endfunc 

但我得到一個可愛的小錯誤信息,當我運行:檢測

:RetabIndents 

錯誤而處理功能RetabIndents :

第2行:

E486:找不到模式:^({4})+

+0

你確定你還沒有取代所有標籤開始空間?你使用的是原始文件還是已經運行':set noet | retab!'的已修改文件(或緩衝區)? – gotgenes

+0

好吧,這是問題,我沒有任何空間來替換。如果找不到匹配,是不是應該忽略修飾符? –

+1

另請參閱http://stackoverflow.com/questions/5144284/force-vi-vim-to-use-leading-tabs-only-on-retab – wimh

回答

9

在與其他人討論此事之後,我需要添加無聲!命令執行前。 所以這就是我現在的工作:

autocmd BufWritePre * :RetabIndents 
command! RetabIndents call RetabIndents() 

func! RetabIndents() 
    let saved_view = winsaveview() 
    execute '%[email protected]^\(\ \{'.&ts.'\}\)\[email protected]\=repeat("\t", len(submatch(0))/'.&ts.')@e' 
    call winrestview(saved_view) 
endfunc 

所以,現在該功能將自動按每行的開始更換標籤的空間。

+2

您可以在':substitute'末尾使用'e'標誌命令而不是'silent!',請參閱':h s_flags'。 – ZyX

+0

這就是我一直在尋找的!謝謝!添加了e標誌並取消了沉默! –

0

我使用不同的方法在我的shell腳本開始處將空格更改爲製表符。我只是從命令行使用sed。

使用BSD sed的:

sed -i "" -e ':loop' -e "s/^\([ ]*\) /\1 /" -e 't loop' somefile.sh 

*注:(ⅰ)在方括號中的字符是一個製表符(ⅱ)aftger的/ \ 1的字符也是製表符。兩個製表符cgharacters都使用Ctrl + v + Tab組合鍵輸入終端。

使用GNU sed的:

sed -i -e ':loop' -e 's/^\([\t]*\) /\1\t/' -e 't loop' somefile.sh