This is a frequent question so I figured I’d mention the solution here:
You want to remove all empty (blank) lines from a buffer. How do you do it? Well, it’s super easy.
Mark what you want to change (or use
C-x h to mark the whole buffer) and run this:
M-x flush-lines RET ^$ RET
And you’re done. So what does that mean? Well,
M-x flush-lines will flush (remove) lines that match a regular expression, and
^$ contain the meta-characters
^ for beginning of string and
$ for end of string. Ergo, if the two meta-characters are next to eachother, it must be a blank line.
We can also generalize it further and remove lines that may have whitespace (only!) characters:
M-x flush-lines RET ^\s-*$ RET
In this case
\s- is the syntax class (type
C-h s to see your buffer’s syntax table) for whitespace characters. The
* meta-character, in case you are not a regexp person, means zero or more of the preceding character.
Update — Pete Wilson asks: “How do you collapse multiple lines into one blank line?”.
That’s a bit harder, mostly because
flush-lines only works well on whole, single lines. For multi-line processing you have two choices: you can abuse regexp, or you can use a macro. It’s fairly easy to do it with regexp in this case, but for more complex data-scrubbing I would use a macro; nevertheless, I will do it both ways*.
*I’m pretty sure my macro/regexp examples are general enough to work in all cases; but let me know if they aren’t
For the regexp approach I will use
query-replace-regexp) and because I have to use a literal newline character I will use Emacs’s
quoted-insert command, bound to
C-q. So to insert a newline, you would type
^J represents the literal newline or line feed character (see ASCII Control Characters on Wikipedia for more information).
So the text we want to search for looks like this:
So how does it work? Well, we tell Emacs to search for any two or more newlines that are at the beginning of a string — where each line is considered a string by Emacs — and because we search for two or more we skip the ones that only have a single newline. So if there are 10 newlines in a row, we replace them all in one fell swoop with a single newline. You can omit the replacement newline to remove them altogether!
The other way is very similar and uses a keyboard macro,
delete-blank-lines bound to
C-x C-o. This approach is more complicated than it really ought to be, because
delete-blank-lines will annoyingly (in this case — it’s a useful feature otherwise!) convert multiple blank lines into a single blank line (good), and remove single blank lines altogether (bad.)
To make the macro, go to the end of the buffer
F3 to begin recording, and then type
C-M-r and in the isearch prompt enter:
Regexp ISearch backward:
Press return to go to the first match and press
C-x C-o. Now press
F4 to stop recording and you’re done with the macro. Press
C-u C-x e to fix all remaining instances, and that’s it — you’re done.
Exercise to the reader: Why did I search in reverse with
C-M-r instead of using
Unbeknownst to many, Emacs comes with a full suite of wrappers around the common GNU network utilities.
Most of the utilities are just simple wrappers around their command-line equivalents, but in full technicolor; but some — like the
nslookup support — also adds full Emacs
Another useful feature is the built-in
ffap support (it means find file at point) and it will try to determine if the point is — if used interactively with the net utils below — on a hostname or IP and default to that.
The net utils library were written with the GNU libraries in mind, so Windows users may find the support a bit lacking. But you can always download the Win32 ports.
Here’s a list of utilities Emacs supports; invoke with
M-x. You may have to configure them to your liking, and you can do that by invoking
M-x customize-group RET net-utils RET.
||Look up the DNS information for an IP or host using
||Very simple wrapper around the commandline tool
You can tell Emacs to set a buffer as not modified (even though it may well be) by pressing
M-~, also bound to
M-x not-modified. This will obviously suppress any save prompts for that file — at least until you do something that makes it become modified again — so do be careful.
… and I thought I would never jump on that bandwagon, but here I am. I get a significant amount of my traffic from people twittering — so why not?
Follow me here:
Seems there’s a new kid in town.
Fabián Ezequiel Gallina has announced a new Emacs mode to, I hope, merge with the existing GNU Emacs mode (referred to as
python.el) and possibly merge with the other Emacs mode out there (referred to as
python-mode.el) and bring balance to the force.
This is great news. I have been meaning to write an article covering All Things Python in Emacs and if I can do that and only cover one mode that’ll make my life a whole lot easier. Currently the Emacs-Python community is way too fragmented due to license issues with
python.el not keeping up with the times.
Altering the key bindings in Emacs should not, on the face of it, be a difficult task. But there’s a reason why the Emacs manual has dedicated 30-odd pages to describing, in great detail, all the subtleties and nuances of how to bind keys.
To save you the time of reading all of that, I’ve written a guide that covers what you need to know to bind keys to even complex commands, and a set of templates you can use in your own code.
I bet the majority of files you edit on a day-to-day basis are the same ones over, and over again. For that reason I recommend you use Emacs’s
recentf package, it is a great — and very sophisticated, like all things Emacs — utility that keeps track of recently used files.
recentf by adding Ido mode support (if you don’t know what Ido is, read Introduction to Ido Mode); and by overriding
C-x C-r, bound to
find-file-read-only, a useless feature I never use. Note: the Ido supercharging only works if you have ido-mode enabled in the first place!
Note that unlike
find-file I’ve opted to display the entire filepath in Ido’s completion engine as I often find that a directory or remote host is the only disambiguator if there are multiple files with the same name. I’ve thought about filtering the list of recent files through the
uniquify module for buffers but that’s for another time.
Here’s what you need to add to your .emacs:
;; get rid of `find-file-read-only' and replace it with something
;; more useful.
(global-set-key (kbd "C-x C-r") 'ido-recentf-open)
;; enable recent files mode.
; 50 files ought to be enough.
(setq recentf-max-saved-items 50)
(defun ido-recentf-open ()
"Use `ido-completing-read' to \\[find-file] a recent file"
(if (find-file (ido-completing-read "Find recent file: " recentf-list))
(message "Opening file...")
You can force Emacs to make a file executable (respecting your
umask settings) if Emacs considers it a script. To determine if it is a script, Emacs will look for the hash-bang notation in the file and treat it as a script if it finds it.
Add this to your .emacs and Emacs will then make the file executable if it is a script.