48

Use command indent-rigidly, which is bound to C-x TAB by default. You can specify the number of spaces to indent by using a prefix argument, such as C-5 C-x TAB. Or you can invoke the command and then use the left/right arrows to interactively adjust the indent level.


25

Use M-s SPC during Isearch to toggle matching whitespace literally. When matching literally, each SPC char you type is matched individually. (This used to be the default Emacs behavior, BTW.) To configure this as the default behavior customize option search-whitespace-regexp to nil. (M-x customize-option search-whitespace-regexp.) See the GNU Emacs manual,...


25

Would something like this be "easier"? (defun current-line-empty-p () (save-excursion (beginning-of-line) (looking-at-p "[[:space:]]*$")))


19

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 buffers by adding the following to your init.el: (add-hook 'before-save-hook 'delete-trailing-whitespace) For future reference, you can search for Emacs ...


12

If you are used python-mode.el C-c > or C-c < to shift blocks left or right


12

A simple method, close to what you have: (defun current-line-empty-p () (string-match-p "\\`\\s-*$" (thing-at-point 'line)))


9

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 changes on an existing org file, use C-u C-c C-q The doc-string for org-tags-column, which can be seen by typing M-x describe-variable RET org-tags-column RET ...


9

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 since Emacs 24.4: (eval-when-compile (require 'subr-x)) (string-trim "\n\r\s\tfoo\n\r\s\t") ; => "foo" Since Emacs 26.1 these inline functions also accept ...


8

You can use ClangFormat to achieve this. After installing the clang-format tool, you can use clang-format.el to perform the appropriate actions from emacs. clang-format.el is also available from MELPA. The emacs commands provided are clang-format-buffer and clang-format-region which you can bind as you need. Note that you can customize formatting options by ...


8

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 let you visualise trailing whitespace nevertheless. I also have a keybinding for this: (defun toggle-show-trailing-whitespace () "Toggle `show-trailing-...


7

The string-rectangle command ( C-x r t) can be used to insert any arbitrary text (spaces included) in a selected region. Let's say you have this block of text and you want to insert 5 spaces in front of all lines. abc def ghi First select a "0 column" region as shown below (the point is on the character 'a' and the mark is in the same column in the ...


7

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 <backspace>") 'hungry-delete-backward) (global-set-key (kbd "C-c <deletechar>") 'hungry-delete-forward) update: as of 12.2018 function definitions ...


7

First off, since you didn't mention it: Using the same version of Emacs (24.4.1), the behavior you describe can be reproduced in emacs -Q. Secondly, what you are doing should work (AFAICT). It seems that turning on whitespace-mode globally (or even in a mode hook) before visiting a file with a custom whitespace-line-column is what's causing the problem. You ...


7

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 () "Custom `js-mode' behaviours." (setq indent-tabs-mode nil)) (add-hook 'js-mode-hook 'my-js-mode-hook)


7

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 characters with a special face or displaying them as special glyphs. The relevant docstrings contain more information. So let's say we want do display spaces as ·...


7

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-whitespace-mode-for-magit) This basically advises the function whitespace-enable-predicate, which whitespace-mode uses to determine, which buffers should receive global-...


7

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 dependencies: (defun s-trim-left (s) "Remove whitespace at the beginning of S." (declare (pure t) (side-effect-free t)) (save-match-data (if (string-...


6

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 want it to work in all buffers then this is one solution: (defun my-show-trailing-ws () "Show trailing whitespace in the current buffer, unless it is read-only....


6

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 partly so that keybindings can remain short and easy to remember. In essence, the answer to your question is that you enter the space character by switching to ...


5

There is C-x TAB (bound to indent-rigidly). Give it a prefix argument to indicate how many spaces you wish to indent by, negative removes that many spaces.


5

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 would actually want what you're asking for (i.e. that trailing whitespace is introduced automatically, and then left there). It's rather more common for people ...


5

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 only the spaces after indenting tabs, and only non-indenting tabs. (defface my-tab '((t (:background "LemonChiffon"))) "..." :group 'faces) (defface my-space '...


5

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 tab-width. The default values of tab-width and css-indent-offset are 8 and 4 respectively, so you'll need two levels of indentation before a tab is inserted. ...


5

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 by typing M-x whitespace-toggle-options. You're setting whitespace-style with this code: (setq whitespace-style (quote (face spaces tabs newline space-mark ...


5

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 available.) You might also try: M-x customize-group whitespace, to see all the related options -- whether to turn on whitespace-mode globally, how to display ...


5

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.e., press RET again. You are prompted by query-replace-regexp in the minibuffer. Press ! to perform all replacements at once. Explanation of the regular ...


5

Another way is just to mark the region and call delete-whitespace-rectangle!


5

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 is a character that represents a syntax code: thus, ‘w’ for word constituent, ‘-’ for whitespace, ‘(’ for open parenthesis, etc. To represent ...


4

You should generally start Emacs with emacs -q to make sure it's not your configuration. If that doesn't get rid of it, but emacs -Q does, chances are it's the way your Emacs was packaged or a site file that's used for every user on your system. Additionally to that you can inspect the faces involved by placing point at the highlighted area and doing M-x ...


4

The Emacs Manual (section Special Isearch) says this: By default, incremental search performs lax space matching: each space, or sequence of spaces, matches any sequence of one or more spaces in the text. Hence, "foo bar" matches "foo bar", "foo bar", "foo bar", and so on (but not "foobar"). More ...


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