简体   繁体   中英

emacs Python files backspace not working

Whenever I am in a buffer of an opened python file and press RET , it goes to the next line and indents by 16 (!) spaces. I don't know how it gets to that number internally, but whenever that happens, I cannot simply press backspace multiple times to undo the non-sense. Instead I get an error when I press backspace:

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

But I don't want any indentation calculation to happen at all, I simple want to remove characters before the cursor ...

So I added the following to my .emacs config file, hoping it would disable any special operations invoked by pressing backspace :

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

However, this didn't help.

I have some more tab related setting in my .emacs config file:

;; ################
;; # 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)

And here is my complete config file, in case it is needed:

;; 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

To summarize, I simply want the backspace key to work like it would in other editors:

  • deletes the character before the cursor
  • deletes a selection before the cursor
  • in all types of files / modes
  • nothing else, no indentation calculation or whatever

I am running emacs GNU Emacs 24.5.1 on a Fedora 22.

How can I achieve this in emacs?

Most modes (major or minor) will not redefine the <backspace> key, but they're all technically free to (See https://www.gnu.org/software/emacs/manual/html_node/elisp/Key-Binding-Conventions.html ). python-mode rebinds <backspace> (actually DEL , which <backspace> aliases to, which you can see from Ch k <backspace> ).

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

You need to do this for every mode that redefines <backspace> . foo-mode-map is the conventional name for foo-mode 's keymap, but some modes have multiple keymaps and/or different variable names. The string after with-eval-after-load is the name of the .el file; we need to run the code after it loads because the keymap is undefined before that.


To use a giant hammer, see this answer: https://stackoverflow.com/a/34404700/245173

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM