configuring_zed__configuring_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
  6Vim 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.
  7
  8This 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 leave feedback in the editor itself (`:feedback`), or [file an issue](https://github.com/zed-industries/community).
  9
 10### Zed-specific features
 11Zed is built on a modern foundation that (among other things) uses tree-sitter to understand the content of the file you're editing, and supports multiple cursors out of the box.
 12
 13Vim mode has several "core Zed" key bindings, that will help you make the most of Zed's specific feature set.
 14```
 15# Normal mode
 16g d   Go to definition
 17g D   Go to type definition
 18c d   Rename (change definition)
 19g A   Go to All references to the current word
 20
 21g <space>  Open the current search excerpt in its own tab
 22
 23g s   Find symbol in current file
 24g S   Find symbol in entire project
 25
 26g n   Add a visual selection for the next copy of the current word
 27g N   The same, but backwards
 28g >   Skip latest word selection, and add next.
 29g <   The same, but backwards
 30g a   Add a visual selection for every copy of the current word
 31
 32g h   Show inline error (hover)
 33
 34# Insert mode
 35ctrl-x ctrl-o  Open the completion menu
 36ctrl-x ctrl-c  Request Github Copilot suggestion (if configured)
 37ctrl-x ctrl-a  Open the inline AI assistant (if configured)
 38ctrl-x ctrl-l  Open the LSP code actions
 39ctrl-x ctrl-z  Hides all suggestions
 40```
 41
 42Vim 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.
 43
 44Vim mode emulates visual block mode using Zed's multiple cursor support. This again leads to some differences, but is much more powerful.
 45
 46Finally, 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.
 47
 48### Custom key bindings
 49Zed does not yet have an equivalent to vim’s `map` command to convert one set of keystrokes into another, however you can bind any sequence of keys to fire any Action documented in the  [Key bindings documentation](https://docs.zed.dev/configuration/key-bindings).
 50
 51You can edit your personal key bindings with `:keymap`.
 52For vim-specific shortcuts, you may find the following template a good place to start:
 53
 54```json
 55[
 56  {
 57    "context": "Editor && VimControl && !VimWaiting && !menu",
 58    "bindings": {
 59      // put key-bindings here if you want them to work in normal & visual mode
 60    }
 61  },
 62  {
 63    "context": "Editor && vim_mode == normal && !VimWaiting && !menu",
 64    "bindings": {
 65      // put key-bindings here if you want them to work only in normal mode
 66    }
 67  },
 68  {
 69    "context": "Editor && vim_mode == visual && !VimWaiting && !menu",
 70    "bindings": {
 71      // visual, visual line & visual block modes
 72    }
 73  },
 74  {
 75    "context": "Editor && vim_mode == insert && !menu",
 76    "bindings": {
 77      // put key-bindings here if you want them to work in insert mode
 78    }
 79  }
 80]
 81```
 82
 83You can see the bindings that are enabled by default in vim mode [here](https://zed.dev/ref/vim.json).
 84
 85The details of the context are a little out of scope for this doc, but suffice to say that `menu` is true when a menu is open (e.g. the completions menu), `VimWaiting` is true after you type `f` or `t` when we’re waiting for a new key (and you probably don’t want bindings to happen). Please reach out on [Github](https://github.com/zed-industries/community) if you want help making a key bindings work.
 86
 87### Command palette
 88
 89Vim 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.
 90
 91Additionally 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.
 92
 93We 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/community) as you find things that are missing from the command palette.
 94
 95As mentioned above, one thing to be aware of is that the regex engine is slightly different from vim's in `:%s/a/b`.
 96
 97Currently supported vim-specific commands (as of Zed 0.106):
 98```
 99# window management
100:w[rite][!], :wq[!], :q[uit][!], :wa[ll][!], :wqa[ll][!], :qa[ll][!], :[e]x[it][!], :up[date]
101    to save/close tab(s) and pane(s) (no filename is supported yet)
102:cq
103    to quit completely.
104:vs[plit], :sp[lit]
105    to split vertically/horizontally (no filename is supported yet)
106:new, :vne[w]
107    to create a new file in a new pane above or to the left
108:tabedit, :tabnew
109    to create a new file in a new tab.
110:tabn[ext], :tabp[rev]
111    to go to previous/next tabs
112:tabc[lose]
113    to close the current tab
114
115# navigating diagnostics
116:cn[ext], :cp[rev], :ln[ext], :lp[rev]
117    to go to the next/prev diagnostics
118:cc, :ll
119    to open the errors page
120
121# jump to position
122:<number>
123    to jump to a line number
124:$
125    to jump to the end of the file
126:/foo and :?foo
127    to jump to next/prev line matching foo
128
129# replacement
130:%s/foo/bar/
131    to replace instances of foo with bar (/g is always assumed, the range must always be %, and Zed uses different regex syntax to vim)
132
133# editing
134:j[oin]
135    to join the current line (no range is yet supported)
136:d[elete][l][p]
137    to delete the current line (no range is yet supported)
138:s[ort] [i]
139    to sort the current selection (with i, case-insensitively)
140```
141
142
143### Related settings
144There are a few Zed settings that you may also enjoy if you use vim mode:
145```
146{
147  // disable cursor blink
148  "cursor_blink": false
149  // use relative line numbers
150  "relative_line_numbers": true,
151  // hide the scroll bar
152  "scrollbar": {"show": "never"},
153}
154```
155
156### Regex differences
157
158Zed uses a different regular expression engine from Vim. This means that you will have to use a different syntax for some things.
159
160Notably:
161* Vim uses `\(` and `\)` to represent capture groups, in Zed these are `(` and `)`.
162* On the flip side, `(` and `)` represent literal parentheses, but in Zed these must be escaped to `\(` and `\)`.
163* When replacing, Vim uses `\0` to represent the entire match, in Zed this is `$0`, same for numbered capture groups `\1` -> `$1`.
164* Vim uses `\<` and `\>` to represent word boundaries, in Zed these are both handled by `\b`
165* Vim uses `/g` to indicate "all matches on one line", in Zed this is implied
166* 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`.
167
168To 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".
169
170For the full syntax supported by Zed's regex engine see the [regex crate documentation](https://docs.rs/regex/latest/regex/#syntax).