key-bindings.md

  1# Key bindings
  2
  3Zed has a very customizable key binding system — you can tweak everything to work exactly how your fingers expect!
  4
  5## Predefined keymaps
  6
  7If you're used to a specific editor's defaults you can set a `base_keymap` in your [settings file](./configuring-zed.md). We currently have:
  8
  9- VSCode (default)
 10- Atom
 11- JetBrains
 12- SublimeText
 13- TextMate
 14- None (disables _all_ key bindings)
 15
 16You can also enable `vim_mode`, which adds vim bindings too.
 17
 18## User keymaps
 19
 20Zed reads your keymap from `~/.zed/keymap.json` on MacOS (or `~/.config/zed/keymap.json` on Linux). You can open the file within Zed with {#kb zed::OpenKeymap}, or via `zed: Open Keymap` in the command palette.
 21
 22The file contains a JSON array of objects with `"bindings"`. If no `"context"` is set the bindings are always active. If it is set the binding is only active when the [context matches](#contexts).
 23
 24Within each binding section a [key sequence](#keybinding-syntax) is mapped to an [action](#actions). If conflicts are detected they are resolved as [described below](#precedence).
 25
 26For example:
 27
 28```json
 29[
 30  {
 31    "bindings": {
 32      "ctrl-right": "editor::SelectLargerSyntaxNode",
 33      "ctrl-left": "editor::SelectSmallerSyntaxNode"
 34    }
 35  },
 36  {
 37    "context": "ProjectPanel && not_editing",
 38    "bindings": {
 39      "o": "project_panel::Open"
 40    }
 41  }
 42]
 43```
 44
 45You can see all of Zeds default bindings in the default keymaps for [MacOS](https://github.com/zed-industries/zed/blob/main/assets/keymaps/default-macos.json) or [Linux](https://github.com/zed-industries/zed/blob/main/assets/keymaps/default-linux.json).
 46
 47If you want to debug problems with custom keymaps you can use `debug: Open Key Context View` from the command palette. Please file [an issue](https://github.com/zed-industries/zed) if you run into something you think should work but isn't.
 48
 49### Keybinding syntax
 50
 51Zed has the ability to match against not just a single keypress, but a sequence of keys typed in order. Each key in the `"bindings"` map is a sequence of keypresses separated with a space.
 52
 53Each key press is a sequence of modifiers followed by a key. The modifiers are:
 54
 55- `ctrl-` The control key
 56- `cmd-`, `win-` or `super-` for the platform modifier (Command on macOS, Windows key on Windows, and the Super key on Linux).
 57- `alt-` for alt (option on macOS)
 58- `shift-` The shift key
 59- `fn-` The function key
 60
 61The keys can be any single unicode codepoint that your keyboard generates (for example `a`, `0`, `£` or `ç`), or any named key (`tab`, `f1`, `shift`, or `cmd`).
 62
 63A few examples:
 64
 65```json
 66 "bindings": {
 67   "cmd-k cmd-s": "zed::OpenKeymap", // matches ⌘-k then ⌘-s
 68   "space e": "editor::Complete", // type space then e
 69   "ç": "editor::Complete", // matches ⌥-c
 70   "shift shift": "file_finder::Toggle", // matches pressing and releasing shift twice
 71 }
 72```
 73
 74The `shift-` modifier can only be used in combination with a letter to indicate the uppercase version. For example `shift-g` matches typing `G`. Although on many keyboards shift is used to type punctuation characters like `(`, the keypress is not considered to be modified and so `shift-(` does not match.
 75
 76The `alt-` modifier can be used on many layouts to generate a different key. For example on macOS US keyboard the combination `alt-c` types `ç`. You can match against either in your keymap file, though by convention Zed spells this combination as `alt-c`.
 77
 78It is possible to match against typing a modifier key on its own. For example `shift shift` can be used to implement JetBrains search everywhere shortcut. In this case the binding happens on key release instead of key press.
 79
 80### Contexts
 81
 82If a binding group has a `"context"` key it will be matched against the currently active contexts in Zed.
 83
 84Zed's contexts make up a tree, with the root being `Workspace`. Workspaces contain Panes and Panels, and Panes contain Editors, etc. The easiest way to see what contexts are active at a given moment is the key context view, which you can get to with `debug: Open Key Context View` in the command palette.
 85
 86Contexts can contain extra attributes in addition to the name, so that you can (for example) match only in markdown files with `"context": "Editor && extension==md"`. It's worth noting that you can only use attributes at the level they are defined.
 87
 88For example:
 89
 90```
 91# in an editor, it might look like this:
 92Workspace os=macos
 93  Pane
 94    Editor mode=full extension=md inline_completion vim_mode=insert
 95
 96# in the project panel
 97Workspace os=macos
 98  Dock
 99    ProjectPanel not_editing
100```
101
102Context expressions can contain the following syntax:
103
104- `X && Y`, `X || Y` to and/or two conditions
105- `!X` to negate a condition
106- `(X)` for grouping
107- `X > Y` to match if a parent in the tree matches X and this layer matches Y.
108
109If you're using Vim mode, we have information on how [vim modes influence the context](./vim.md#contexts)
110
111### Actions
112
113Pretty much all of Zed's functionality is exposed as actions. Although there is
114no explicitly documented list, you can find most of them by searching in the
115command palette, by looking in the default keymaps for
116[MacOS](https://github.com/zed-industries/zed/blob/main/assets/keymaps/default-macos.json)
117or
118[Linux](https://github.com/zed-industries/zed/blob/main/assets/keymaps/default-linux.json), or by using Zed's autocomplete in your keymap file.
119
120Most actions do not require any arguments, and so you can bind them as strings: `"ctrl-a": "language_selector::Toggle"`. Some require a single argument, and must be bound as an array: `"ctrl-a": ["workspace::ActivatePaneInDirection", "down"]`. Some actions require multiple arguments, and are bound as an array of a string and an object: `"ctrl-a": ["pane::DeploySearch", { "replace_enabled": true }]`.
121
122### Precedence
123
124When multiple keybindings have the same keystroke and are active at the same time, precedence is resolved in two ways:
125
126- Bindings that match on lower nodes in the context tree win. This means that if you have a binding with a context of `Editor` it will take precedence over a binding with a context of `Workspace`. Bindings with no context match at the lowest level in the tree.
127- If there are multiple bindings that match at the same level in the tree, then the binding defined later takes precedence. As user keybindings are loaded after system keybindings, this allows user bindings to take precedence over builtin keybindings.
128
129The other kind of conflict that arises is when you have two bindings, one of which is a prefix of the other. For example if you have `"ctrl-w":"editor::DeleteToNextWordEnd"` and `"ctrl-w left":"editor::DeleteToEndOfLine"`.
130
131When this happens, and both bindings are active in the current context, Zed will wait for 1 second after you tupe `ctrl-w` to se if you're about to type `left`. If you don't type anything, or if you type a different key, then `DeleteToNextWordEnd` will be triggered. If you do, then `DeleteToEndOfLine` will be triggered.
132
133## Tips and tricks
134
135### Disabling a binding
136
137If you'd like a given binding to do nothing in a given context you can use
138`null` as the action. This is useful if you hit the keybinding by accident and
139want to disable it, or if you want to type the character that would be typed by
140the sequence (for example to disable the builtin `alt-t` binding), or if you
141want to disable multikey bindings starting with that key.
142
143```
144"bindings": { "cmd-k": null }
145```
146
147A `null` has the same precedence rules as normal actions. So disables all bindings that would match further up in the tree too. If you'd like a binding that matches further up in the tree to take precedence over a lower binding, you need to rebind it to the action you want in the context you want.
148
149### Remapping keys
150
151A common request is to be able to map from a single keystroke to a sequence. You can do this with the `workspace::SendKeystrokes` action.
152
153```json
154[
155  {
156    "bindings": {
157      "alt-down": ["workspace::SendKeystrokes", "down down down down"],
158      "cmd-alt-c": [
159        "workspace::SendKeystrokes",
160        "cmd-shift-p copy relative path enter"
161      ],
162      "cmd-alt-r": ["workspace::SendKeystrokes", "cmd-p README enter"]
163    }
164  },
165  {
166    "context": "Editor && vim_mode == insert",
167    "bindings": {
168      "j k": ["workspace::SendKeystrokes", "escape"]
169    }
170  }
171]
172```
173
174There are some limitations to this, notably:
175
176- Any asynchronous operation will not happen until after all your key bindings have been dispatched. For example this means that while you can use a binding to open a file (as in the `cmd-alt-r` example) you cannot send further keystrokes and hope to have them interpreted by the new view.
177- Other examples of asynchronous things are: opening the command palette, communicating with a language server, changing the language of a buffer, anything that hits the network.
178- There is a limit of 100 simulated keys at a time.
179
180The argument to `SendKeystrokes` is a space-separated list of keystrokes (using the same syntax as above). Due to the way that keystrokes are parsed, any segment that is not recognized as a keypress will be sent verbatim to the currently focused input field.
181
182If the argument to `SendKeystrokes` contains the binding used to trigger it, it will use the next-highest-precedence definition of that binding. This allows you to extend the default behaviour of a key binding.
183
184### Forward keys to terminal
185
186If you're on Linux or Windows, you might find yourself wanting to forward key combinations to the built-in terminal instead of them being handled by Zed.
187
188For example, `ctrl-n` creates a new tab in Zed on Linux. If you want to send `ctrl-n` to the built-in terminal when it's focused, add the following to your keymap:
189
190```json
191{
192  "context": "Terminal",
193  "bindings": {
194    "ctrl-n": ["terminal::SendKeystroke", "ctrl-n"]
195  }
196}
197```
198
199### Task Key bindings
200
201You can also bind keys to launch Zed Tasks defined in your tasks.json.
202See the [tasks documentation](tasks.md#custom-keybindings-for-tasks) for more.