2012-03-22 60 views
19

當我複製一個Python代碼,並粘貼到vim。縮進都是錯誤的。 但我粘貼到emacs或gedit,它是正確的。如何將源代碼粘貼到vim而沒有錯誤格式?

這是很難描述,讓我們看看截圖。 注意:藍色和黃色線只是使用「縮進指南插件」。 the screenshot

這是源代碼示例:

import threading 
import time 
class timer(threading.Thread): #The timer class is derived from the class threading.Thread 
    def __init__(self, num, interval): 
     threading.Thread.__init__(self) 
     self.thread_num = num 
     self.interval = interval 
     self.thread_stop = False 

    def run(self): #Overwrite run() method, put what you want the thread do here 
     while not self.thread_stop: 
      print 'Thread Object(%d), Time:%s/n' %(self.thread_num, time.ctime()) 
      time.sleep(self.interval) 
    def stop(self): 
     self.thread_stop = True 


def test(): 
    thread1 = timer(1, 1) 
    thread2 = timer(2, 2) 
    thread1.start() 
    thread2.start() 
    time.sleep(10) 
    thread1.stop() 
    thread2.stop() 
    return 

if __name__ == '__main__': 
    test() 

回答

32

自動縮進踢

禁用它最簡單的方法是::set paste

:help paste 

'paste'     boolean (default off)  
         global 
         {not in Vi} 
    Put Vim in Paste mode. This is useful if you want to cut or copy 
    some text from one window and paste it in Vim. This will avoid 
    unexpected effects. 
    Setting this option is useful when using Vim in a terminal, where Vim 
    cannot distinguish between typed text and pasted text. In the GUI, Vim 
    knows about pasting and will mostly do the right thing without 'paste' 
    being set. The same is true for a terminal where Vim handles the 
    mouse clicks itself. 
+0

非常感謝你。如果我打開粘貼模式,是否對其他東西有任何影響,例如編輯代碼等? – 2012-03-23 01:51:12

+0

它只是禁用所有與輸入文本格式相關的設置。見':help paste' – 2012-03-23 09:31:33

9

卡羅伊的答案是正確的關於paste選項。

然後,您可以在.vimrc添加映射到快速啓用/禁用「粘貼」選項:

例如,我用 set pastetoggle=<F10>

相關問題