How to add a keybinding
This guide shows you how to bind a key combination to an editor action. As a working example, you bind Ctrl+G to jump the cursor to the top of the file.
Prerequisites
- A working build (
makesucceeds) - Familiarity with C
1. Declare the handler
Add the prototype to include/prototypes.h:
void editorGotoTop(void);
2. Implement the action
Add the function to src/editor.c:
void editorGotoTop(void){
E.cx = 0;
E.cy = 0;
E.rowOff = 0;
E.colOff = 0;
}
The global editor state E (defined in include/data.h) holds the cursor position (cx, cy) and the scroll offsets (rowOff, colOff). Resetting all four moves the cursor and the viewport to the top-left corner.
3. Bind the key
In src/input.c, add a case to the switch inside editorProcessKeypress():
case CTRL_KEY('g'):
editorGotoTop();
break;
The CTRL_KEY(k) macro in include/common.h masks the character with 0x1f, which mirrors how the terminal encodes Ctrl combinations.
4. Rebuild and test
make
./B-textEditor README.md
Scroll down with Page Down, then press Ctrl+G. The cursor returns to line 1, column 1.
Avoid reserved combinations
The terminal driver intercepts some Ctrl combinations before the editor sees them. Ctrl+C, Ctrl+Z, and Ctrl+D carry signal or EOF semantics. Check the existing cases in editorProcessKeypress() before you choose a key: Ctrl+Q, Ctrl+S, Ctrl+F, Ctrl+H, and Ctrl+L are already taken.