Following up emacs TAB indentation with TABs, not spaces - emacs

If my .emacs contains just the lines
(setq-default indent-tabs-mode nil)
(add-hook 'text-mode-common-hook
(lambda () (setq indent-tabs-mode t)))
I am expecting that if I edit a file list.txt containing
<tab> - Item 1
<tab> - Item 2
and press return after Item 2, a tab will be inserted on the next line, and not eight spaces.
But I get eight spaces, not a TAB.
How do I modify the .emacs above (for 24.5, if it matters) so that I get TABs?
Needless to say, I am trying to avoid C-q TAB. The idea is to automate the insertion of C-q TAB after a return, when needed.
Does something so simple really require smart-tabs?
I have asked this question in the past and was told that a .emacs such as the one above would solve the problem. It doesn't.

text-mode-common-hook doesn't seem to exist. There is text-mode-hook, though.
This should work:
(setq-default indent-tabs-mode nil)
(add-hook 'text-mode-hook
(lambda () (setq indent-tabs-mode t)))

Related

How to revert indent-tabs-mode when editing makefiles in emacs?

I've got the following in my .emacs since for most source code I use spaces only:
(add-hook 'prog-mode-hook
(lambda ()
(setq indent-tabs-mode nil)))
This messes up my makefiles though since they require tabs. How do I work around this so that I get spaces by default for all source code, but makefiles retain tabs?
You can always add an add-hook for makefile-mode to change it back.
(add-hook 'makefile-mode-hook
(lambda ()
(setq indent-tabs-mode t)))

In Emacs, while editing a CMake file, how to turn off smart indentation?

I just want each line to get the same indentation as the previous line, and that TAB would indent 4 spaces. For C++ I managed it with:
(setq-default indent-tabs-mode nil)
(setq-default c-syntactic-indentation nil)
(setq-default c-basic-offset 4)
In my .emacs, but for CMake files Emacs just indents the lines automatically according to its own rules, and TAB has no effect at all.
What you probably need to disable is known as electric-indent-mode which is the function which indents your code when you press return. To disable it for cmake-mode, as in this answer is to include the following line in your init
(add-hook 'cmake-mode-hook (lambda () (electric-indent-local-mode -1)))

How to turn off ‘tab’ highlighting in go buffers?

I’ve just started writing Go programs in Emacs. How can I turn off tabs highlighting in go-mode buffers? I use «whitespace» for whitespace chars highlighting. Go btw is the only mode where I don’t want tabs highlighted since tabs are standard formatting in Go.
Sincerely, Pavel.
To be clear, you're doing something like
(require 'whitespace)
(global-whitespace-mode t)
right? You can disable whitespace-mode for go-mode with
(setq whitespace-global-modes '(not go-mode))
There is a related question on emacs stack exchange.
I found that this
(add-hook 'go-mode-hook
(lambda ()
(add-hook 'before-save-hook 'gofmt-before-save)
(setq whitespace-style '(face empty trailing lines-tail))
(setq tab-width 4)
(setq indent-tabs-mode 1)))
Worked a bit better for me. Leaves whitespace-mode on but doesn't highlight tabs. Also runs go fmt before save and sets tab width to 4. I'm using prelude.
Add this line
(whitespace-toggle-options '(tabs)))
To your go-mode hook e.g
(use-package go-mode
:preface
(defun go-mode-config ()
(whitespace-toggle-options '(tabs)))
:config
(add-hook 'go-mode-hook (lambda ()
(go-mode-config))))
Taken from prelue go config

How to safely override tab key just for indentation in emacs

I'm a new user to emacs, and I don't particularly like the way emacs modes handle indentation, especially when mixing modes (say ASP and perl). I have written the following functions to indent things the way a "classic" editor would:
(defun classic-indent (width)
"Tab the current line or block the 'classic' way"
(save-excursion
(if (not (use-region-p)) (select-current-line))
(indent-rigidly (mark) (point) width)))
(defun indent-forward ()
"tab two space forward"
(interactive)
(classic-indent 2))
(defun indent-back ()
"tab two spaces back"
(interactive)
(classic-indent -2))
(defun select-current-line ()
"Select the current line"
(interactive)
(end-of-line) ; move to end of line
(set-mark (line-beginning-position)))
The idea here being to bind indent-back to <backtab> and indent-forward to <tab>. The functions work great when calling them with M-x, and the <backtab> binding works fine, but if I try to bind <tab> directly, it interfere's with all sorts of cool things like auto-completion. I tried setting indent-line-function with:
(setq indent-line-function 'indent-forward)
and setting my major modes indentation function with:
(setq cperl-indent-command 'indent-forward)
but neither has any effect. I'm not sure if I'm setting them wrong or if this is even the right approach.
to sum up, How can I override indentation with the tab key without clobbering other 'tab' behavior like auto-completion?
Emacs wiki has a whole category page about indentation and TAB: http://www.emacswiki.org/emacs/CategoryIndentation. See that for other wiki pages about libraries or snippets that give you various kinds of TAB DWIM ("smart" TAB) behavior. Here are a couple:
http://www.emacswiki.org/emacs/TabCompletion
http://www.emacswiki.org/emacs/IndentOrInsertTab
It's likely the major-mode default is overriding your setting when you load a buffer. Do something like this ensure your indent-line-function is used buffer-local and it will work.
(add-hook 'elixir-mode-hook
(lambda ()
(electric-indent-mode -1)
(setq indent-line-function 'indent-forward)))
ref: https://www.emacswiki.org/emacs/BufferLocalVariable

How to change indentation in text-mode for emacs

I'm in text mode and want my tab key to indent a line to two spaces.
The file looks like this:
Line one
Line two
The cursor is situated before the 'L' : "Line two", and I hit TAB and it gets indented 6 spaces as opposed to the desired 2 spaces.
Actions I've tried:
I've tried updating the variable: tab-stop-list
(setq tab-stop-list '(2 4 6 8 10 12 14 16))
I've tried adding a text-mode-hook
(add-hook 'text-mode-hook
'(lambda ()
(setq tab-width 2)))
Add this to your .emacs :
(add-hook 'text-mode-hook
'(lambda ()
(setq indent-tabs-mode nil)
(setq tab-width 2)
(setq indent-line-function (quote insert-tab))))
See Emacs Indentation Tutorial.
The default for in text-mode will indent to the first non-whitespace character in the line above it.
From the key binding documentation in text mode
TAB (translated from ) runs the command indent-for-tab-command,
which is an interactive compiled Lisp function in `indent.el'.
It is bound to TAB.
(indent-for-tab-command &optional ARG)
Indent line or region in proper way for current major mode or insert a tab.
Depending on `tab-always-indent', either insert a tab or indent.
In most major modes, if point was in the current line's indentation,
it is moved to the first non-whitespace character after indenting;
otherwise it stays at the same position in the text....
Luckily, this can be changed. Adding the following to your text-mode-hook should do what you need:
(setq tab-width 2)
(setq indent-line-function (quote insert-tab))
Try setting
(setq standard-indent 2)
In your .emacs