04 Aug 2017
Lately, due to working with a large code base, I've grown more and
more fond of
counsel-rg. It's an Elisp wrapper
around ripgrep - a relatively
new recursive grep tool that aims to be faster than the competition
Besides being really fast,
rg also has some really
One such switch is especially useful for Emacs:
-M, --max-columns NUM
: Don't print lines longer than this limit in bytes. Longer lines are omitted,
and only the number of matches in that line is printed.
-M switch is useful twofold:
- Emacs is slow when dealing long lines (by long I mean thousands of chars per line)
- Emacs is slow at accepting a huge amount of output from a process
For each character you add to your input,
counsel-rg starts a new
shell command to recalculate the matches with the new input. This
means that in order to avoid keyboard lag there's only about 0.1
seconds available for both:
- Running the shell command.
- Accepting output from the shell command.
So I'm quite happy that
rg speeds up both steps. Less time spent on
these steps provides for much smoother searching.
I also work with large log files, one file at a time. For a long time,
counsel-grep-or-swiper as my main search command:
(global-set-key (kbd "C-s") 'counsel-grep-or-swiper)
But for a 40Mb log file with really long lines
counsel-grep-or-swiper started to lag a bit. I tried
and it was actually faster than
grep, although it was searching the
whole directory. So I thought, why not use
rg instead of
switch is actually really easy and required only a simple user
"rg -i -M 120 --no-heading --line-number --color never '%s' %s")
If you haven't tried
ripgrep so far, I suggest you give it a go. Happy hacking!
And if you're a C hacker and have some free time on your hands, why
not look at the long lines and the process output issues in Emacs? I'd
be very grateful:)
09 Apr 2017
Ivy is a completion method that's
Ido, but with emphasis on simplicity and customizability.
The current release constitutes of
339 commits and almost a full
year of progress since
0.8.0. Many issues ranging
to #946 were fixed.
The number of people who contributed code as grown
Details on changes
Changelog.org has been a part of the repository since
can get the details of the current and past changes:
Many improvements are incremental and don't require any extra code to
enable. I'll go over a few selected features that require a bit of
information to make a good use of them.
A better action choice interface
ivy completions, pressing M-o allows to execute
one of the custom actions for the current command. Now you have an
option to use hydra for selecting
an action. Use this code to turn on the feature:
One big advantage of the new interface is that you can peak at the
action list with M-o without dismissing the candidate
list. Press M-o again to go back to candidate selection
without selecting an action.
Here's some code from my config that ensures that I always have some extra actions to choose from:
(defun ora-insert (x)
(if (stringp x)
(defun ora-kill-new (x)
(if (stringp x)
'(("i" ora-insert "insert")
("w" ora-kill-new "copy")))
counsel-rg joins the group of grepping commands in
wraps around the newly popular and very
fast ripgrep shell tool.
A nice improvement to the grepping commands is the ability to specify
extra flags when you press C-u (
command. See this gif
for an example of excluding
*.el from the files searched by
You can now customize
#652 for using this to
counsel-git-log work on Windows.
counsel-info-lookup-symbol now substitutes the built in
- Pressing C-r while in the minibuffer of
shell-command now gives you completion of your previous
Use the new
counsel-yank-pop-separator variable to make
There was breaking change for alist type collections some months
ago. Right now the action functions receive an
item from the
collection, instead of
(cdr item) like before. If anything breaks,
the easy fix is to add an extra
cdr to the action function.
Unique index for alist completion was added. The uniqueness assumption
is that the completion system is passed a list of unique strings, of
which one (or more) are selected. Unlike plain string completion,
alists may require violating the uniqueness assumption: there may be
two elements with the same
car but different
cdr. Example: C
function declaration and definition for tag completion. Until now,
whenever two equal strings were sent to
ivy-read, only the first one
could be selected. Now, each alist car gets an integer index assigned
to it as a text property
'idx. So it's possible to differentiate two
alist items with the same key.
Action functions don't require using
with-ivy-window anymore. This
allows for a lot of simplification, e.g. use
insert instead of
(lambda (x) (with-ivy-window (insert x))).
You can now customize faces in
ivy-switch-buffer by the mode of each buffer.
Here's a snippet from my config:
'((emacs-lisp-mode . swiper-match-face-1)
(dired-mode . ivy-subdir)
(org-mode . org-level-4)))
Looks neat, I think:
swiper-include-line-number-in-search if you'd like to
match line numbers while using
Offers completion for
bookmark-jump. Press M-o d to
delete a bookmark and M-o e to edit it.
A custom option
counsel-bookmark-avoid-dired, which is off by
default, allows to continue completion for bookmarked
directories. Turn it on with:
(setq counsel-bookmark-avoid-dired t)
and when you choose a bookmarked directory, the choice will be
counsel-find-file instead of opening a
counsel-colors-emacs and counsel-colors-web
Completion for colors by name:
- the default action inserts the color name.
- M-o h inserts the color hex name.
- M-o N copies the color name to the kill ring.
- M-o H copies the color hex name to the kill ring.
The colors are displayed in the minibuffer, it looks really cool:
You also get 108 shades of grey to choose from, for some reason.
Completion for faces by name:
Shows the history of the Emacs commands executed and lets you select
and eval one
again. See #826 for a
company's candidates and inserts the result into the buffer.
counsel-dired-jump and counsel-file-jump
Jump to a directory or a file in the current directory.
counsel-dpkg and counsel-rpm
Wrap around the popular system package managers.
Install or uninstall Emacs packages with completion.
Navigate the current buffer's mark ring.
Navigate the current buffer's tags.
Navigate the current buffer's outlines.
Completion for the last hydra's heads.
Completion for headlines of files in your
Again, thanks to all the contributors. Happy hacking!
28 Mar 2017
Recently, I've had to code some C++ at work. And I saw it as a good
opportunity to step up my Emacs' IDE game. I've eschewed clang-based
tools until now, but GCC isn't adding AST support any time soon, and
CEDET is too slow and too clumsy with macros for the particular
project that I
the line in
broke the camel's back. Basically it's 30 lines of macros that expand
to 30 lines of typedefs. Maybe it's a valid implementation choice, I'd
rather avoid the macros altogether, but in any case I couldn't get
CEDET to parse that.
The first thing I tried
was rtags. My project was
CMake-based, so I just put this line in my subdirectory
cd ../build && cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=1 ..
-DCMAKE_EXPORT_COMPILE_COMMANDS=1 causes a
compile_commands.json file to be emitted during the actual
compilation. This file describes the compile flags for every source
file. These flags are essential in helping the parser understand
what's going on.
Then, in the
build directory I start:
rtags-find-symbol-at-point should work now. I still like to
use CEDET as backup, it's pretty good at tracking variables
defined in the current function:
(defun ciao-goto-symbol ()
(ring-insert find-tag-marker-ring (point-marker))
(or (and (require 'rtags nil t)
(and (require 'semantic/ia)
(define-key c++-mode-map (kbd "M-.") 'ciao-goto-symbol)
(define-key c++-mode-map (kbd "M-,") 'pop-tag-mark)
For my other C++ projects which aren't CMake-based, I use the
excellent bear tool to emit the
compile_commands.json file. It's as easy as:
Use Irony for completion
It didn't take long to figure out that
rtags isn't great at
completion. I almost accepted that's just the way it is. But this
morning I decided to make some changes and
try irony-mode. And it
worked beautifully for completion! What's ironic, is that
goto-symbol, so the time spent to figure out
was worth it.
Here's my Irony setup; I only changed the C-M-i binding to
the newly written
counsel-irony, now available in the
package on MELPA:
(add-hook 'c++-mode-hook 'irony-mode)
(add-hook 'c-mode-hook 'irony-mode)
(defun my-irony-mode-hook ()
[remap completion-at-point] 'counsel-irony)
[remap complete-symbol] 'counsel-irony))
(add-hook 'irony-mode-hook 'my-irony-mode-hook)
(add-hook 'irony-mode-hook 'irony-cdb-autosetup-compile-options)
And here are some screenshots of
First of all, the completion is displayed inline, similarly to modern IDEs.
You can use all of Ivy's regex tricks to complete your candidate:
Note how the power of regex matching allows me to narrow the initial
1622 candidates to only 22 functions that have
arguments. One of the candidates is cut off for being longer than the
window width. You can still match against the invisible text, but you
won't see it. It's possible to use C-c C-o (
store the current candidates into a buffer:
Clicking the mouse on any of the lines in the new buffer will insert
the appropriate symbol into the C++ buffer.
I'd like to thank the authors of
irony-mode for these
nice packages. Hopefully,
counsel-irony is a nice addition. Happy
18 Mar 2017
ediff.el --- a comprehensive visual interface to diff & patch
I wrote about
ediff years ago.
Today, I'll just reference a useful ediff snippet
from my config that I've added
some time ago and refined only recently.
The premise is quite simple: press e in
immediately ediff two marked files, no questions asked:
(define-key dired-mode-map "e" 'ora-ediff-files)
And here's the code, with a few bells and whistles:
;; -*- lexical-binding: t -*-
(defun ora-ediff-files ()
(let ((files (dired-get-marked-files))
(if (<= (length files) 2)
(let ((file1 (car files))
(file2 (if (cdr files)
(if (file-newer-than-file-p file1 file2)
(ediff-files file2 file1)
(ediff-files file1 file2))
(setq ediff-after-quit-hook-internal nil)
(error "no more than 2 files should be marked"))))
Some notes on how the extra code adds convenience:
In case no files are marked, the file at point is used as the first
read-file-name is used for the second file. Since I
have the magic
(setq dired-dwim-target t) in my config, in case a
dired buffer is open,
offer it as the starting directory during completion. Very useful
to compare two files in two different directories.
Depending on the order of the arguments to
changes will appear either as added or removed;
file-newer-than-file-p tries to put the arguments in a logical
order by looking at the files' last change times.
ediff-after-quit-hook-internal is used to restore the previous
window configuration after I quit
ediff with q.
That's about it. Hopefully, it's useful. Happy hacking.