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 ...
Ryan's user avatar
  • 3,969
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:]]*$")))
François Févotte's user avatar
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 ...
Basil's user avatar
  • 11.9k
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 ...
phils's user avatar
  • 47.3k
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)))
PythonNut's user avatar
  • 10.2k
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 ...
lawlist's user avatar
  • 18.6k
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-...
Julian Kniephoff's user avatar
10 votes

How remove whitespaces in the beginning of lines?

Another way is just to mark the region and call delete-whitespace-rectangle!
D. Dimakakos's user avatar
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 ...
Tobias's user avatar
  • 32.1k
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 ...
JeanPierre's user avatar
  • 7,273
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 ...
glucas's user avatar
  • 20.1k
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 ...
db48x's user avatar
  • 14.9k
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....
Tobias's user avatar
  • 32.1k
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 <...
Iqbal Ansari's user avatar
  • 7,438
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 ...
phils's user avatar
  • 47.3k
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 (...
phils's user avatar
  • 47.3k
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.
rpluim's user avatar
  • 4,477
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 ...
Heikki's user avatar
  • 2,931
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 ...
GDP2's user avatar
  • 1,320
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's user avatar
  • 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 ...
simenheg's user avatar
  • 111
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 ...
Drew's user avatar
  • 74.6k
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 ...
Nsukami _'s user avatar
  • 6,281
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-...
manandearth's user avatar
  • 2,058
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 ...
Aquaactress's user avatar
  • 1,383
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's user avatar
  • 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 ...
Drew's user avatar
  • 74.6k
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 ...
purple_arrows's user avatar
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 ...
Drew's user avatar
  • 74.6k
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 ...
Croad Langshan's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible