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
  • 4,049
17 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
  • 12.4k
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
  • 51.1k
12 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
  • 18k
11 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
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
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
  • 5,245
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
  • 33.2k
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,465
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.6k
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
  • 33.2k
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
  • 51.1k
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
  • 51.1k
7 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
  • 121
7 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
  • 77.5k
7 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,118
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
  • 3,066
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,350
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,521
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,453
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
  • 77.5k
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
  • 77.5k
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
5 votes

Is [:space:] same as \s-?

I recommend reading the Emacs Lisp manual instead of Emacswiki. Here's some of it on backslash sequences in regular expressions: ‘\sCODE’ matches any character whose syntax is CODE. Here CODE ...
wasamasa's user avatar
  • 22.2k
5 votes
Accepted

setq require-final-newline not working when setq-default does

You need to either set the default value of the variable, if you want it for every file: (setq-default require-final-newline t) ;; or ... (customize-set-variable 'require-final-newline t) or to set ...
NickD's user avatar
  • 29.8k
5 votes

Delete trailing whitespace on save, but only for yaml files

(add-hook 'yaml-mode-hook (lambda () (add-hook 'before-save-hook #'delete-trailing-whitespace nil "local")))
shynur's user avatar
  • 5,288
4 votes
Accepted

Whitespace-mode tab mark disturb my indentation

This is a known problem as made evident in a comment in whitespace.el: (defcustom whitespace-display-mappings '( (space-mark ?\ [?\u00B7] [?.]) ; space - centered dot (...
wasamasa's user avatar
  • 22.2k
4 votes
Accepted

How can I switch whitespace-mode enabled/disabled by key?

There is a set of conventions for minor modes documented in the docstring of define-minor-mode: The defined mode command takes one optional (prefix) argument. Interactively with no prefix ...
wasamasa's user avatar
  • 22.2k
4 votes

tab and spaces shown aligned in emacs C++ mode, but not in other editors

The normal convention is that TAB characters align to the next multiple-of-8 column. Notice: I wrote 8, not 4. So if your other text editor aligns to a multiple-of-4 column (like you seem to imply), ...
Stefan's user avatar
  • 26.4k

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