A More Evil Helm

Modes, Submodes, States, and Now Hydras

The hydra package for emacs basically provides an extremely convenient way to create custom, persistent states where single keys will have a different effect than they normally would. The point is the same as that behind modal editing. If you’re going to be performing multiple related actions in a sequence, it is more efficient to enter a state where you can execute those actions using only single keys.

I use the word “state” because the word “mode” has a very different different meaning in emacs (kind of like “yank” does). The equivalent of a vim mode in emacs is basically an evil state (using the evil plugin). The equivalent of a vim-submode would be a hydra. There have already been evil plugins such as evil-lisp-state and code in spacemacs that create mini-states for more specific tasks, but hydra makes the creation of these much simpler.

A commonly given example of when one might use a hydra is when repeating an action over and over, such as scrolling or zooming. However, it’s generally simpler to just use the dot operator for repeating single actions. A better one would be the example given by Oleh Krehel (hydra’s author) and bcarell for switching between splits.

Hydra has other convenience features, such as allowing for help text for each key or “head” in the hydra to be printed in the echo area. It does everything one would initially hope for and allows for global hydras as well as hydras in specific major modes. It even makes a “color” distinction where a head with the color blue will exit the state while those that are red (default) will not.

A Hydra to Make Helm More Like Unite

Helm is pretty much the emacs equivalent of unite except even more integrated. One thing I sometimes miss when using Helm is the ability to switch from insert to normal to do things like mark or move between candidates. I originally created mini-states using the same method evil-lisp-state uses. It works, but it’s ugly, long (>60 lines), and you have to define a new evil state each time. Spacemacs has a more simple way to do things if you want to extract it, but I prefer hydra. With hydra there’s no evil involved, and it’s as easy as this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
(defhydra helm-like-unite ()
  "vim movement"
  ("?" helm-help "help")
  ("<escape>" keyboard-escape-quit "exit")
  ("<SPC>" helm-toggle-visible-mark "mark")
  ("a" helm-toggle-all-marks "(un)mark all")
  ;; not sure if there's a better way to do this
  ("/" (lambda ()
          (interactive)
          (execute-kbd-macro [?\C-s]))
       "search")
  ("v" helm-execute-persistent-action)
  ("g" helm-beginning-of-buffer "top")
  ("G" helm-end-of-buffer "bottom")
  ("j" helm-next-line "down")
  ("k" helm-previous-line "up")
  ("i" nil "cancel"))

Then bind it:

1
2
3
4
;; to escape
(define-key helm-map (kbd "<escape>") 'helm-like-unite/body)
;; or with key-chord.el; suggested by ReneFroger
(key-chord-define minibuffer-local-map "jk" 'helm-like-unite/body)

Now you can enter the helm-like-unite hydra with escape in helm and then use j and k to navigate up and down and space to mark candidates. Hydra even makes numbers work as digit arguments, so you can do 9k as you would in vim. You can use escape to quit helm or i to return to normal functionality. Keys not mapped in a hydra will exit the hydra, so return and tab will have their normal behaviour. ? will bring up helm’s help. / will do whatever C-s would do for those selected buffers (e.g. helm-buffers-run-multi-occur or helm-ff-run-grep). v will describe the function, or preview the buffer/file, or go to a line (in helm occur), etc.

It’s trivial to add more actions, for example binding p to helm-copy-to-buffer or d and u to helm-next-page and helm-previous-page. Here are some other suggestions:

1
2
3
4
5
6
;; suggested by ReneFroger
("h" helm-previous-source)
("l" helm-next-source)
;; suggested by Sylvain Benner (syl20bnr)
;; if you want to use a key besides TAB to go to action select and then exit the hydra
("r" helm-select-action :color blue)

Potential Problems

A downside could be that it now takes two escapes to get out of helm unless you use a different key to enter the hydra (one escape to enter the hydra and one to then quit helm). Anyway, this will probably appeal mostly only to evil users. If you don’t move up and down and mark specific candidates a lot in helm, this probably isn’t worth it. I still think it’s a cool example of hydra’s simplicity.

Update

One annoyance is that helm will override the hydra help echo after a few seconds. However, hydra now has :pre and :post which allow you to, for example, change the color of the cursor when you’re in the hydra to make it clear even after the echo disappears.

Hydra has also added the new color amaranth that will change the default behaviour so that only blue heads (keys that are specified by the user to exit) will leave the hydra. This means that pressing unbound keys in the hydra won’t exit the hydra. This may be the preferred behaviour for some.

Why I One-Space

Steve Losh’s article on using two spaces after a sentence continues to pop up, going mostly unchallenged it would seem. I personally fail to see any reason to use two spaces that isn’t completely subjective and find his arguments otherwise to be self-defeating.

His Arguments for Two Spaces

  1. It looks better when using a monospaced font.
  2. It gives the user more “power”

Two-Spacing is Pretty

This is entirely subjective. I use monospaced fonts all the time and still think two spaces in between a sentence looks hideous.

Two-Spacing Gives More Power?

Okay so what about the non-trivial argument? Well, it seems to be the same as the argument he says is not convincing (less effort). His specific example for “power” only applies to vim users and only to some of them.

This is what happens when I type das in vim using his example.

Before with cursor above t:

Bob started speaking. Hello, Mr. Smi[t]h! How are you today?

After das:

Bob started speaking. How are you today?

It ends up looking exactly how I want. This is because I’m using reedes’ excellent textobj-sentence plugin that makes vim’s as and is smarter. It even allows for adding custom abbreviations to ignore. As Losh says, we can have our cake and eat it too!

Granted, this plugin didn’t exist at the time of the article’s writing (though the idea isn’t exactly hard to come up with). Even if this plugin did not currently exist, his argument is bunk. What is the benefit of being able to do things like das? If what is meant by power is not efficiency and saving keystrokes, effort, and time, then I’ve no clue what the point is. Count how many times I’ve used abbreviations in this post. You don’t even have to count the number sentences to realize the number of extra keystrokes I would have had to type to put two spaces in between every sentence is exponentially greater than the single extra keystroke it would have taken me to deal with that abbreviation if I had needed to (the dot operator). So even if space is only half a percent of keystrokes typed, the number of keystrokes needed to deal with abbreviations is even more insignificant. So much for power.

As for parsing, this is merely a hypothetical. Has he ever needed to do that? I actually often use a script for counting sentences, and it’s really not that difficult to deal with single spaced sentences like textobj-sentence does. A script that chose not to deal with it would suffer from the problem of not working with anything written by people who don’t two-space. Unless this script needed to be absolutely perfect at counting, there would be no need to two-space sentences. This method of counting one-spaced sentences only fails in the rare case that a sentence actually does end with an abbreviation.

Update (2015-09-08)

Emacs users can find an equivalent of vim-textobj-sentence here.

Using Ranger as a Save Dialog

Quick Round of Applause for Octopress

Today I found the perfect excuse to start a blog when I stumbled across github pages and octopress. It’s something I’ve been wanting to do for a while, but I never found blogger or wordpress to be that attractive. Finding out that I could easily host a blog on github and write all my posts in markdown was enough to git me to do it immediately. For a first post, I thought I’d just start with presenting something basic I did recently to start using ranger for saving downloads.

Why Ranger?

Ranger is the most efficient file manager I have come across. It already integrates easily enough with most other programs. The browser is an exception and one due not to any fault of ranger. You have very little choice in general when it comes to saving files using most GUI browsers. The best the default file saver has to offer you is Recent Folders, rudimentary tab completion, and the ability to open in the last directory you saved in. While it is possible to change the file picker or file saver, none of the few options (e.g. the GTK file picker for GNOME and the QT file picker for KDE) are particularly sophisticated.

Ranger is operated entirely with the keyboard. You can press '' to get to the last visited directory or '<key> to visit any directory you’ve bookmarked with a letter. This makes navigating even large directory structures fairly quick. When you don’t have a bookmark for the exact directory you want to move a file to, you can hit f and then type the name of the directory. As soon as the keys you type correspond to a unique directory, that directory will automatically be opened. Alternatively, there is fasd integration allowing you to jump immediately to a frequently/recently used directory. You can even create a fzf command to jump to a directory with a fuzzy search (e.g. using locate or find). Because of these features, using ranger can reduce a lot of scrolling and clicking to a few keypresses.

Implementation

You can write your own save dialog. I don’t know if it would be easy or even possible to write a save dialog wrapper for ranger. Because I often want to save without anything popping up (e.g. when saving related files that will be moved to the same directory), I just bypass the save dialog entirely and download everything automatically to a single folder. I used to just open ranger in this folder later and move things, but this led to a pileup of unorganized crap since I rarely got around to cleaning the folder.

Now I use pentadactyl to “replace” the default save dialog with ranger. Pentadactyl already eliminates the need for the GUI save dialog with :w for saving webpages (or whatever else the current url is) and ;s for download hinting. Path and file names can be specified with tab completion as well. Other vim-oriented browsers have similar options, but this method still isn’t nearly as quick as using ranger. An autocommand can be used to fake ranger integration:

au DownloadPost * silent !/path/to/script "<file>"

With this in place, every time something is downloaded, pentadactyl will pass the full path of the downloaded file to the specified script. Making the command silent helps to ensure that pentadactyl will not steal the focus back from ranger.

Here’s an example script to open ranger with the file cut:

1
termite --geometry="600x400" -e "ranger --selectfile=""$1"" --cmd=cut" &

It will look something like this (updated 2016-01-02):

My full script can be found here and fixes a lot of problems that can occur. For example, it uses detox to clean up the filename and ignores files saved under /tmp (happens when using “open with”). It also supports consecutively saving files to a temporary directory and then moving them all at once later. This is useful for preventing a lot of popups when saving related files (e.g. using a count or :tabdo). It has an example for automatically moving certain filetypes; I do this with torrent files. Finally, it gives an example way to use emacsclient and dired instead of ranger (which can open a lot quicker in some situations).

The full script also prevents multiple popups that would normally happen due to a pentadactyl bug. The more firefox windows opened, the more ranger instances are started. For a while, I thought this was because pentadactyl has no autocmd clearing mechanism. Sourcing an autocommands file with au Enter won’t fix this though. This bug happens because the DownloadPost autocmd will execute for every open window. I realized this because the following would still execute from a private window when called from the autocommand but not manually:

1
2
3
4
5
command! -nargs=1 maybe-open-ranger <<EOF
if !PrivateBrowsingUtils.isWindowPrivate(window)
  silent !~/bin/ranger/ranger_browser_fm.sh <args>
endif
EOF

Uploads

Pentadactyl also provides a way to avoid using a GUI for selecting a file for upload. If you can hint an upload button, pentadactyl will give you an Upload: prompt and allow you to select a file with tab completion. Alternatively, it is possible to yank the file path in ranger beforehand and paste this into either the pentadactyl or GUI save dialog. As for creating a new file picker that somehow wrapped ranger, I have no clue how difficult this would be.

For some websites, I’d rather use a CLI program. For example, tumblr also allows to post by email and even use markdown to do so. There is also tumblr-rb. The imgur-cli python script is useful for imgur uploads, and I’ve added a keybinding for it in ranger. Imup is pretty good for anonymous (no account login) uploads to different image hosts as well. There is also plowshare for uploading to sites like mediafire.

I have not tried the following programs, but they may also be of use. For youtube, there are youtube-upload and googlecl. Googlecl is a lot more popular and does a lot more than just youtube (e.g. picasa uploads), and youtube-upload looks like it might require you to enter your password in the command. Googlecl is also in the official arch repos. For facebook, fbcommand allows uploading photos (and multiple photos at once as an album… as does googlecl with picasa by the way).