2010-07-02 39 views
5

我試圖從Vim切換到Emacs,但我正在試圖將其配置爲處理標籤,我希望如此。我要求:Emacs選項卡的全局配置

  • 插入的「標籤」被擴展爲兩個空格。不管我做什麼,Emacs固執地堅持八個。
  • 製表符(即實際\t個字符)在屏幕上由兩個空格表示。
  • 按TAB應該在光標處插入一個標籤,而不是縮進整行。目前,我在任何地方按Tab鍵,Emacs在行首開始處理所有空格;這是迄今爲止最令人憤慨的事情。

我現在~/.emacs讀取

(setq standard-indent 2) 
(setq-default indent-tabs-mode nil) 

但我沒有試過從網絡建議的配置,其中沒有什麼,他們說他們會做的結束。 (API是否不斷變化?我使用的是GNU Emacs 23.1.1,顯然。)

回答

7

Emacs對處理縮進的支持非常靈活。一般來說,你所使用的模式決定了它們是如何工作的 - 所以如果你正在處理一個C文件,那麼按壓標籤工作的方式將與在Python文件上工作的方式不同。

所以它取決於你在哪個模式下工作,這會限制你得到的答案。在大多數情況下,我會建議您不要反對它 - 對我來說,縮進行爲是emacs的最佳功能之一。但是,您需要花時間爲自己定製它。

要更改制表符的顯示方式,您需要將製表符寬度設置爲2.如果您正在編輯Java或C樣式代碼,那麼聽起來好像您想要將所有這些好的縮進功能都關閉到NIL :

  • C-製表總是縮進
  • C-語法壓痕
  • 縮進標籤模式

我建議你設置這些通過定製界面。如果您使用「M-x customize-group RET C」,則可以看到C模式的各種設置。

如果您正在編輯不同類型的文件,則說明會有所不同。

也許emacs的文件格式不正確。你可以嘗試做「M-x基本模式」,看看你是否喜歡那裏的行爲。

1

這應該讓你得到你想要的大部分。您可能需要自定義您通常使用的其他編程模式。

(defun insert-tab() 
    "self-insert-command doesn't seem to work for tab" 
    (interactive) 
    (insert "\t")) 
(setq indent-line-function 'insert-tab) ;# for many modes 
(define-key c-mode-base-map [tab] 'insert-tab) ;# for c/c++/java/etc. 
(setq-default tab-width 2) 
3
;; * Inserted "tabs" to be expanded into two spaces. Emacs stubbornly 
;; sticks to eight, no matter what I do. 

;; * Tabs (i.e. real \t characters) to be represented on screen by two 
;; spaces. 

(setq-default tab-width 2) 


;; * Pressing TAB should insert a tab at the cursor rather than indent 
;; the entire line. Currently, I press TAB anywhere and Emacs 
;; destroys all whitespace at the start of the line; this is the 
;; most infuriating thing so far. 

(setq-default indent-tabs-mode t) 

(mapcar (lambda (hooksym) 
      (add-hook hooksym 
        (lambda() 
         (kill-local-variable 'indent-tabs-mode) 
         (kill-local-variable 'tab-width) 
         (local-set-key (kbd "TAB") 'self-insert-command)))) 

     '(
      c-mode-common-hook 

      ;; add other hook functions here, one for each mode you use :-(
     )) 

;; How to know the name of the hook function? Well ... visit a file 
;; in that mode, and then type C-h v major-mode RET. You'll see the 
;; mode's name in the *Help* buffer (probably on the second line). 

;; Then type (e.g.) C-h f python-mode; you'll see blather about the 
;; mode, and (hopefully) somewhere in there you'll see (again e.g.) 
;; "This mode runs the hook `python-mode-hook', as the final step 
;; during initialization."