2010-02-12 24 views

Trả lời

12

Thao tác này sẽ xóa 4 dấu cách khỏi mặt trước của dòng hiện tại (miễn là không gian tồn tại).

(global-set-key (kbd "<S-tab>") 'un-indent-by-removing-4-spaces) 
(defun un-indent-by-removing-4-spaces() 
    "remove 4 spaces from beginning of of line" 
    (interactive) 
    (save-excursion 
    (save-match-data 
     (beginning-of-line) 
     ;; get rid of tabs at beginning of line 
     (when (looking-at "^\\s-+") 
     (untabify (match-beginning 0) (match-end 0))) 
     (when (looking-at "^ ") 
     (replace-match ""))))) 

Nếu biến của bạn tab-width sẽ xảy ra là 4 (và đó là những gì bạn muốn "undo"), sau đó bạn có thể thay thế (looking-at "^ ") với một cái gì đó tổng quát hơn như (concat "^" (make-string tab-width ?\)).

Ngoài ra, mã sẽ chuyển đổi các tab ở mặt trước của dòng thành không gian bằng cách sử dụng untabify.

+1

Tốt! Nhưng tôi sẽ sử dụng WHEN thay vì IF, vì không có mệnh đề nào khác. – Ken

+1

đối với tôi, nó không hoạt động với một vùng được chọn. Chỉ với dòng đầu tiên: -/ – Ismael

+0

Đối với tôi, nó không hoạt động chút nào. :( –

40

Để làm điều đó, tôi sử dụng lệnh indent-rigidly, được liên kết với C-x TAB. Đặt đối số -4 để di chuyển vùng đã chọn sang bên trái theo bốn khoảng trắng: C-u -4 C-x TAB.

http://www.gnu.org/s/emacs/manual/html_node/elisp/Region-Indent.html

+2

Theo cùng một mục nhập thủ công, bạn có thể thích 'thụt lề-mã-cứng nhắc 'vì nó để lại các chú thích và các chuỗi nhiều dòng một mình – toolbear

5

tôi đã thực hiện một số chức năng cho tabbing một khu vực trên bằng bốn không gian sang trái hoặc phải tùy thuộc vào nếu bạn sử dụng tab hoặc shift + tab:

(defun indent-region-custom(numSpaces) 
    (progn 
     ; default to start and end of current line 
     (setq regionStart (line-beginning-position)) 
     (setq regionEnd (line-end-position)) 

     ; if there's a selection, use that instead of the current line 
     (when (use-region-p) 
      (setq regionStart (region-beginning)) 
      (setq regionEnd (region-end)) 
     ) 

     (save-excursion ; restore the position afterwards    
      (goto-char regionStart) ; go to the start of region 
      (setq start (line-beginning-position)) ; save the start of the line 
      (goto-char regionEnd) ; go to the end of region 
      (setq end (line-end-position)) ; save the end of the line 

      (indent-rigidly start end numSpaces) ; indent between start and end 
      (setq deactivate-mark nil) ; restore the selected region 
     ) 
    ) 
) 

(defun untab-region (N) 
    (interactive "p") 
    (indent-region-custom -4) 
) 

(defun tab-region (N) 
    (interactive "p") 
    (if (active-minibuffer-window) 
     (minibuffer-complete) ; tab is pressed in minibuffer window -> do completion 
    ; else 
    (if (string= (buffer-name) "*shell*") 
     (comint-dynamic-complete) ; in a shell, use tab completion 
    ; else 
    (if (use-region-p) ; tab is pressed is any other buffer -> execute with space insertion 
     (indent-region-custom 4) ; region was selected, call indent-region 
     (insert " ") ; else insert four spaces as expected 
    ))) 
) 

(global-set-key (kbd "<backtab>") 'untab-region) 
(global-set-key (kbd "<tab>") 'tab-region) 

Sửa: Mã gia tăng của Maven để kiểm tra xem trong minibuffer, cũng như cho tab hoàn thành trong bộ đệm cụ thể (ví dụ: shell).

+1

Tốt, câu trả lời được chấp nhận chỉ làm việc cho 1 dòng, không phải cho các vùng –

+1

Hoàn hảo. Làm việc cho các vùng và các dòng. – mythicalcoder

1

Tôi đã cải thiện câu trả lời của Stanley Bak. Đối với tôi, sự ràng buộc quan trọng toàn cầu này đã làm rối tung hoàn thành minibuffer.

Vì vậy, tôi cũng đã đưa trường hợp cho minibuffer.

Chỉnh sửa: Đổi tên indent-region ->indent-region-custom.

indent-region đang xung đột với lệnh hiện tại và cung cấp lỗi khi thụt lề (lưu trước khi lưu) và một số tổ hợp phím khác.

(defun indent-region-custom(numSpaces) 
    (progn 
    ;; default to start and end of current line 
    (setq regionStart (line-beginning-position)) 
    (setq regionEnd (line-end-position)) 
    ;; if there's a selection, use that instead of the current line 
    (when (use-region-p) 
     (setq regionStart (region-beginning)) 
     (setq regionEnd (region-end)) 
    ) 

    (save-excursion ; restore the position afterwards 
     (goto-char regionStart) ; go to the start of region 
     (setq start (line-beginning-position)) ; save the start of the line 
     (goto-char regionEnd) ; go to the end of region 
     (setq end (line-end-position)) ; save the end of the line 

     (indent-rigidly start end numSpaces) ; indent between start and end 
     (setq deactivate-mark nil) ; restore the selected region 
    ) 
    ) 
) 

(defun untab-region (N) 
    (interactive "p") 
    (indent-region-custom -4) 
) 

(defun tab-region (N) 
    (interactive "p") 
    (if (active-minibuffer-window) 
     (minibuffer-complete) ; tab is pressed in minibuffer window -> do completion 
    (if (use-region-p) ; tab is pressed is any other buffer -> execute with space insertion 
     (indent-region-custom 4) ; region was selected, call indent-region-custom 
     (insert " ") ; else insert four spaces as expected 
    ) 
    ) 
) 

(global-set-key (kbd "<backtab>") 'untab-region) 
(global-set-key (kbd "<tab>") 'tab-region) 
Các vấn đề liên quan