How to change indentation in text-mode for emacs - 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

Related

Following up emacs TAB indentation with TABs, not spaces

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

How do I disable electric indent on RET but still keep other electric characters (e.g., ‘{’)?

In Emacs 24.4, the default indentation behavior has been changed—new lines are now automatically indented. From the release notes:
*** `electric-indent-mode' is now enabled by default.
Typing RET reindents the current line and indents the new line.
`C-j' inserts a newline but does not indent. In some programming modes,
additional characters are electric (eg `{').
I prefer the old behavior, so I added
(electric-indent-mode 0)
to my .emacs file. However, this disables all electric characters, which is not what I intended.
Is there any way to disable the new behavior while still having characters like ‘{’ or ‘:’ trigger an indentation?
You want to remove ?\n from electric-indent-chars. You can do this globally with:
(setq electric-indent-chars (remq ?\n electric-indent-chars))
or only in a particular mode (e.g. C):
(add-hook 'c-mode-hook
(lambda ()
(setq-local electric-indent-chars (remq ?\n electric-indent-chars))))
By checking the documentation for c-electric-brace, I found that the behavior of electric characters is controlled by the buffer-local variable c-electric-flag. It worked after I added the following lines to my .emacs file:
(add-hook 'c-mode-hook
(lambda ()
(set 'c-electric-flag t)))
(add-hook 'c++-mode-hook
(lambda ()
(set 'c-electric-flag t)))

Emacs: highlight 2 or more empty lines

I have entered the following code in my .emacs file to highlight unwanted white spaces.
(require 'whitespace)
(setq whitespace-style '(face empty tabs lines-tail trailing))
(global-whitespace-mode t)
This shows (1) empty lines at the beginning & end of buffer
(2) tabs
(3) lines which go over the 80 character limit
(4) trailing white spaces
I would like emacs to automatically highlight '2 or more empty lines'. Any ideas on how to implement this? I did find a blog post explaining a way to do this with the help of regexp, but I am not sure how to implement this in .emacs file.
Edit 1: Found a way to delete extra blank lines but this still doesn't help me with highlighting multiple blank lines automatically. delete extra blank lines in emacs
Edit 2: Adding the following to .emacs seems to work, but only after I save and reopen file in a buffer.
(add-hook 'change-major-mode-hook '(lambda () (highlight-regexp "\\(^\\s-*$\\)\n" 'hi-yellow)))
Edit 3: After adding (global-hi-lock-mode 1) to .emacs file just before the line in Edit 2, it seems to highlight 1 or more empty lines within the buffer. I am not sure how to modify the regexp so that it will only accept 2 or more empty lines.
Just use library Highlight (highlight.el). That's what it's for.
Use command hlt-highlight-regexp-region (C-x X h x) or hlt-highlight-regexp-to-end (C-x X h e). (To unhighlight a regexp, use C-x X u x or C-x X u e.)
Interactively, you input the regexp to use as usual in Emacs (with C-q C-j to match a newline character, and no need for double backslashes), so you type \(^\s-*$\) C-q C-j.
Your highlight-regexp-solution can be made into a minor-mode with the following elisp (e.g., in your .emacs file).
You can activate the minor mode by right-clicking onto one of the mode-names in the mode-line and then selecting nl2-mode. You can deactivate the minor mode by clicking on nl2 in the mode line and selecting Turn off minor mode.
To understand the code see the help for define-minor-mode and define-key (e.g., C-h f define-minor-mode RET). Note, that in emacs also mouse clicks in menus count as key strokes.
(define-minor-mode nl2-mode
"Highlight two successive newlines."
:global t
:lighter " nl2"
(if nl2-mode
(highlight-regexp "\\(^\\s-*$\\)\n" 'hi-yellow)
(unhighlight-regexp "\\(^\\s-*$\\)\n")))
(define-key mode-line-mode-menu [nl2-mode]
`(menu-item ,(purecopy "nl2-mode") nl2-mode
:help "Highlight two succesive newlines."
:button (:toggle . (bound-and-true-p nl2-mode))))
There are several facts that make highlighting two consecutive empty lines more complicated (font-lock tends to only highlight non-empty regions, linebreaks are limits for the region to re-fontify, re-fontification after buffer changes are required).
The following code shows one way. Maybe, there are easier ways.
(require 'font-lock)
(global-font-lock-mode)
(defface jit-lock-nl2-face '((default :background "yellow"))
"Face to indicate two or more successive newlines."
:group 'jit-lock)
(defun jit-nl2-extend (start end &optional old)
"Extend region to be re-fontified"
(save-excursion
(save-match-data
;; trailing:
(goto-char end)
(skip-chars-forward "[[:blank:]]\n")
(setq jit-lock-end (point))
;; leading:
(goto-char start)
(beginning-of-line)
(skip-chars-backward "[[:blank:]]\n")
(unless (bolp) (forward-line))
(setq jit-lock-start (point)))))
(defun jit-nl2 (jit-lock-start jit-lock-end)
"Highlight two or more successive newlines."
(save-excursion
(save-match-data
(jit-nl2-extend jit-lock-start jit-lock-end)
;; cleanup
(remove-text-properties jit-lock-start jit-lock-end '(font-lock-face jit-lock-nl2-face))
;; highlight
(while (< (point) jit-lock-end)
(if (looking-at "[[:blank:]]*\n\\([[:blank:]]*\n\\)+")
(progn (put-text-property (match-beginning 0) (match-end 0) 'font-lock-face 'jit-lock-nl2-face)
(goto-char (match-end 0)))
(forward-line))))))
(add-hook 'after-change-major-mode-hook (lambda ()
(add-hook 'jit-lock-after-change-extend-region-functions 'jit-nl2-extend)
(jit-lock-register 'jit-nl2)
(jit-lock-mode 1)
))

Emacs Lisp function to toggle variable 'tab-width' between 4 & 8

The source tree that I work on has files indented with different tab values, (not to mention spaces) and the best I can do is to set emacs to use the style found in the region of code I am modifying. Instead of doing M-x set-variable tab-width to 4 or 8, a key binding to toggle the tab-width among these two value would help immensely.
Thanks.
;; Obviously substitute your preferred key for <f8>
(global-set-key (kbd "<f8>") 'tf-toggle-tab-width-setting) ; ' "fix" highlighting
(defun tf-toggle-tab-width-setting ()
"Toggle setting tab widths between 4 and 8"
(interactive)
(setq tab-width (if (= tab-width 8) 4 8))
(redraw-display))
Edited to add redraw-display as per comment suggested
Not quite answering the question (the answers given are good enough), but you might want to consider setting a per-file local variable. For example, assuming that "//" means comment in your language, you would put the following in the first line of the file:
// -*- tab-width: 4 -*-
And emacs will set the variable for you whenever you visit the file. See http://www.gnu.org/software/emacs/manual/html_node/emacs/Specifying-File-Variables.html for more information on file-based variables.
Of course this might not be a choice if the file is shared among a group - unless you can convince your colleges that this first line comment is completely harmless and extremely useful!
Thanks for all the answers. I added a redraw-display call so that the change is reflected immediately. (Tried posting this as a comment, but can't.)
(global-set-key (kbd "<f8>") 'tf-toggle-tab-width-setting)
(defun tf-toggle-tab-width-setting () "toggle setting tab widths between 4 and 8"
(interactive)
(setq tab-width (if (= tab-width 8) 4 8))
(message "set tab-width to %d." tab-width)
(redraw-display)
)
And along the same lines. :(
(global-set-key (kbd "<f7>") 'tf-toggle-indent-mode-setting)
(defun tf-toggle-indent-mode-setting ()
"toggle indenting modes"
(interactive)
(setq indent-tabs-mode (if (eq indent-tabs-mode t) nil t))
(message "Indenting using %s." (if (eq indent-tabs-mode t) "tabs" "spaces"))
)
Throw this in your .emacs or .emacs.d/init.el file:
(defun toggle-spaces ()
"Toggle tab-width between 4 and 8"
(interactive)
(if (eq tab-width 4)
(setq tab-width 8)
(setq tab-width 4)))
;; This will set Ctrl-g to toggle but you can set it to anything
;; you want.
(global-set-key "\C-g" 'toggle-spaces)

How do I make Emacs show blank spaces?

How can I make Emacs show blank spaces (like a space, a tab, a line-jump, etc). Many other editors like Kate and Eclipse have this feature and I find it very useful to see when code is indent-broken because of mix of spaces and tabs (particularly Python).
WhiteSpace mode is an Emacs minor mode for visualizing all white space characters in the current buffer. It can be activated with M-x whitespace-mode.
Here is a screenshot of WhiteSpace in action taken directly from the Emacs wiki,
Note: WhiteSpaceMode has now replaced BlankMode
All the possible settings to do that seem to be summarized here (blank-mode) and here and here (ShowWhiteSpace)
also:
(if (>= emacs-major-version 22)
(progn
;; Mode to use with Emacs 22
;; http://emacswiki.org/cgi-bin/wiki/BlankMode
(require 'blank-mode)
;; Mode not active by default: let's activate it
(global-blank-mode t)
;; ... activate it when text mode where color syntax is not active by default
(add-hook 'text-mode-hook 'blank-mode-on)
;; All invisible chars are shown, except newline char.
(setq blank-chars '(tabs spaces trailing lines space-before-tab))
;; Show only for one color, no mark inserted
(setq blank-style '(color))
;; Use for normal space (not shown)
(set-face-background 'blank-space-face nil)
(set-face-foreground 'blank-space-face "black")
;; used for non breakable space
(set-face-background 'blank-hspace-face "PaleGreen")
(set-face-foreground 'blank-hspace-face "black")
;; Used for spaces left of a tab
(set-face-background 'blank-space-before-tab-face "orange")
(set-face-foreground 'blank-space-before-tab-face "black")
;; Used for tab
(set-face-background 'blank-tab-face "lemonchiffon")
(set-face-foreground 'blank-tab-face "black")
;; used for extra space at the end of a line
(set-face-background 'blank-trailing-face "gold")
(set-face-foreground 'blank-trailing-face "black")
;; Used for line too long
(set-face-background 'blank-line-face "snow2")
(set-face-foreground 'blank-line-face "black")
)
(progn
;; For older Emacs prior to version 22.
;; http://www.emacswiki.org/cgi-bin/wiki/show-wspace.el
(require 'show-wspace)
(add-hook 'font-lock-mode-hook 'show-ws-highlight-tabs)
(add-hook 'font-lock-mode-hook 'show-ws-highlight-hard-spaces)
(add-hook 'font-lock-mode-hook 'show-ws-highlight-trailing-whitespace)
)
)
indent-broken? - never use tabs in your code - disk space is cheap these days.
Put (setq-default indent-tabs-mode nil) in your .emacs file. Get used to typing C-x h M-x untabify to untabify the entire buffer. To search for tabs type C-s C-i. If you have obscure control characters in your buffers you can see them with M-x hexl-mode.
Also C-x h M-x indent-region will indent the entire buffer. Some modes like vhdl-mode have a beautify region command.