python - emacs Python 文件退格键不起作用

标签 python emacs key-bindings backspace

每当我在打开的 python 文件的缓冲区中并按 RET 时,它就会转到下一行并缩进 16 (!) 个空格。我不知道它内部是如何得到这个数字的,但每当发生这种情况时,我不能简单地多次按 backspace 来撤消无意义的操作。相反,当我按退格键时出现错误:

python-indent-calculate-indentation: Wrong type argument: integer-or-marker-p, tab-width [x times]

但我根本不希望发生任何缩进计算,我只是想删除光标之前的字符...

因此,我将以下内容添加到我的 .emacs 配置文件中,希望它能够禁用通过按 backspace 调用的任何特殊操作:

(global-unset-key [?\C-h])
(global-set-key [?\C-h] 'delete-backward-char)

但是,这并没有帮助。

我的 .emacs 配置文件中有更多与选项卡相关的设置:

;; ################
;; # TAB SETTINGS #
;; ################
;; set default tab char's display width to 4 spaces
(setq-default tab-width 4) ; emacs 23.1, 24.2, default to 8
(setq-default indent-tabs-mode t)
(setq-default tab-stop-list (number-sequence 4 200 4))
(setq-default tab-width 4)
(setq-default python-indent 'tab-width)
(setq-default python-indent-offset 'tab-width)
(setq-default python-indent-levels (number-sequence 4 200 4))
(setq-default python-indent-guess-indent-offset nil)

(defvaralias 'c-indent-level 'tab-width)
(defvaralias 'c-basic-offset 'tab-width)
(defvaralias 'sgml-indent-level 'tab-width)
(defvaralias 'sgml-basic-offset 'tab-width)

这是我的完整配置文件,以防需要:

;; deactivate version control integration, so that emacs starts up faster
(setq vc-handled-backends ())
(setq-default vc-handled-backends nil)
(eval-after-load "vc" '(remove-hook 'find-file-hooks 'vc-find-file-hook))

;; Installation of el-get
(add-to-list 'load-path "~/.emacs.d/el-get/el-get")
    (unless (require 'el-get nil 'noerror)
        (with-current-buffer
            (url-retrieve-synchronously "https://raw.githubusercontent.com/dimitri/el-get/master/el-get-install.el")
            (goto-char (point-max))
            (eval-print-last-sexp)))
(add-to-list 'el-get-recipe-path "~/.emacs.d/el-get-user/recipes")
(el-get 'sync)

;; enable company mode, always
(add-hook 'after-init-hook 'global-company-mode)

;; COMPANY SETTINGS
(setq-default company-idle-delay 0.1)


(setq-default scroll-preserve-screen-position t) ;; go back to line with the cursor after scrolling, if that line is on the screen again


;; other stuff
(custom-set-variables
 ;; custom-set-variables was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 '(ansi-color-names-vector ["#212526" "#ff4b4b" "#b4fa70" "#fce94f" "#729fcf" "#ad7fa8" "#8cc4ff" "#eeeeec"])
 '(custom-enabled-themes (quote (seti)))
 '(custom-safe-themes (quote ("94ba29363bfb7e06105f68d72b268f85981f7fba2ddef89331660033101eb5e5" default)))
 '(python-shell-buffer-name "Python Console")
 '(show-paren-mode t)
 '(tab-width 4))

(custom-set-faces
 ;; custom-set-faces was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 '(cursor ((t (:background "gold" :foreground "#151718"))))
 '(show-paren-match ((t (:background "#000000" :foreground "spring green" :underline nil :weight ultra-bold))))
 '(show-paren-mismatch ((t (:underline (:color "#CE4045" :style wave))))))

;; MELPA
(require 'package) ;; You might already have this line
(add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/"))
(add-to-list 'package-archives '("melpa-stable" . "https://melpa.org/packages/"))
(when (< emacs-major-version 24)
  ;; For important compatibility libraries like cl-lib
  (add-to-list 'package-archives '("gnu" . "http://elpa.gnu.org/packages/")))
(package-initialize) ;; You might already have this line



; SAVE SESSIONS - save sessions to restore buffers on next startup
(desktop-save-mode 1)
;; remember cursor position in file
(require 'saveplace)
(setq-default save-place t)


; load theme on startup
(add-hook 'emacs-startup-hook (load-theme 'seti))

;; turn on highlighting current line
(global-hl-line-mode 1)
(set-face-background 'hl-line "#303030")
(set-face-foreground 'highlight nil) ;; To keep syntax highlighting in the current line


;; highlight matching parenthesis
(show-paren-mode 1)
(setq-default show-paren-delay 0) ;; 0 delay
(setq-default show-paren-style 'parenthesis) ;;'parenthesis is another possible value, only highlighting the brackets



;; ################
;; # TAB SETTINGS #
;; ################
;; set default tab char's display width to 4 spaces
(setq-default tab-width 4) ; emacs 23.1, 24.2, default to 8
(setq-default indent-tabs-mode t)
(setq-default tab-stop-list (number-sequence 4 200 4))
(setq-default tab-width 4)
(setq-default python-indent 'tab-width)
(setq-default python-indent-offset 'tab-width)
(setq-default python-indent-levels (number-sequence 4 200 4))
(setq-default python-indent-guess-indent-offset nil)

(defvaralias 'c-indent-level 'tab-width)
(defvaralias 'c-basic-offset 'tab-width)
(defvaralias 'sgml-indent-level 'tab-width)
(defvaralias 'sgml-basic-offset 'tab-width)

(add-hook 'python-mode-hook
(lambda ()
        (setq indent-line-function 'insert-tab)
        (setq-default indent-tabs-mode t)
        (setq-default tab-width 4)
        (setq-default python-indent 'tab-width)
        (setq-default python-indent-offset 'tab-width)
        (setq-default python-indent-levels (number-sequence 4 200 4))
        (setq-default python-indent-guess-indent-offset nil)
))

;; FONT SETTINGS
(set-default-font "Ubuntu Mono 11")

;; SCROLL SPEED
(setq-default mouse-wheel-scroll-amount '(2 ((shift) . 2))) ;; two lines at a time    
(setq-default mouse-wheel-progressive-speed nil) ;; don't accelerate scrolling
(setq-default mouse-wheel-follow-mouse 't) ;; scroll window under mouse    
(setq-default scroll-step 2) ;; keyboard scroll one line at a time


;; case-insensitive minibuffer completion
(setq read-buffer-completion-ignore-case t)
(setq read-file-name-completion-ignore-case t)

;; LINE NUMBERS
(require 'linum)
(global-linum-mode t)


;; DELETE SELECTED TEXT WHEN TYPING
(delete-selection-mode 1)

;; NEO TREE VIEW
(add-to-list 'load-path "/home/xiaolong/.emacs.d/elpa/neotree-20160306.730/neotree.el")
(require 'neotree)
(global-set-key [f8] 'neotree-toggle)
(setq neo-smart-open t)


;; AUTO-COMPLETE
(ac-config-default)
(setq ac-delay 0.25)
(define-key ac-completing-map (kbd "RET") 'ac-stop)


;; #########################
;; # SELF DEFINED FUNCTION #
;; #########################
;; DEFINE A FUNCTION FOR DUPLICATING A LINE
(defun duplicate-line()
  (interactive)
  (move-beginning-of-line 1)
  (kill-line)
  (yank)
  (newline)
  (yank)
  )


;; ORG MODE SHIFT SELECT
(setq-default org-support-shift-select t)

;; INDENT / UNINDENT REGION
(defun my-indent-region (N)
  (interactive "p")
  (if (use-region-p)
      (progn (indent-rigidly (region-beginning) (region-end) (* N 4))
             (setq deactivate-mark nil))
    (self-insert-command N)))

(defun my-unindent-region (N)
  (interactive "p")
  (if (use-region-p)
      (progn (indent-rigidly (region-beginning) (region-end) (* N -4))
             (setq deactivate-mark nil))
    (self-insert-command N)))

;; ### Hoooks for Haskell ###
(add-hook 'haskell-mode-hook 'turn-on-haskell-doc-mode)
(add-hook 'haskell-mode-hook 'turn-on-haskell-indentation)

;; ### Function for commenting and uncommenting lines ###

(defun comment-or-uncomment-line-or-region ()
  "Comments or uncomments the current line or region."
  (interactive)
  (if (region-active-p)
    (comment-or-uncomment-region (region-beginning) (region-end))
    (comment-or-uncomment-region (line-beginning-position) (line-end-position))
  )
)

;; ###################
;; # KEY DEFINITIONS #
;; ###################
(global-set-key (kbd "RET") nil)
(define-key global-map (kbd "RET") 'newline-and-indent)
(define-key global-map (kbd "<C-return>") 'newline)
(define-key global-map (kbd "C-b") 'elpy-goto-definition)
(global-set-key (kbd "<C-mouse-5>") (lambda () (interactive) (text-scale-decrease 1)))
(global-set-key (kbd "<C-mouse-4>") (lambda () (interactive) (text-scale-increase 1)))
(global-set-key [C-kp-add] 'text-scale-increase)
(global-set-key [C-kp-subtract] 'text-scale-decrease)
(global-set-key (kbd "<dead-circumflex>") "^")
(global-set-key (kbd "<S-dead-grave>") "`")
(global-set-key (kbd "<dead-acute>") "´")
(global-set-key (kbd "<C-tab>") 'company-complete-common)
; (global-set-key (kbd "C-SPC") 'jedi:complete)
(require 'multiple-cursors)
(global-set-key (kbd "C-S-c C-S-c") 'mc/edit-lines)
(global-set-key (kbd "C-n") 'set-mark-command)
(global-set-key (kbd "C-d") 'duplicate-line)
(global-set-key (kbd "C->") 'my-indent-region)
(global-set-key (kbd "C-<") 'my-unindent-region)
;;(global-set-key (kbd "TAB") 'self-insert-command)
;;(keyboard-translate (kbd "<backtab>") (kbd "C-u -4 C-x TAB"))
(global-set-key [f8] nil)
(global-set-key [f8] 'neotree-toggle)
(global-set-key (kbd "C-M-SPC") nil)
(global-set-key (kbd "<C-kp-divide>") 'comment-or-uncomment-line-or-region) ;; ### comment and uncomment lines

(global-unset-key [?\C-h])  ; this is for the backspace key
(global-set-key [?\C-h] 'delete-backward-char)  ; this is for the backspace key

总而言之,我只是希望退格键像在其他编辑器中一样工作:

  • 删除光标之前的字符
  • 删除光标之前的选择
  • 在所有类型的文件/模式中
  • 没有别的,没有缩进计算或其他什么

我正在 Fedora 22 上运行 emacs GNU Emacs 24.5.1

如何在 emacs 中实现此目的?

最佳答案

大多数模式(主要或次要)不会重新定义 <backspace>关键,但它们在技术上都是免费的(参见 https://www.gnu.org/software/emacs/manual/html_node/elisp/Key-Binding-Conventions.html )。 python-mode重新绑定(bind)<backspace> (实际上是 DEL ,它是 <backspace> 的别名,您可以从 C-h k <backspace> 中看到)。

(with-eval-after-load "python"
  (define-key python-mode-map (kbd "DEL") nil))

您需要对每个重新定义 <backspace> 的模式执行此操作。 foo-mode-mapfoo-mode 的常规名称的键盘映射,但某些模式具有多个键盘映射和/或不同的变量名称。 with-eval-after-load之后的字符串是 .el 文件的名称;我们需要在加载后运行代码,因为在此之前键盘映射未定义。

<小时/>

要使用巨锤,请参阅此答案:https://stackoverflow.com/a/34404700/245173

关于python - emacs Python 文件退格键不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37532303/

相关文章:

linux - 绑定(bind)到快捷键的 xdotool 命令不起作用

emacs - GNU Emacs 中的字母间距

java - 如何编辑 F2 的 JTable 键绑定(bind)

python - 删除 Pandas 后如何更新分层索引?

python - 在python中实现COM接口(interface)类型库

Emacs:如何让它成为午夜指挥官的编辑器?

emacs - kill-word/forward-word 应该在换行处停止

使用 JFileChooser 进行 Java 键绑定(bind)

javascript - 如何从 Microsoft 更新目录网页获取更新二进制 URL?

python - 以python中的for循环结束