1# Vim Mode
2
3Zed includes a Vim emulation layer known as "vim mode". On this page, you will learn how to turn Zed's vim mode on or off, what tools and commands are available, and how to customize keybindings.
4
5## Philosophy
6
7Vim mode tries to offer a familiar experience to Vim users: it replicates the behavior of motions and commands precisely when it makes sense and uses Zed-specific functionality to provide an editing experience that "just works" without requiring configuration on your part. This includes support for semantic navigation, multiple cursors, or other features usually provided by plugins like surrounding text.
8
9So, Zed's vim mode does not replicate Vim one-to-one, but it meshes Vim's modal design with Zed's modern features to provide a more fluid experience. It's also configurable, so you can add your own key bindings or override the defaults.
10
11> **Note:** The foundations of Zed's vim mode should already cover many use cases, and we're always looking to improve it. If you find missing features that you rely on in your workflow, please [file an issue](https://github.com/zed-industries/zed/issues).
12
13## Enabling and disabling vim mode
14
15When you first open Zed, a checkbox will appear on the welcome screen, allowing you to enable vim mode.
16
17If you missed this, you can toggle vim mode on or off anytime by opening the command palette and using the workspace command `toggle vim mode`.
18
19## Zed-specific features
20
21Zed 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.
22
23Vim mode has several "core Zed" key bindings that will help you make the most of Zed's specific feature set.
24
25```
26# Language server
27g d Go to definition
28g D Go to declaration
29g y Go to type definition
30g I Go to implementation
31
32c d Rename (change definition)
33g A Go to All references to the current word
34
35g s Find symbol in current file
36g S Find symbol in entire project
37
38g ] Go to next diagnostic
39g [ Go to previous diagnostic
40] d Go to next diagnostic
41[ d Go to previous diagnostic
42g h Show inline error (hover)
43g . Open the code actions menu
44
45# Git
46] c Go to next git change
47[ c Go to previous git change
48
49# Treesitter
50] x Select a smaller syntax node
51[ x Select a larger syntax node
52
53# Multi cursor
54g l Add a visual selection for the next copy of the current word
55g L The same, but backwards
56g > Skip latest word selection, and add next.
57g < The same, but backwards
58g a Add a visual selection for every copy of the current word
59
60# Pane management
61g / Open a project-wide search
62g <space> Open the current search excerpt
63<ctrl-w> <space> Open the current search excerpt in a split
64<ctrl-w> g d Go to definition in a split
65<ctrl-w> g D Go to type definition in a split
66
67# Insert mode
68ctrl-x ctrl-o Open the completion menu
69ctrl-x ctrl-c Request GitHub Copilot suggestion (if configured)
70ctrl-x ctrl-a Open the inline AI assistant (if configured)
71ctrl-x ctrl-l Open the code actions menu
72ctrl-x ctrl-z Hides all suggestions
73
74# Ex commands
75:E[xplore] Open the project panel
76:C[ollab] Open the collaboration panel
77:Ch[at] Open the chat panel
78:A[I] Open the AI panel
79:No[tif] Open the notifications panel
80:fe[edback] Open the feedback window
81:cl[ist] Open the diagnostics window
82:te[rm] Open the terminal
83:Ext[ensions] Open the extensions window
84```
85
86Vim 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.
87
88Vim mode emulates visual block mode using Zed's multiple cursor support. This again leads to some differences, but is much more powerful.
89
90Vim'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.
91
92Finally, 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.
93
94## Custom key bindings
95
96You can edit your personal key bindings with `:keymap`.
97For vim-specific shortcuts, you may find the following template a good place to start.
98
99```json
100[
101 {
102 "context": "VimControl && !menu",
103 "bindings": {
104 // put key-bindings here if you want them to work in normal & visual mode
105 }
106 },
107 {
108 "context": "vim_mode == normal && !menu",
109 "bindings": {
110 // "shift-y": ["workspace::SendKeystrokes", "y $"] // use nvim's Y behavior
111 }
112 },
113 {
114 "context": "vim_mode == insert",
115 "bindings": {
116 // "j k": "vim::NormalBefore" // remap jk in insert mode to escape.
117 }
118 },
119 {
120 "context": "EmptyPane || SharedScreen",
121 "bindings": {
122 // put key-bindings here (in addition to above) if you want them to
123 // work when no editor exists
124 // "space f": "file_finder::Toggle"
125 }
126 }
127]
128```
129
130If 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.
131
132You 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).
133
134### Contexts
135
136Zed'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.
137
138Vim mode adds several contexts to the `Editor`:
139
140- `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`).
141- `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`).
142- `"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.
143
144### Restoring common text editing keybindings
145
146If you're using vim mode on Linux or Windows, you may find it overrides keybindings you can't live without: <kbd>Ctrl</kbd>+<kbd>v</kbd> to copy, <kbd>Ctrl</kbd>+<kbd>f</kbd> to search, etc. You can restore them by copying this data into your keymap:
147
148```json
149{
150 "context": "Editor && !menu",
151 "bindings": {
152 "ctrl-c": "editor::Copy", // vim default: return to normal mode
153 "ctrl-x": "editor::Cut", // vim default: decrement
154 "ctrl-v": "editor::Paste", // vim default: visual block mode
155 "ctrl-y": "editor::Undo", // vim default: line up
156 "ctrl-f": "buffer_search::Deploy", // vim default: page down
157 "ctrl-o": "workspace::Open", // vim default: go back
158 "ctrl-a": "editor::SelectAll", // vim default: increment
159 }
160},
161```
162
163## Command palette
164
165Vim 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.
166
167Additionally, 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.
168
169We do not (yet) emulate the full power of vim’s command line, in particular, 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.
170
171As mentioned above, one thing to be aware of is that the regex engine is slightly different from vim's in `:%s/a/b`.
172
173Currently supported vim-specific commands:
174
175```
176# window management
177:w[rite][!], :wq[!], :q[uit][!], :wa[ll][!], :wqa[ll][!], :qa[ll][!], :[e]x[it][!], :up[date]
178 to save/close tab(s) and pane(s) (no filename is supported yet)
179:cq
180 to quit completely.
181:vs[plit], :sp[lit]
182 to split vertically/horizontally (no filename is supported yet)
183:new, :vne[w]
184 to create a new file in a new pane above or to the left
185:tabedit, :tabnew
186 to create a new file in a new tab.
187:tabn[ext], :tabp[rev]
188 to go to previous/next tabs
189:tabc[lose]
190 to close the current tab
191
192# navigating diagnostics
193:cn[ext], :cp[rev], :ln[ext], :lp[rev]
194 to go to the next/prev diagnostics
195:cc, :ll
196 to open the errors page
197
198# handling git diff
199:dif[fupdate]
200 to view the diff under the cursor ("d o" in normal mode)
201:rev[ert]
202 to revert the diff under the cursor ("d p" in normal mode)
203
204# jump to position
205:<number>
206 to jump to a line number
207:$
208 to jump to the end of the file
209:/foo and :?foo
210 to jump to next/prev line matching foo
211
212# replacement (/g is always assumed and Zed uses different regex syntax to vim)
213:[range]s/foo/bar/
214 to replace instances of foo with bar
215
216# editing
217:j[oin]
218 to join the current line (no range is yet supported)
219:d[elete][l][p]
220 to delete the current line (no range is yet supported)
221:s[ort] [i]
222 to sort the current selection (with i, case-insensitively)
223:y[ank]
224```
225
226As any Zed command is available, you may find that it's helpful to remember mnemonics that run the correct command. For example:
227
228```
229:diffs Toggle all Hunk [Diffs]
230:cpp [C]o[p]y [P]ath to file
231:crp [C]opy [r]elative [P]ath
232:reveal [Reveal] in finder
233:zlog Open [Z]ed Log
234:clank [C]ancel [lan]guage server work[k]
235```
236
237## Settings
238
239Vim mode is not enabled by default. To enable vim mode, you need to add the following configuration to your settings file:
240
241```json
242{
243 "vim_mode": true
244}
245```
246
247Alternatively, you can enable vim mode by running the `toggle vim mode` command from the command palette.
248
249Some vim settings are available to modify the default vim behavior:
250
251```json
252{
253 "vim": {
254 // "always": use system clipboard when no register is specified
255 // "never": don't use system clipboard unless "+ or "* is specified
256 // "on_yank": use system clipboard for yank operations when no register is specified
257 "use_system_clipboard": "always",
258 // Let `f` and `t` motions extend across multiple lines
259 "use_multiline_find": true,
260 // Let `f` and `t` motions match case insensitively if the target is lowercase
261 "use_smartcase_find": true,
262 // Use relative line numbers in normal mode, absolute in insert mode
263 // c.f. https://github.com/jeffkreeftmeijer/vim-numbertoggle
264 "toggle_relative_line_numbers": true,
265 // Add custom digraphs (e.g. ctrl-k f z will insert a zombie emoji)
266 "custom_digraphs": {
267 "fz": "🧟♀️"
268 }
269 }
270}
271```
272
273There are also a few Zed settings that you may also enjoy if you use vim mode:
274
275```json
276{
277 // disable cursor blink
278 "cursor_blink": false,
279 // use relative line numbers
280 "relative_line_numbers": true,
281 // hide the scroll bar
282 "scrollbar": { "show": "never" },
283 // prevent the buffer from scrolling beyond the last line
284 "scroll_beyond_last_line": "off",
285 // allow cursor to reach edges of screen
286 "vertical_scroll_margin": 0,
287 "gutter": {
288 // disable line numbers completely:
289 "line_numbers": false
290 },
291 "command_aliases": {
292 "W": "w",
293 "Wq": "wq",
294 "Q": "q"
295 }
296}
297```
298
299If 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:
300
301```json
302{
303 "context": "Dock",
304 "bindings": {
305 "ctrl-w h": ["workspace::ActivatePaneInDirection", "Left"],
306 "ctrl-w l": ["workspace::ActivatePaneInDirection", "Right"],
307 "ctrl-w k": ["workspace::ActivatePaneInDirection", "Up"],
308 "ctrl-w j": ["workspace::ActivatePaneInDirection", "Down"]
309 // ... or other keybindings
310 }
311}
312```
313
314Subword motion is not enabled by default. To enable it, add these bindings to your keymap.
315
316```json
317[
318 {
319 "context": "VimControl && !menu && vim_mode != operator",
320 "bindings": {
321 "w": "vim::NextSubwordStart",
322 "b": "vim::PreviousSubwordStart",
323 "e": "vim::NextSubwordEnd",
324 "g e": "vim::PreviousSubwordEnd"
325 }
326 }
327]
328```
329
330Surrounding 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.
331
332```json
333{
334 "context": "vim_mode == visual",
335 "bindings": {
336 "shift-s": [
337 "vim::PushOperator",
338 {
339 "AddSurrounds": {}
340 }
341 ]
342 }
343}
344```
345
346## Supported plugins
347
348Zed has nascent support for some Vim plugins:
349
350- From `vim-surround`, `ys`, `cs` and `ds` work. Though you cannot add new HTML tags yet.
351- From `vim-commentary`, `gc` in visual mode and `gcc` in normal mode. Though you cannot operate on arbitrary objects yet.
352- From `netrw`, most keybindings are supported in the project panel.
353- From `vim-spider`/`CamelCaseMotion` you can use subword motions as described above.
354
355## Regex differences
356
357Zed uses a different regular expression engine from Vim. This means that you will have to use a different syntax for some things.
358
359Notably:
360
361- Vim uses `\(` and `\)` to represent capture groups, in Zed these are `(` and `)`.
362- On the flip side, `(` and `)` represent literal parentheses, but in Zed these must be escaped to `\(` and `\)`.
363- When replacing, Vim uses `\0` to represent the entire match, in Zed this is `$0`, same for numbered capture groups `\1` -> `$1`.
364- Vim uses `/g` to indicate "all matches on one line", in Zed this is implied
365- 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`.
366
367To 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".
368
369For the full syntax supported by Zed's regex engine see the [regex crate documentation](https://docs.rs/regex/latest/regex/#syntax).