I’m fond of seeing somewhere the full path to the file I’m currently
editing(as opposed to just the file name displayed in
modeline). Emacs’s frame title seems like a good place to display such
information, since by default it doesn’t show anything
interesting. Here’s how we can achieve this:
The path will be displayed in an abbreviated manner(/home/bozhidar/
will be shortened to just ~/). If you’d like to see the expanded path
just remove the abbreviate-file-name function invocation. If a
buffer is not visiting a file, the buffer’s name would be displayed
frame-title-format like this by default.
Emacs does not keep track of recently visited files by default. Sad,
but true. On a more positive note - it has the feature(courtesy of
recentf), but it’s simply not enabled out-of-the-box. Let’s see what
we can do to change that:
That wasn’t that hard. Now Emacs will keep track of the last 200
files we visited and will show the last 15 of them in the File->Open
recent(this menu entry is added by recentf) menu. That’s it.
At this point you’re probably feeling let down. After all - who uses
the menu bar in Emacs anyways? Fortunately there is also a command
named recentf-open-files. Upon invoking it with M-x
recentf-open-files you’ll be presented with a list of all recently
If you’re an ido user you might prefer to use some command based on
it instead. Here’s one:
(defunrecentf-ido-find-file()"Find a recent file using ido."(interactive)(let((file(ido-completing-read"Choose recent file: "recentf-listnilt)))(whenfile(find-filefile))))
I’d suggest binding whichever command you prefer to either C-c f or
C-x C-r (bound by default to the infrequently used
You probably know that major modes in Emacs can derive(inherit)
functionality from a parent major mode.
Emacs 24.1 introduced prog-mode - a new major mode from which all
programming modes should be derived. All of the programming modes that
come bundled with Emacs already inherit it(as do most
actively maintained third-party modes). Having a common parent for all
the programming modes allows us to do some pretty neat stuff - like
setting common behavior for all of them. Here’s how we can enable
spell-checking in comments:
That’s all for now. We’ll discuss flyspell in more detail down the road.
Emacs is packed with an insane amount of cool stuff. With Easter fast
approaching (I’m an Orthodox Christian and ours is in the beginning of
May this year) I cannot help but think of easter eggs(“secret”
commands, inside jokes, etc). Let’s see if we can find some in Emacs!
From time to time(most often when I refactor code) I need to
quickly(this removes dired from the equation) delete a file and kill
the buffer associated with it. Since most of the files I work with
are under version control I can just use the tried and true M-x
vc-delete-file. Unfortunately the command does not act on the current
file and will prompt you for a file to delete. Looks like we need to
create a simple wrapper around it to get the job done:
(defundelete-file-and-buffer()"Kill the current buffer and deletes the file it is visiting."(interactive)(let((filename(buffer-file-name)))(whenfilename(if(vc-backendfilename)(vc-delete-filefilename)(progn(delete-filefilename)(message"Deleted file %s"filename)(kill-buffer))))))
The wrapper is extra smart and will work on files that are not under
version control as well! I’m pretty fond of commands that do what you
mean instead of throwing errors. Now that we have this neat little
command we should probably bind it to some each to press keys, like C-c D:
While programming(at least in some languages) I often find myself
wishing to drag the current line a couple lines up or down. Emacs does
have the means to transpose (switch) lines (courtesy of the
transpose-lines command bound to C-x C-t), but I find it
unwieldy. That’s why I’ve built a couple of custom commands on top of
(defunmove-line-up()"Move up the current line."(interactive)(transpose-lines1)(forward-line-2)(indent-according-to-mode))(defunmove-line-down()"Move down the current line."(interactive)(forward-line1)(transpose-lines1)(forward-line-1)(indent-according-to-mode))
Those are not the kind of commands you’ll want to invoke with
M-x. Therefore I suggest binding them to something like C-S-up and
C-S-down(these bindings are often used for the same purpose in other
editors and IDEs):
Just about every modern editor and IDE these days highlights the line
you’re currently on, which I find extremely helpful. Emacs doesn’t do
this out-of-the-box, but provides the means to do so - the built-in
global minor mode global-hl-line-mode. To take it out for a spin
just add the following line to your Emacs config:
In case you don’t like the highlighting color you can change it by
adjusting the value of the hl-line-face face. Most decent color themes
pick a pretty good value for it, so normally you should not bother
changing it manually.
global-hl-line-mode is enabled out-of-the-box in
If you’re into programming highlighting the matching parentheses in
your code is probably something you find rather desirable. Emacs
doesn’t do so by default, but the global minor mode show-paren-mode,
that comes bundled with Emacs, does.
Valid styles are parenthesis (meaning highlight the matching
parentheses), expression (meaning show the entire expression
enclosed by the parentheses) and mixed (meaning show the matching
parentheses if both are visible, and the expression otherwise).
Personally I find parenthesis as the least distracting option and
therefore I favor it.
If you’d like to change the faces used for matching and mismatched
parentheses you’d have to alter show-paren-mismatch and
show-paren-match. Most decent color themes do this out the box.
show-paren-mode is enabled out-of-the-box in
Adjusting interactively the font size is Emacs is pretty easy. Three
keybindings refer to the mighty text-scale-adjust command - C-x
C-+(increase font size), C-x C--(decrease font size) and C-x
C-0(reset font size to default). After using any of the keybindings
you can proceed to adjust the font size further just by pressing +,
- or 0 respectively. text-scale-adjust is mostly a wrapper
around text-scale-increase and text-scale-decrease.
You might want to bind them C-+ and C--(generally a bad idea since
you won’t be able to type stuff like C-- some-command anymore) to
get more common keybindings for the operations.
Every Emacs user knows what C-x o(other-window) does - it moves
your cursor to the next window. When you’re out of windows - the
command will take you back to the first one.
Relatively few people are aware that the command takes a prefix
argument which allows you to move several windows forward or
backward. That’s obviously pretty useful when you’re a heavy window user
and like to split your frames into many windows.
To move 3 windows forward you’d do C-u 3 C-x o. To move 2 windows
backwards you’d do C-u -2 C-x o.
If you’re mostly working with only two windows you probably don’t need
to know that much about other-window - after all invoking the
command one time will take you to the other window and invoking it a
second time will take you back to the window you were originally
in. I, however, often employ 3 or windows (blessed be the people who
made huge LCD displays so cheap) and I really dislike having to type
C-u -1 C-x o (or C-- C-x o, M--1 C-x o or M-- C-x o for that
matter) to go back to the previous window. To alleviate that
particular nuisance I utilize the following little trick: