29
votes
Accepted
Is there a function for removing trailing spaces in a buffer?
According to the EmacsWiki, M-x delete-trailing-whitespace will eliminate all trailing whitespace in a buffer. In Spacemacs, it is bound to SPC x d w by default
You can do it automatically for all ...
26
votes
Accepted
Easiest way to check if current line is "empty" (ignoring whitespace)?
Would something like this be "easier"?
(defun current-line-empty-p ()
(save-excursion
(beginning-of-line)
(looking-at-p "[[:blank:]]*$")))
16
votes
What's the idiomatic (or best) way to trim surrounding whitespace from a string?
What's the idiomatic (or best) way to trim surrounding whitespace from a string?
The built-in library subr-x.el has included the inline functions string-trim-left, string-trim-right, and string-trim ...
12
votes
How can I visualize trailing whitespace like this?
Ignoring whitespace-mode, there's always:
(setq-default show-trailing-whitespace t)
and
M-x customize-face RET trailing-whitespace RET
which might not give you that exact visualisation, but will ...
12
votes
Easiest way to check if current line is "empty" (ignoring whitespace)?
A simple method, close to what you have:
(defun current-line-empty-p ()
(string-match-p "\\`\\s-*$" (thing-at-point 'line)))
11
votes
Accepted
Too much whitespace on setting org-mode tags
The variable org-tags-column controls the distance of tags at the end of the heading. To have just one space, the original poster may wish to consider using:
(setq org-tags-column 0)
To apply the ...
10
votes
Accepted
magit-status does not open, when using (global-whitespace-mode 1)
I just added the following to my init.el:
(defun prevent-whitespace-mode-for-magit ()
(not (derived-mode-p 'magit-mode)))
(add-function :before-while whitespace-enable-predicate 'prevent-...
10
votes
How remove whitespaces in the beginning of lines?
Another way is just to mark the region and call delete-whitespace-rectangle!
9
votes
What's the idiomatic (or best) way to trim surrounding whitespace from a string?
There is the string manipulation library s.el where trimming whitespace and newlines at the beginning and the end of a string is implemented as function s-trim. I cite that function here with its ...
8
votes
How can I visualize trailing whitespace like this?
The documentation in the emacs manual says:
Whitespace mode is a buffer-local minor mode that lets you
“visualize” many kinds of whitespace in the buffer, by either drawing
the whitespace ...
8
votes
Accepted
Emacs equivalent to :set list in vim
Try M-x whitespace-mode to toggle display of space, line breaks, etc. You can set whitespace-style to control what gets displayed. (Try M-x customize-variable whitespace-style to see what options are ...
8
votes
Accepted
emacs command to remove spaces until next parens
There is the command just-one-space which collapses all whitespace around the point down to a single space character. It works in any mode, not just paredit. By default it's bound to M-SPC, which can ...
8
votes
How remove whitespaces in the beginning of lines?
IMHO the standard way is:
Go to the top of your buffer.
Type C-M-% for query-replace-regexp.
Input ^\s-+ as regular expression and RET. (See explanation below.)
Leave the replacement string empty, i....
7
votes
Accepted
How to delete all whitespace up to the first non-whitespace character?
You might find the hungry-delete package useful. I personally bind C-cdelete to delete whitespace after point, and C-cbackspace to delete whitespace before point like so
(global-set-key (kbd "C-c <...
7
votes
Disable removal of trailing whitespace in Emacs
This is a feature of electric-indent-mode (which is on by default in recent versions of Emacs). The behaviour is hard-coded in electric-indent-post-self-insert-function.
I think that very few people ...
7
votes
Accepted
How do I make javascript-mode not turn all 8 spaces into tabs?
The buffer-local indent-tabs-mode variable controls this (in general, for any mode).
Note that javascript-mode is an alias for js-mode, so there isn't a javascript-mode-hook.
(defun my-js-mode-hook (...
7
votes
Accepted
How can I ask emacs to show me (visually) invisible characters like CR LF?
Check out M-xwhitespace-mode and M-xcustomize-group RETwhitespace
By default it is set up to visually emphasise spaces, tabs, and line endings, but you can tune it to eg leave spaces alone.
7
votes
Confusion about space and tab characters in Emacs
Like most things in emacs, the tabs policy is configurable. It sounds to me that you'd like to follow the most common apporach and use the tab key as an indentation tool and not have tab characters in ...
6
votes
Accepted
Disable removal of trailing whitespace in Emacs
After a good bit of digging and learning some features of Emacs Lisp, I managed to make a hack which produces the effect that I desire. I imagine that I'm not the only one who wants to leave trailing ...
6
votes
Accepted
How to skip forward to first whitespace character?
You can use skip-syntax-forward to skip non-whitespace. Docstring:
(skip-syntax-forward SYNTAX &optional LIM)
Move point forward across chars in specified syntax classes.
SYNTAX is a ...

Dan♦
- 32.3k
6
votes
Accepted
css-mode: use tabs and not spaces
The built-in CSS mode should support this just fine by giving indent-tabs-mode a non-nil value.
Note that spaces will still be used for padding if the number of indentation spaces isn't a multiple of ...
6
votes
Accepted
Is there a hook that runs for all read only buffers
If you want this to act in any buffer, not just a file-visiting buffer then find-file-hook is not appropriate. (You said "all buffers", but you also spoke of editable/non-editable "files".)
If you ...
6
votes
Accepted
How to remove empty lines of a selected region of code in Emacs?
Select what you want to change, or C-x h to select the whole buffer.
Then: M-x flush-lines RET followed by ^$ RET or ^[[:space:]]*$ RET
^[[:space:]]*$ contain the meta-characters:
^ for beginning of ...
6
votes
emacs command to remove spaces until next parens
For deleting white spaces there are a few commands in Emacs.
You can find it all in EmacsWiki DeletingWhiteSpaces
Here are two examples from the wiki:
delete-horizontal-space
M-\ or M-x delete-...
6
votes
Accepted
How do you type a space character in Spacemacs?
Spacemacs uses evil-mode which is a VI emulation layer. Evil-mode adds VI modal editing to spacemacs. In a nutshell modal editing has different modes where keybindings do different things. This is ...
5
votes
Easiest way to check if current line is "empty" (ignoring whitespace)?
(defun blank-line-p (&optional pos)
"Returns `t' if line (optionally, line at POS) is empty or
composed only of whitespace."
(save-excursion
(goto-char (or pos (point)))
(beginning-of-...

Dan♦
- 32.3k
5
votes
Accepted
Whitespace highlighting
This should do what you want. Define regexps that match what you want, and faces.
Then match the subgroups in the function you add to font-lock-keywords. The subgroups are used to say that you want ...
5
votes
Accepted
Turn off whitespace-mode highlighting
Quoting from the manual:
The kinds of whitespace visualized are determined by the list variable
whitespace-style. Individual elements in that list can be toggled on
or off in the current buffer ...
5
votes
Accepted
Delete trailing whitespace when saving, except certain modes
(add-hook 'before-save-hook
(when '(not fundamental-mode))
'delete-trailing-whitespace)
You are not passing a function as the second arg to add-hook. You are passing the ...
5
votes
Confusion about space and tab characters in Emacs
To make tabs and spaces visible, you can enable whitespace-mode with M-x whitespace-mode RET or M-x global-whitespace-mode RET. Have a look at variable whitespace-style (e.g. M-x customize RET ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
whitespace × 137indentation × 19
tabs × 13
whitespace-mode × 13
org-mode × 7
regular-expressions × 7
highlighting × 7
newlines × 7
search × 6
isearch × 6
spacemacs × 4
latex × 4
hooks × 4
major-mode × 3
text-editing × 3
region × 3
comment × 3
display × 3
formatting × 3
query-replace × 3
prelude × 3
linum-mode × 3
deletion × 3
key-bindings × 2
org-export × 2