2010-02-12 18 views

Respuesta

12

Esto elimina 4 espacios del frente de la línea actual (suponiendo que existan espacios).

(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 ""))))) 

Si la variable tab-width pasa a ser 4 (y eso es lo que desea "deshacer"), entonces se puede sustituir el (looking-at "^ ") con algo más general como (concat "^" (make-string tab-width ?\)).

Además, el código convertirá las pestañas en el frente de la línea a espacios usando untabify.

+1

¡Guau! Pero usaría WHEN en lugar de IF, ya que no hay ninguna cláusula else. – Ken

+1

para mí, no funciona con una región seleccionada. Solo con la primera línea: -/ – Ismael

+0

Para mí, no funciona en absoluto. :( –

40

Para hacer eso, uso el comando indent-rigidly, vinculado a C-x TAB. Dale el argumento -4 para mover la región seleccionada hacia la izquierda por cuatro espacios: C-u -4 C-x TAB.

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

+2

Por la misma entrada manual, es posible que prefiera 'indent-code-rigidly 'como deja solo comentarios y cadenas de varias líneas – toolbear

5

hice algunas funciones para la tabulación de una región a lo largo de cuatro espacios hacia la izquierda o la derecha dependiendo de si pestaña o shift + tab utiliza:

(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) 

Editar: Código de Agregado Maven para compruebe si está en el minibúfer, así como para completar la tabulación en almacenamientos intermedios específicos (shell, por ejemplo).

+1

Bueno, la respuesta aceptada solo funcionaba para 1 línea, no para las regiones –

+1

Perfecto. Funciona para regiones y líneas. Replica otras funcionalidades comunes del editor. – mythicalcoder

1

He mejorado la respuesta de Stanley Bak. Para mí, este enlace de clave global estaba arruinando la finalización del minibúfer.

Así que he incluido un estuche para minibúfer también.

Editar: Renombrar indent-region ->indent-region-custom.

indent-region está en conflicto con el comando existente y da error para sangría al guardar (enganchar antes de guardar) y algunas otras combinaciones de teclas.

(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) 
Cuestiones relacionadas