### 5.1 Key Bindings ¶

`ne` allows you to associate any keystroke with any command. These associations are referred to as key bindings. You define your key bindings in a (possibly UTF-8) file named `.keys` in your `~/.ne` directory. You could additionally create a `.keys` file in your current directory, any your system administrator might also place a `.keys` file in your system’s global directory. These will always be loaded each time you start `ne`, but you can also create key binding files with the same base name (`.keys`) followed by a dash and some string. Such a file will only be loaded when the string matches the value of your `TERM` environment variable. For example, you might have files named `~/.ne/.keys`, `~/.ne/.keys-xterm`, and `~/.ne/.keys-vt102`. The first file’s key bindings will always be loaded, while at most one of the latter two files will be loaded, depending on your `TERM` environment variable.

You can change your key binding files’ default base name from `.keys` by passing an argument to the `--keys` option when you start `ne`, and you can prevent any configuration from being loaded with the `--no-config` option (see Arguments).

The format of key binding files is simple: each line starting with the ‘`KEY`’ sequence of capital characters is considered the description of a key binding. Each line starting with ‘`SEQ`’ binds a character sequence to a key. All other lines are considered comments. The format of a key binding description is

    KEY hexcode command

The hexcode value is the ASCII code of the keystroke. (For special keys such as `Insert` or function keys, you should take a look at the file `default.keys` that comes with `ne`’s distribution: it contains a complete, commented definition of `ne`’s standard bindings that you can modify with a trial-and-error approach.) The easiest way to see the code `ne` uses for a given key is by using the KeyCode command. It prompts you to press a key, then reports the code for that key on the status bar. It also displays the command bound to that key if there is one.

You can write just the hexadecimal digits, nothing else is necessary (but a prefixing ‘`0x`’ is tolerated). For instance,

    KEY 1 MoveSOL

binds to ``Control`-A` the action of moving to the start of a line, while

    KEY 101 LineUp

binds to the “cursor-up” key the action of moving the cursor one line up.

command can be any `ne` command, including `Escape` (which allows reconfiguring the menu activator) and `Macro`, which allows binding complex sequences of actions to a single keystroke. The binding of a macro is very fast because on the first call the macro is cached in memory. See Macro.

Note that you cannot ever redefine `Return` or `Escape`. This is a basic issue—however brain damaged is the current configuration, you will always be able to exploit fully the menus and the command line.

Besides the “standard” combinations (e.g., ``Control`-letter`), it possible to program combinations based on the `Meta` key (a.k.a. `Alt`). The situation in this case is a bit more involved, because depending on the terminal emulator you are using, the effect of the `Meta` key can be widely different. For instance, `xterm` raises the eighth bit of a character, so, for instance,

    KEY 81 MoveSOF

binds ``Control`-`Meta`-a` to the action of moving to the start of the document. However, `gnome-terminal` will emit the character of ASCII code 1 prefixed with ESC instead (“`\x1b\x01`”). To handle this case, `ne` provides codes from 180 on for simulated `Meta` sequences: for instance,

    KEY 181 MoveSOF

binds the abovementioned sequence to the same action as before. In general, the code 180+x corresponds to the sequence ESC followed by the ASCII character of code x. Note that some of these sequences may be disabled, if they conflict with existing sequences of your terminal (for instance, ESC followed by ‘`O`’ is always disabled because it prefixes several built-in keyboard sequences).

As a final note, we remark that typing ``Meta`-a` on `gnome-terminal` will produce an ESC followed by ‘`a`’ (“`\x1ba`”). Since it is obviously easier to press just `Meta` rather than `Meta` and `Control` at the same time, it is a good idea to associate the same sequence also to this combination, using

    KEY 1E1 MoveSOF

Moreover, this setting provides the user with a second choice: one can press `Escape` followed by a letter instead of using modifiers.

This is the approach used by default in `ne`: this way, `Control` with `Meta` plus a letter should always work, and `Meta` should work sometimes (of course, if you’re sure to use always the same kind of emulator you can bind more features). Again, the best place to look is the `default.keys` file.

As stated above, each line starting with ‘`SEQ`’ binds a character sequence to a key code. The format for a ‘`SEQ`’ binding is

    SEQ "sequence" hexcode

where "sequence" is a double-quoted string of characters (which can include escaped hexadecimals) followed by a hexadecimal key code as described above for ‘`KEY`’ definitions.

You should rarely need this, as properly configured systems already do this for most keys. However, some key combinations (`Control` in conjunction with cursor keys for example) are usually not defined. If you know the character sequence your system generates for such a combination, you may use ‘`SEQ`’ to bind that sequence to a particular key code if that sequence isn’t already defined on your system. For example, `Control`-“cursor-left” may generate the sequence `\x1b[1;5D`. The following lines bind that sequence to the `f10` key code ‘`14A`’, then bind that key code to the ‘`HELP`’ command.

    SEQ  "\x1b[1;5D"  14A
    KEY  14A          HELP

Sequences are inherently terminal- or terminal emulator-specific, so their utility will vary depending on how many emulators you use. At least they give you the possibility to use keys or key combinations that aren’t covered by `curses`.

The key binding file is parsed at startup. If something does not work, `ne` exits displaying an error message. If you want `ne` to skip parsing the key binding file (for instance, to correct the broken file), just give `ne` the `--no-config` argument. See Arguments.