Working with Kakoune [Part 1]

Some friends recently requested an analogue of this excellent introduction to Vim techniques for the kakoune editor. This is my attempt to demonstrate the fundamental operations that allow you to be productive in kakoune. I’ll be following roughly the same outline as that Vim blog post, since I think the progression flows well and it may facilitate easy comparison between the two.

Prerequisites

You should be familiar with the basic concepts on a modal editor, but not necessarily kakoune. Vim or neovim experience would be sufficient.

You should know that in kak, the basic cursor navigation is the same as Vim. Arrows or h,j,k,l move the cursor, i,a allow you to enter insert mode, esc puts you back in normal mode, :w saves, and :q quits.

Selections

Kakoune is a selection-oriented editor. Vim had a dedicated visual mode for highlighting text. In kakoune, there’s no difference between visual mode and normal mode.

You can see this in the basic navigation commands. Each one selects a region of text. j selects the character below the cursor. l selects the character to the right of the cursor. w selects the next word.

The magic happens when you add the Shift key. Using Shift+ extends your current selection instead of selecting something new. To extend my selection to include the next word, I type W instead of w. To extend it by a single character to the left, I type H. This simple convention — that capitalized letters modify the current selection whereas lowercase ones select something new — is repeated throughout all of kak.

There are lots of ways to select text in kak, but we won’t cover them exhaustively here.

Actions

In kak, all actions (such as copy/paste/delete) apply to whatever text is selected.

Copy / Paste/ Delete

To delete a word, select it (w), then type d.

Like Vim, y will copy (“yank”), p will paste, and d will delete.

To select the current line, you can type x. Therefore, deleting the current line is xd and copying it is xy.

Merging lines

To merge a line with the line below it, use alt+j. In Vim it would be J, but kak uses capital navigation to modify the selection. J is already taken as “extend the selection downward”.

Wrapping lines at a length

Kak works very hard not to reinvent the wheel. Instead of rewriting common functionality that is implemented by other command line tools, kak makes it easy to leverage those tools.

Wrapping text is a great example of this. There’s already a standard shell utility for wrapping text. It’s called fold.

To wrap some text to a specific line width in kak, select the text. Then type |. This command prompts you for some external shell command. Kak will provide your selected text on stdin, and it will replace the selection with whatever the command you invoke sends on stdout.

In our case, we could use % to select a whole file, | to send to an external command, fold -w 80 -s to wrap to a width of 80 characters and to only break between words, and then hit enter to perform the change.

This may seem like too much work, but it’s trivial to make a custom shortcut for this functionality so that you don’t have to do all of that typing each time.

# put this in your ~/.config/kak/kakrc
map -docstring "wrap file to 80 char width" global user w '%|fold -w 80 -s'

Additionally, we can reuse this same send-to-external-command functionality to do some other cool tricks.

If you can, install the figlet and boxes utilities in your package manager. Then try piping line of text through one (or both) of those.

Remember, x to select a line, so x|figlet would send the current line through figlet.

Try selecting any text and doing |figlet | boxes<ret>. Now that’s the power of composition!

I think I’ll stop here for now, but rest assured that there’s a lot more where this came from. I hope to unpack more of it for you soon!

Photo credit:
Victoria Chen

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.