reddit / emacs

Spacemacs - How can I stop it from changing tabs into spaces when I press backspace?

pandagoespoop · 2022-10-05 21:39
Hello there pretty people, an emacs newbie here, and would love some help. When I insert a new line in my Java file, I get a lovely, sexy tab for indentation inserted automatically. When I press the backspace key to delete the tab, all my tabs are changed into spaces. This disgusts me. It angers me. I am close to homicidal. Does anyone know how to stop this? I have tried running "indent-tabs-mode t", but that does nothing, which makes sense, because tabs are already enabled. The functionality I want is simple. Tabs for indentation, spaces for alignment. When I delete a tab, it deletes the tab. When I delete a space, it deletes the space. I've been googling for quite a while, and can't seem to figure it out. Extra info - Emacs 28.2 snap package. Git cloned spacemacs just today after issues with doomemacs. &#x200B; **UPDATE - sort of solved it:** So I have been experimenting, and I have had to change a few things to get what I want. I am testing all this by writing java code and have lsp enabled, which may affect how indentation is decided. For some reason spaceemacs wants to indent by having a series of tabs first, followed by 4 spaces, which is completely against how any programmer should indent. With the config below, it still does this for some lines I add, but I can press tab again to convert it into a tab. I had to change a few variables using M-x customize-variable and typing the variable name. These are under the function emacs-custom-settings(). I added this to my .spacemacs file: (defun dotspacemacs/user-config () ;; My settings to override spaceemacs' weird indentation rules. ;;(global-set-key (kbd "<backspace>") 'backward-delete-char) ;; bad practice to override, but keep for info. (global-set-key (kbd "TAB") 'tab-to-tab-stop) ) (defun dotspacemacs/emacs-custom-settings () (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. '(backward-delete-char-untabify-method nil) ;; make the backspace delete a single character, instead of changing tabs to spaces. '(electric-indent-mode nil) ;; Stops it changing the indent as I type on the current line. '(global-hungry-delete-mode nil) ;; Turns hungry delete mode off, needed as I overwrote the backspace key earlier. '(indent-tabs-mode t) ;; set indent to tabs '(tab-always-indent t) ;; always insert tabs '(tab-width 8) ;; A sane tab length. ;; I want to see the whitespace, to make sure it's all working properly. '(global-whitespace-mode t) '(whitespace-style '(face trailing tabs spaces lines newline missing-newline-at-eof empty indentation space-after-tab space-before-tab space-mark tab-mark)) ) )
lebensterben · 2022-10-05 21:43
if you're homicidal because of Spacemacs you should stop using it, seriously.
pandagoespoop · 2022-10-05 21:53
Nah, it's alright. It's not Spacemacs. Just the spaces haha.
MitchellMarquez42 · 2022-10-06 01:12
Out of curiosity: what's the difference between indentation and alignment?
pandagoespoop · 2022-10-06 02:56
In this example below, I use a tab to indent all the lines in the 'if statement', and use spaces to make sure that the words all align underneath each other. If however I only used tabs, it would look different in different editors as people change the size of their tab characters. void doodies() { Tab______if( is_pooping && Tab______ is_almost_done && Tab______ does_roll_have_paper Tab______) { Tab______Tab______reach_for_loo_roll(); Tab______} } void dating() { Tab______if( boy_name == "bob" && girl_name == "bobbylina" && Tab______ boy_age == 23 && girl_age == 23 && Tab______ boy_height == 1.5 && girl_height == 1.5 && Tab______ boy_weight == 120 && girl_weight == 120 Tab______) { Tab______Tab______say_hi_sexy(); Tab______} }
Aminumbra · 2022-10-06 05:13
Just for future reference: you say that you have been googling for a while, but to help you with Emacs, google might be *worse* than Emacs sometimes. `C-h a` (`apropos-command`) or `C-h d` (`apropos-documentation`) called with arguments "tab space delete" returns, almost at the top of the results, the function `backward-delete-char-untabify`. Its documentation immediately mentions `backward-delete-char-untabify-method`, which is a (customizable) variable, which you set to `nil` instead of `untabify` (which is likely its current value) and *voilà* ! Another solution is to try to see which function exactly is run when you press backspace: `C-h k DEL` (`describe-key`) and read the documentation/source code to see where the "strange" behaviour is coming from.
pandagoespoop · 2022-10-06 15:31
Thank you! I still had to disable hungry delete too, as that was overriding the backward-delete-char-untabify nil I updated my post with the config I've ended up with. It's not perfect as spacemacs' auto indent seems to add spaces along with my tabs, but I can at least tab out of it and fix it :).