在Emacs中使用Python,如果我想添加一個try /除了一段代碼,我經常發現我不得不逐行縮進整個塊。在Emacs中,你如何縮小整個區塊。Python的Emacs批量縮進
我不是一個有經驗的Emacs用戶,只是發現它是通過ssh工作的最佳工具。我在命令行(Ubuntu)上使用Emacs,而不是gui,如果這有什麼區別的話。
在Emacs中使用Python,如果我想添加一個try /除了一段代碼,我經常發現我不得不逐行縮進整個塊。在Emacs中,你如何縮小整個區塊。Python的Emacs批量縮進
我不是一個有經驗的Emacs用戶,只是發現它是通過ssh工作的最佳工具。我在命令行(Ubuntu)上使用Emacs,而不是gui,如果這有什麼區別的話。
如果您使用Emacs編程Python,那麼您應該使用python模式。隨着蟒蛇模式,標誌着代碼塊,
C-c >
或C-c C-l
後,將這個區域4個空格向右
C-c <
或C-c C-r
轉變區域4位向左
如果您需要將代碼移動兩級縮進,或者某些任意數量,您可以在命令前添加一個參數:
C-u 8 C-c >
將區域右移8個空格
C-u 8 C-c <
移區域8米的空間向左
另一種替代方法是使用M-x indent-rigidly
這勢必C-x TAB
:
C-u 8 C-x TAB
移區域8米的空間向右
C-u -8 C-x TAB
移區域8左邊的空格
對文本矩形而不是文本行操作的rectangle commands也有用。
例如,標記的矩形區域後,
C-x r o
插入空格以填充矩形區域(有效地移位代碼向右)
C-x r k
殺死矩形區域(有效地移位代碼向左)
C-x r t
提示用字符串替換矩形。輸入C-u 8 <space>
將輸入8個空格。
PS。使用Ubuntu時,要使python模式成爲所有.py文件的默認模式,只需安裝python-mode
軟件包即可。
indent-region
映射到C-M-\
應該做的伎倆。
這是個可怕的建議。無法推斷Python縮進(因爲它是句法!),所以ident-region是無用的。 – 2016-01-05 03:36:26
除了默認映射到C-M-\
的indent-region
以外,矩形編輯命令對Python非常有用。標記區域爲正常,則:
C-x r t
(string-rectangle
):將提示輸入要插入到各行的字符;非常適合插入一定數量的空格C-x r k
(kill-rectangle
):刪除矩形區域;非常適合去除縮進您也可以C-x r y
(yank-rectangle
),但這只是很少用處。
我做這樣的事情普遍
;; intent whole buffer
(defun iwb()
"indent whole buffer"
(interactive)
;;(delete-trailing-whitespace)
(indent-region (point-min) (point-max) nil)
(untabify (point-min) (point-max)))
由於whitespace是Python語法的一部分,整個文件的縮進區域是一個壞主意。 – 2010-04-06 15:21:51
我一直在使用這個功能來處理我的縮進和取消縮進:
(defun unindent-dwim (&optional count-arg)
"Keeps relative spacing in the region. Unindents to the next multiple of the current tab-width"
(interactive)
(let ((deactivate-mark nil)
(beg (or (and mark-active (region-beginning)) (line-beginning-position)))
(end (or (and mark-active (region-end)) (line-end-position)))
(min-indentation)
(count (or count-arg 1)))
(save-excursion
(goto-char beg)
(while (< (point) end)
(add-to-list 'min-indentation (current-indentation))
(forward-line)))
(if (< 0 count)
(if (not (< 0 (apply 'min min-indentation)))
(error "Can't indent any more. Try `indent-rigidly` with a negative arg.")))
(if (> 0 count)
(indent-rigidly beg end (* (- 0 tab-width) count))
(let (
(indent-amount
(apply 'min (mapcar (lambda (x) (- 0 (mod x tab-width))) min-indentation))))
(indent-rigidly beg end (or
(and (< indent-amount 0) indent-amount)
(* (or count 1) (- 0 tab-width))))))))
然後我把它分配給鍵盤快捷鍵:
(global-set-key (kbd "s-[") 'unindent-dwim)
(global-set-key (kbd "s-]") (lambda() (interactive) (unindent-dwim -1)))
我是Emacs的新手,所以這個答案可能接近無用。
到目前爲止所提及的答案都不包括文字的重新縮進,如dict
或list
。例如。 M-x indent-region
或M-x python-indent-shift-right
和公司是不會幫助,如果你已經剪切和粘貼下面的文字,需要它來理智地重新縮進:
foo = {
'bar' : [
1,
2,
3 ],
'baz' : {
'asdf' : {
'banana' : 1,
'apple' : 2 } } }
感覺就像M-x indent-region
應python-mode
理智地做一些事情,但那還不是如此。
對於文字被括起來的特定情況,使用有問題的行上的TAB可以得到你想要的(因爲空白不起作用)。
所以,我一直在做在這樣的情況下,迅速記錄keyboard macro像<f3> C-n TAB <f4>
如F3,CTRL-N(或向下箭頭),TAB,F4,然後使用F4反覆應用宏可以省下按鍵的幾個。或者你可以做C-u 10 C-x e
來應用它10次。 (我知道這聽起來不是很多,但是嘗試重新縮進100行垃圾文字而不丟失向下箭頭,然後不得不重複5行並重復這些操作))。
我使用下面的代碼片段。在選項卡上,當選項處於非活動狀態時,它縮進當前行(正常情況下);當選擇不活動時,它會將整個區域縮進到右側。
(defun my-python-tab-command (&optional _)
"If the region is active, shift to the right; otherwise, indent current line."
(interactive)
(if (not (region-active-p))
(indent-for-tab-command)
(let ((lo (min (region-beginning) (region-end)))
(hi (max (region-beginning) (region-end))))
(goto-char lo)
(beginning-of-line)
(set-mark (point))
(goto-char hi)
(end-of-line)
(python-indent-shift-right (mark) (point)))))
(define-key python-mode-map [remap indent-for-tab-command] 'my-python-tab-command)
'C-c>'正確。 'C-c <'左邊 – 2016-10-20 20:14:03