Kakoune, the Text Editor I Didn’t Know I Needed

Like the idea of Emacs and Vim but think it is too complicated?

If you are a software developer, you may have tried Emacs and Vim at some point in your life. If you are like me and kind of have a goldfish memory for complicated keyboard combinations, you would have given up on these editors already deciding they are too much work to learn.

I’ve learned the basics of Vim but never got efficient at using it. Instead my goto editor ended up being TextMate on the Mac. It tried to take idea of these two old Unix editor and bring them into the modern era.

So far so good, but I have lately found myself having to ssh into a Linux box. That is when you realize that a text editor working well in a terminal would be really handy.

I am willing to sacrifice some power for better ease of use. Kakoune maybe just that kind of editor. It is a sort of Vim replacement. It has made a number of design changes relative to Vim that makes it much easier to become productive for people who don’t want to spend lots of time learning Vim.

Vim has this powerful idea of combining actions. E.g. I can can hit w to move a word or 3w to move 3 words. I can prepend an action such as delete d to combine actions, so d3w, deletes 3 words.

Kakoune has taken this same concept and reversed it. So you write 3wd instead. That is you specify the objects (part of the text) you want to do something with and the action last.

This may not sound like much, but it makes a huge difference. Why? Because it allows you to see at all times what text you are performing a command on. Kakoune always highlights the text that will be affected by the next action. So if I hit 3w, I will see 3 words getting highlighted. Hence there is no guessing game what words will be deleted by d.

Maybe this does not sound like much to you, but once you start using it, you realize how much it simplifies everything. You can use any combination of commands to get text selected. Once the text is selected you can do a whole host of things:

  • Delete it
  • Replace the text with something else you write. Remember the cursor can be in multiple locations, so you can write replacement text for many locations in one go.
  • Perform a regexp search only within selected text.
  • Pipe selected text into a unix command and replace selected text with result. E.g. you can select a list of words and pipe the words into the unix sort command to get the text sorted.

What helps doing stuff, is that you get online help for some much of what you do. Vim and Emacs does not give this level of constant feedback. In many ways using Kakoune is like using a GUI app. The advantage of GUIs over command line interfaces is that they display all the options available to you. Kakoune is very good at showing you what options are available and what it expects from you. Whenever you start writing various commands it will tell you what it can complete to, and what possible arguments it takes.

For people like me with Goldfish memory when it comes to advance text editors this is a big advantage.

To install it I just use homebrew.

$ brew install kakoune

Part of the reason I am writing this intro, is because of one really annoying thing I spent a lot of time to figure out, and that is to get the alt key to work in Kakoune. Without the alt key a whole bunch important stuff will not work. Go to Terminal.app preferences and check the "Use Options as Meta key" checkbox, as shown below.

Image for post

Now you can edit a file by writing kak in the terminal. E.g.

$ kak somefilename.txt

You can configure this for iTerm2 too if you use that instead. For left and right option key, you need to select the Esc+ option as shown below.

Image for post

You can find extensive documentation of all the commands here on the official site. So instead I will focus on explaining how to read the docs and what are the most useful keyboard commands to get you started.

The documentation will write keyboard commands like <c-o> and <a-X>. If you are a bit slow on the uptake you, like me, you did not immediately realize this means Ctrl+o and Alt+Shift+X. So a means Alt or Option key, while a capital letter means you need to hold Shift.

So here is my list of commands according to need.

  • <esc> That means the Escape key. Use it to enter command mode. You need to get into command mode first if you want to exit the program, or issue any other commands.
  • :q Exit Kakoune. If you don't want to save, write :q!.
  • :w Save file you are currently editing
  • i Insert text at current cursor location. This throws you into insertion mode or writing mode, whatever you may call it. It alls you to actually write text in the file. To get back to issuing commands again hit <esc>.

A lot of these commands are used to navigate and select text. But remember you can use the shift key to add to the selection. E.g. each time you hit w that jumps one word and selects that word. The previous word becomes deselected. However if you hold shift and hit w three times, you will end up with 3 words elected. So 3W selects 3 words.

  • w jump one word forward and select it.
  • b jump backwards one word and select it.
  • h one character back.
  • l one character forward
  • <a-h> jump to beginning of line and create selection from current position to beginning.
  • <a-l> jump to end of line.
  • % Select all text.
  • <space> Deselect selection. Useful if you got multiple cursors.
  • u Undo last edit.
  • y Copy (yank) current selection.
  • p Past what was copied.
  • ` Uppercase selection.
  • ~ Lowercase selection.
  • / Search for a text. You can use <tab> to compete words, instead of writing them out fully. Readline keybindings also work so, you can use e.g. <c-w> (Ctrl+w) too delete a word you wrote.<c-a> and <c-e> to jump to beginning or end of text.
  • n Look for next matching text searched for with /
  • <a-n> Previous match. So reverse of n.
  • s Substitution using Regexp. You need to select some text to apply this to first. Remember you can combine this. You can write "foobar" to select all occurrences of "foobar" within the selection. Hit enter, and then ` to make "foobar" into uppercase.

As programmers we often deal with code grouped in between parenthesis, braces, quotes etc, and we want to treat these blocks of code as a unit. Maybe you want to move a block of code or delete it. That means it is handy to be able to easily select it.

The way this works is:

  1. Decide if you are selecting the whole block or just one side. E.g. do you want the selection from your cursor position to the left side?
  2. Decide what sort of block you are selecting: a word, a string, code between curly braces etc.

Some useful commands and keys:

  • [ Chose to select all text to the left of cursor until start of block.
  • ] From cursor until end of block.
  • <a-a> From start to end of block.
  • [, ], {, }, (, )Press either of these to select the code within this type of braces or parenthesis.
  • b Alternative for selecting parenthesis
  • w Select a word. E.g. if the cursor is in the middle of a word, <a-a>, w will select the whole word.

Let us look at a bigger example. Assume you are in the middle of block atfoo:

(egg spam (bar foo bar) chicken)

And want to select the whole (bar foo bar) then you hit: <a-a>, b.

Geek dad, living in Oslo, Norway with passion for UX, Julia programming, science, teaching, reading and writing.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store