emacs remove any indentation

Here’s another quick one. It could be when formatting pieces of code that you want to remove any space from the beginning of the line. Removing de-facto any indentation.

M-x query-replace-regexp RET
^[[:space:]]+ RET

Confirm then one by one with ‘y‘ or replace all of the buffer with ‘!‘.

“Reverting” the auto-fill in emacs

Most of the time I use emacs with auto-fill mode breaking any line at 70. Sometimes though I’d like a paragraph to be reverted to a single line; for example for copy-pasting into other editors where it could breaks the formatting. Here’s how

M-x set-variable RET fill-column RET 100000
M-q (on the desired paragraphs)
M-x set-variable RET fill-column RET 70

First line set the fill-column to a large amount, the second will re-run the fill-paragraph and the third will reset the fill-column to my usual value

emacs strip blank lines

Spiral-Bound Pad

Spiral-Bound Pad (by incurable_hippie)

Here is a quick way to strip (remove) blank lines from a file with emacs.

  1. position on top of file: M-<
  2. Call a query-regex-replace: M-C-%
  3. input the regex ^ C-q C-j that stand for match all lines that consist in only a carriage return.
  4. RET-RET. The first one confirm the regex and the last one is for “replace with nothing”.
  5. When asked use ! to replace-all.

Summing all commands:

M-< C-M-% ^ C-q C-j RET RET !

here you are 🙂

[emacs] log4j-start-filter

Sometimes ago I cited a cool mode for emacs: log4j-mode. Since I started useing it just for “syntax hilight”, I’ve discovered a very cool macro: log4j-start-filter.

I had the needs to filter an application log file (30Mb growing) in which many applications is logging. I’ve not decided this, so don’t hang up with me :).

Well just typing M-x log4j-start-filter <my-list-of-keywords-space-separated> RET and automagically the file is showing only what I need. I can’t live without this functionality.

The counterpart of this macro is M-x log4j-stop-filter.

[emacs] save a macro

Once you have recorded a macro, you can save it for later usage on a file (or within your .emacs). The steps to follow in order to get this work, is (1) give a name to the macro, (2) insert it into a buffer.

M-x name-last-kbd-macro RET my-cool-macro-name

then (what I usually do), open a buffer for saving it and insert the macro into it

C-x C-f ~/my-buffer.el ;;open a new (or existing one) file.
M-x insert-kbd-macro RET my-cool-macro-name ;;insert the macro into the buffer
C-x C-s ;;save the buffer

Now, it’s time to use the saved macro. If you have saved it in your .emacs you’ll have it ready on every emacs startup, otherwise it’s enough to load the .el file when needed (I prefer this way)

M-x load-file RET ~/my-buffer.el

[emacs] toggle a buffer in read only

I’m a bit abstracted and usually works with a lot of opened files. So it happens sometimes that in the rush of job i ended editing a file instead of another.

I’m also usual to open existing files I wrote in order to get some hints. And I could edit them for error. To avoid this I usually use my preferred editor functionality to set a buffer as read only, so it’s not possible to write inside it. How?

M-x toggle-read-only

Easy! 🙂

[emacs] sorting lines

I will never stop saying how much I love emacs. I have to admit that it’s very difficult to learn how to use it and even after you have learned enough to use it, you will always find something that you didn’t know.

The task for today is simple: sorting the lines in a region. There’s an already existent macro: sort-lines. So first you will have to select the region and the call the macro. In the following example we will sort the lines of the whole document.

C-x h <RET> M-x sort-lines

Again: easy easy and easy 🙂

Emacs and startup position

I’ve updated my post on .emacs adding the functionality to position the frame on startup.

This in order to avoid an ugly windows bug. If you like me, prefer to have the taskbar on top of the screen instead of the default (bottom), you’ll find that sometimes emacs starts with the title bar under it. So you won’t be able to move the window.

I’ve added the code in the previous post, however here is an extract

(setq initial-frame-alist '((left . 50) (top . 50)))