vim.md

  1# Vim Mode
  2
  3Zed includes a vim emulation layer known as "vim mode". This document aims to describe how it works, and how to make the most out of it.
  4
  5## Philosophy
  6
  7Vim mode in Zed is supposed to primarily "do what you expect": it mostly tries to copy vim exactly, but will use Zed-specific functionality when available to make things smoother.
  8
  9This means Zed will never be 100% Vim compatible, but should be 100% Vim familiar! We expect that our Vim mode already copes with 90% of your workflow, and we'd like to keep improving it. If you find things that you can’t yet do in Vim mode, but which you rely on in your current workflow, please [file an issue](https://github.com/zed-industries/zed/issues).
 10
 11## Zed-specific features
 12
 13Zed is built on a modern foundation that (among other things) uses tree-sitter and language servers to understand the content of the file you're editing, and supports multiple cursors out of the box.
 14
 15Vim mode has several "core Zed" key bindings, that will help you make the most of Zed's specific feature set.
 16
 17```
 18# Language server
 19g d     Go to definition
 20g D     Go to declaration
 21g y     Go to type definition
 22g I     Go to implementation
 23
 24c d     Rename (change definition)
 25g A     Go to All references to the current word
 26
 27g s   Find symbol in current file
 28g S   Find symbol in entire project
 29
 30g ]   Go to next diagnostic
 31g [   Go to previous diagnostic
 32] d   Go to next diagnostic
 33[ d   Go to previous diagnostic
 34g h   Show inline error (hover)
 35g .   Open the code actions menu
 36
 37# Git
 38] c   Go to next git change
 39[ c   Go to previous git change
 40
 41# Treesitter
 42] x   Select a smaller syntax node
 43[ x   Select a larger syntax node
 44
 45# Multi cursor
 46g l   Add a visual selection for the next copy of the current word
 47g L   The same, but backwards
 48g >   Skip latest word selection, and add next.
 49g <   The same, but backwards
 50g a   Add a visual selection for every copy of the current word
 51
 52# Pane management
 53g /        Open a project-wide search
 54g <space>  Open the current search excerpt
 55<ctrl-w> <space>  Open the current search excerpt in a split
 56<ctrl-w> g d      Go to definition in a split
 57<ctrl-w> g D      Go to type definition in a split
 58
 59# Insert mode
 60i a / a a      Select the function argument the cursor is in
 61ctrl-x ctrl-o  Open the completion menu
 62ctrl-x ctrl-c  Request GitHub Copilot suggestion (if configured)
 63ctrl-x ctrl-a  Open the inline AI assistant (if configured)
 64ctrl-x ctrl-l  Open the code actions menu
 65ctrl-x ctrl-z  Hides all suggestions
 66
 67# Ex commands
 68:E[xplore]    Open the project panel
 69:C[ollab]     Open the collaboration panel
 70:Ch[at]       Open the chat panel
 71:A[I]         Open the AI panel
 72:No[tif]      Open the notifications panel
 73:fe[edback]   Open the feedback window
 74:cl[ist]      Open the diagnostics window
 75:te[rm]       Open the terminal
 76:Ext[ensions] Open the extensions window
 77```
 78
 79Vim mode uses Zed to define concepts like "brackets" (for the `%` key) and "words" (for motions like `w` and `e`). This does lead to some differences, but they are mostly positive. For example `%` considers `|` to be a bracket in languages like Rust; and `w` considers `$` to be a word-character in languages like Javascript.
 80
 81Vim mode emulates visual block mode using Zed's multiple cursor support. This again leads to some differences, but is much more powerful.
 82
 83Vim's macro support (`q` and `@`) is implemented using Zed's actions. This lets us support recording and replaying of autocompleted code, etc. Unlike Vim, Zed does not re-use the yank registers for recording macros, they are two separate namespaces.
 84
 85Finally, Vim mode's search and replace functionality is backed by Zed's. This means that the pattern syntax is slightly different, see the section on [Regex differences](#regex-differences) for details.
 86
 87## Custom key bindings
 88
 89You can edit your personal key bindings with `:keymap`.
 90For vim-specific shortcuts, you may find the following template a good place to start.
 91
 92> **Note:** We made some breaking changes in Zed version `0.145.0`. For older versions, see [the previous version of this document](https://github.com/zed-industries/zed/blob/c67aeaa9c58619a58708722ac7d7a78c75c29336/docs/src/vim.md#L90).
 93
 94```json
 95[
 96  {
 97    "context": "VimControl && !menu",
 98    "bindings": {
 99      // put key-bindings here if you want them to work in normal & visual mode
100    }
101  },
102  {
103    "context": "vim_mode == insert",
104    "bindings": {
105      // "j k": "vim::NormalBefore" // remap jk in insert mode to escape.
106    }
107  },
108  {
109    "context": "EmptyPane || SharedScreen",
110    "bindings": {
111      // put key-bindings here (in addition to above) if you want them to
112      // work when no editor exists
113      // "space f": "file_finder::Toggle"
114    }
115  }
116]
117```
118
119If you would like to emulate vim's `map` (`nmap` etc.) commands you can bind to the [`workspace::SendKeystrokes`](./key-bindings.md#remapping-keys) action in the correct context.
120
121You can see the bindings that are enabled by default in vim mode [here](https://github.com/zed-industries/zed/blob/main/assets/keymaps/vim.json).
122
123#### Contexts
124
125Zed's keyboard bindings are evaluated only when the `"context"` matches the location you are in on the screen. Locations are nested, so when you're editing you're in the `"Workspace"` location is at the top, containing a `"Pane"` which contains an `"Editor"`. Contexts are matched only on one level at a time. So it is possible to combine `Editor && vim_mode == normal`, but `Workspace && vim_mode == normal` will never match because we set the vim context at the `Editor` level.
126
127Vim mode adds several contexts to the `Editor`:
128
129- `vim_mode` is similar to, but not identical to, the current mode. It starts as one of `normal`, `visual`, `insert` or `replace` (depending on your mode). If you are mid-way through typing a sequence, `vim_mode` will be either `waiting` if it's waiting for an arbitrary key (for example after typing `f` or `t`), or `operator` if it's waiting for another binding to trigger (for example after typing `c` or `d`).
130- `vim_operator` is set to `none` unless `vim_mode == operator` in which case it is set to the current operator's default keybinding (for example after typing `d`, `vim_operator == d`).
131- `"VimControl"` indicates that vim keybindings should work. It is currently an alias for `vim_mode == normal || vim_mode == visual || vim_mode == operator`, but the definition may change over time.
132
133### Restoring some sense of normality
134
135If you're using Vim mode on Linux or Windows, you may find that it has overridden keybindings
136that you can't live without. You can restore them to their defaults by copying these into your keymap:
137
138```json
139{
140  "context": "Editor && !menu",
141  "bindings": {
142    "ctrl-c": "editor::Copy",          // vim default: return to normal mode
143    "ctrl-x": "editor::Cut",           // vim default: decrement
144    "ctrl-v": "editor::Paste",         // vim default: visual block mode
145    "ctrl-y": "editor::Undo",          // vim default: line up
146    "ctrl-f": "buffer_search::Deploy", // vim default: page down
147    "ctrl-o": "workspace::Open",       // vim default: go back
148    "ctrl-a": "editor::SelectAll",     // vim default: increment
149  }
150},
151```
152
153## Command palette
154
155Vim mode allows you to enable Zed’s command palette with `:`. This means that you can use vim's command palette to run any action that Zed supports.
156
157Additionally vim mode contains a number of aliases for popular vim commands to ensure that muscle memory works. For example `:w<enter>` will save the file.
158
159We do not (yet) emulate the full power of vim’s command line, in particular we special case specific patterns instead of using vim's range selection syntax, and we do not support arguments to commands yet. Please reach out on [GitHub](https://github.com/zed-industries/zed) as you find things that are missing from the command palette.
160
161As mentioned above, one thing to be aware of is that the regex engine is slightly different from vim's in `:%s/a/b`.
162
163Currently supported vim-specific commands:
164
165```
166# window management
167:w[rite][!], :wq[!], :q[uit][!], :wa[ll][!], :wqa[ll][!], :qa[ll][!], :[e]x[it][!], :up[date]
168    to save/close tab(s) and pane(s) (no filename is supported yet)
169:cq
170    to quit completely.
171:vs[plit], :sp[lit]
172    to split vertically/horizontally (no filename is supported yet)
173:new, :vne[w]
174    to create a new file in a new pane above or to the left
175:tabedit, :tabnew
176    to create a new file in a new tab.
177:tabn[ext], :tabp[rev]
178    to go to previous/next tabs
179:tabc[lose]
180    to close the current tab
181
182# navigating diagnostics
183:cn[ext], :cp[rev], :ln[ext], :lp[rev]
184    to go to the next/prev diagnostics
185:cc, :ll
186    to open the errors page
187
188# jump to position
189:<number>
190    to jump to a line number
191:$
192    to jump to the end of the file
193:/foo and :?foo
194    to jump to next/prev line matching foo
195
196# replacement (/g is always assumed and Zed uses different regex syntax to vim)
197:%s/foo/bar/
198  to replace instances of foo with bar
199:X,Ys/foo/bar/
200    to limit replacement between line X and Y
201    other ranges are not yet implemented
202
203# editing
204:j[oin]
205    to join the current line (no range is yet supported)
206:d[elete][l][p]
207    to delete the current line (no range is yet supported)
208:s[ort] [i]
209    to sort the current selection (with i, case-insensitively)
210```
211
212As any Zed command is available, you may find that it's helpful to remember mnemonics that run the correct command. For example:
213
214```
215:diff   Toggle Hunk [Diff]
216:diffs  Toggle all Hunk [Diffs]
217:revert Revert Selected Hunks
218:cpp    [C]o[p]y [P]ath to file
219:crp    [C]opy [r]elative [P]ath
220:reveal [Reveal] in finder
221:zlog   Open [Z]ed Log
222```
223
224## Settings
225
226Vim mode is not enabled by default. To enable Vim mode, you need to add the following configuration to your settings file:
227
228```json
229{
230  "vim_mode": true
231}
232```
233
234Alternatively, you can enable Vim mode by running the `toggle vim mode` command from the command palette.
235
236Some vim settings are available to modify the default vim behavior:
237
238```json
239{
240  "vim": {
241    // "always": use system clipboard when no register is specified
242    // "never": don't use system clipboard unless "+ or "* is specified
243    // "on_yank": use system clipboard for yank operations when no register is specified
244    "use_system_clipboard": "always",
245    // Lets `f` and `t` motions extend across multiple lines
246    "use_multiline_find": true
247  }
248}
249```
250
251There are also a few Zed settings that you may also enjoy if you use vim mode:
252
253```json
254{
255  // disable cursor blink
256  "cursor_blink": false,
257  // use relative line numbers
258  "relative_line_numbers": true,
259  // hide the scroll bar
260  "scrollbar": { "show": "never" },
261  // allow cursor to reach edges of screen
262  "vertical_scroll_margin": 0,
263  "gutter": {
264    // disable line numbers completely:
265    "line_numbers": false
266  },
267  "command_aliases": {
268    "W": "w",
269    "Wq": "wq",
270    "Q": "q"
271  }
272}
273```
274
275If you want to navigate between the editor and docks (terminal, project panel, AI assistant, ...) just like you navigate between splits you can use the following key bindings:
276
277```json
278{
279  "context": "Dock",
280  "bindings": {
281    "ctrl-w h": ["workspace::ActivatePaneInDirection", "Left"],
282    "ctrl-w l": ["workspace::ActivatePaneInDirection", "Right"],
283    "ctrl-w k": ["workspace::ActivatePaneInDirection", "Up"],
284    "ctrl-w j": ["workspace::ActivatePaneInDirection", "Down"]
285    // ... or other keybindings
286  }
287}
288```
289
290Subword motion is not enabled by default. To enable it, add these bindings to your keymap.
291
292```json
293[
294  {
295    "context": "VimControl && !menu && vim_mode != operator",
296    "bindings": {
297      "w": "vim::NextSubwordStart",
298      "b": "vim::PreviousSubwordStart",
299      "e": "vim::NextSubwordEnd",
300      "g e": "vim::PreviousSubwordEnd"
301    }
302  }
303]
304```
305
306Surrounding the selection in visual mode is also not enabled by default (`shift-s` normally behaves like `c`). To enable it, add the following to your keymap.
307
308```json
309{
310  "context": "vim_mode == visual",
311  "bindings": {
312    "shift-s": [
313      "vim::PushOperator",
314      {
315        "AddSurrounds": {}
316      }
317    ]
318  }
319}
320```
321
322## Supported plugins
323
324Zed has nascent support for some Vim plugins:
325
326- From `vim-surround`, `ys`, `cs` and `ds` work. Though you cannot add new HTML tags yet.
327- From `vim-commentary`, `gc` in visual mode and `gcc` in normal mode. Though you cannot operate on arbitrary objects yet.
328- From `netrw`, most keybindings are supported in the project panel.
329- From `vim-spider`/`CamelCaseMotion` you can use subword motions as described above.
330
331## Regex differences
332
333Zed uses a different regular expression engine from Vim. This means that you will have to use a different syntax for some things.
334
335Notably:
336
337- Vim uses `\(` and `\)` to represent capture groups, in Zed these are `(` and `)`.
338- On the flip side, `(` and `)` represent literal parentheses, but in Zed these must be escaped to `\(` and `\)`.
339- When replacing, Vim uses `\0` to represent the entire match, in Zed this is `$0`, same for numbered capture groups `\1` -> `$1`.
340- Vim uses `/g` to indicate "all matches on one line", in Zed this is implied
341- Vim uses `/i` to indicate "case-insensitive", in Zed you can either use `(?i)` at the start of the pattern or toggle case-sensitivity with `cmd-option-c`.
342
343To help with the transition, the command palette will fix parentheses and replace groups for you when you run `:%s//`. So `%s:/\(a\)(b)/\1/` will be converted into a search for "(a)\(b\)" and a replacement of "$1".
344
345For the full syntax supported by Zed's regex engine see the [regex crate documentation](https://docs.rs/regex/latest/regex/#syntax).