Подтвердить что ты не робот

Emacs Shift-Tab влево сдвигает блок

Как я могу получить Emacs в Shift Tab, чтобы переместить выделенный текст влево на 4 пробела?

4b9b3361

Ответ 1

Это удаляет 4 пробела из передней части текущей строки (при условии, что существуют пробелы).

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

Если вашей переменной tab-width оказывается 4 (и что вы хотите "отменить" ), вы можете заменить (looking-at "^ ") на нечто более общее, например (concat "^" (make-string tab-width ?\ )).

Кроме того, код преобразует вкладки в начале строки в пробелы, используя untabify.

Ответ 2

Для этого я использую команду indent-rigidly, привязанную к C-x TAB. Дайте ему аргумент -4 для перемещения выбранной области влево четырьмя пробелами: C-u -4 C-x TAB.

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

Ответ 3

Я сделал некоторые функции для табуляции области над четырьмя пробелами влево или вправо в зависимости от того, используете ли вы вкладку или 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 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)

Изменить. Добавлен код Maven для проверки в минибуфере, а также для заполнения вкладки в определенных буферах (например, оболочке).

Ответ 4

Я улучшил ответ Стэнли Бака. Для меня эта глобальная привязка ключей испортила завершение минибуфера.

Итак, я включил случай для минибуфера.

Изменить: переименование indent-regionindent-region-custom.

indent-region сталкивается с существующей командой и дает ошибку для отступа при сохранении (перехват для сохранения до сохранения) и некоторые другие комбинации клавиш.

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