2010-01-14 53 views
51

插入模式開始行每當我要評論在vim增加縮進線,我打 - Ø(打開高於目前的一個新行,切換到插入模式)並開始輸入Python註釋(使用#)。然後,這個哈希奇蹟般地移動到行的開頭(無縮進),我必須點擊幾次tab。評論(#)走在Vim的

任何人都知道如何解決它?

回答

52

我想你在.vimrc有set smartindent

:h smartindent

When typing '#' as the first character in a new line, the indent for 
that line is removed, the '#' is put in the first column. The indent 
is restored for the next line. If you don't want this, use this 
mapping: ":inoremap # X^H#", where ^H is entered with CTRL-V CTRL-H. 
When using the ">>" command, lines starting with '#' are not shifted 
right. 

我相信你並不需要,而編碼蟒蛇smartindenting。所以,只是從你的設置中刪除或添加以下到您的.vimrc:

au! FileType python setl nosmartindent 
+0

這個答案顯然是對的錢,非常感謝。我完全錯過了這一點。 但是,我不同意這個事實,我不需要smartindenting,因爲python是關於它的。相反,我可以添加異常。 但是,我想知道哪個是最好的,手冊的inoremap還是Haes提供的?或者說,有什麼不同? – 2010-01-15 08:31:46

+2

我認爲python'smartindent'沒用。 Python程序員不需要縮進就可以自動插入1.在以'{'結尾的行後面。 2.在以'}'開頭的行之前。來自'cinwords'的關鍵字可以通過python filetype indentation正確處理。 – 2010-01-15 09:13:24

+0

基本上inoremaps是相同的。這個inoremap唯一的缺點是 - 你不能用'>>'把註釋轉移到正確的位置 – 2010-01-15 09:16:33

1

你可能想嘗試Nerd Commenter,這是一個插件,允許你自動添加評論到大多數語言的行。您只需將光標放在您感興趣的行上,並輸入,c空間並且該行將被註釋掉。相同的擊鍵將移除註釋以顯示該行。

所以,如果您有:

def func(): 
    print("indented") <- cursor position in command mode 

類型Ç空間,你會得到:

def func(): 
    #print("indented") 
+0

comments.vim是一個輕量級替代http://www.vim.org/scripts/script.php?script_id=1528 – Shaun 2014-06-12 12:36:50

9

嘗試把在你的.vimrc:

autocmd BufRead *.py inoremap # X<c-h># 

這將使得在Python源文件中始終縮進插入散列(磅)符號。

+0

謝謝@Haes,我實際上寧願在編輯python文件時留下smartindent。也許在啓動一個新的if語句後添加一個'tab'是微不足道的,但這是我習慣的。 – isaaclw 2014-10-07 19:03:40

+1

好吧,實際上我需要的是「filetype plugin indent on」,然後我禁用了smartindent for python。 – isaaclw 2014-10-07 19:18:46