Using NeoVim effectively requires mastering its keybindings, understanding how to navigate between modes, and leveraging plugins and customization to streamline your workflow. Here's a structured guide to get you started and maximize your efficiency:
NeoVim has multiple modes, and the most effective way to use it is by switching between them efficiently.
-
Normal Mode: This is the default mode (press
Esc
to enter it).- In this mode, you move around and manipulate text without actually inserting any.
-
Insert Mode: This is the mode for typing text (enter it by pressing
i
,a
,o
, etc.).- Press
Esc
to return to Normal mode from Insert mode.
- Press
-
Visual Mode: Used for selecting text. You can enter Visual mode by pressing
v
.- Press
V
for Line Visual Mode to select entire lines.
- Press
-
Command Mode: Used for executing commands (start it with
:
in Normal mode).- Commands like
:wq
(write and quit) and:q!
(quit without saving) are entered here.
- Commands like
Navigating within a file in NeoVim can be done much faster using keybindings rather than relying on arrow keys or mouse.
- h/j/k/l: Move left, down, up, right.
- w: Jump to the start of the next word.
- b: Jump to the start of the previous word.
- 0: Jump to the beginning of the line.
- $: Jump to the end of the line.
- gg: Jump to the top of the file.
- G: Jump to the bottom of the file.
- Ctrl-u: Move up half a page.
- Ctrl-d: Move down half a page.
- /: Search forward (type your search term after
/
). - n: Jump to the next occurrence of the search.
- N: Jump to the previous occurrence.
- i: Enter insert mode before the current character.
- I: Enter insert mode at the beginning of the line.
- a: Enter insert mode after the current character.
- A: Enter insert mode at the end of the line.
- o: Insert a new line below and enter insert mode.
- O: Insert a new line above and enter insert mode.
- y: Yank (copy) text.
yw
yanks a word,yy
yanks a line. - d: Delete (cut) text.
dw
deletes a word,dd
deletes a line. - p: Paste after the cursor.
- P: Paste before the cursor.
- u: Undo the last action.
- Ctrl + r: Redo the undone action.
- v: Enter visual mode to select text.
- V: Enter line-wise visual mode.
- y: Yank the selected text.
- d: Delete the selected text.
- >: Indent the selected text.
- <: Un-indent the selected text.
- :e filename: Open a new file.
- :w filename: Save the current file under a new name.
- :ls: List open buffers.
- :b1: Switch to buffer 1 (replace 1 with the buffer number).
- :bd: Close the current buffer.
- :split filename: Open a file in a horizontal split.
- :vsplit filename: Open a file in a vertical split.
- Ctrl-w + w: Switch between windows.
- Ctrl-w + q: Close the current window.
- :tabnew filename: Open a file in a new tab.
- :tabnext: Go to the next tab.
- :tabprev: Go to the previous tab.
To further enhance your NeoVim experience, you'll need a few plugins. Here’s how to use them effectively:
Use nvim-tree for file navigation. This plugin acts as a file explorer inside NeoVim.
- Toggle file tree:
<leader>e
- Navigate directories: Use arrow keys to expand/collapse directories.
Use Telescope to quickly search files, functions, and more.
- Search files:
<leader>f
- Search for text inside files:
<leader>g
- Search recent files:
<leader>r
Use gitsigns to track changes directly in your editor.
- Stage hunk:
:Gitsigns stage_hunk
- Undo stage hunk:
:Gitsigns undo_stage_hunk
- Preview hunk:
:Gitsigns preview_hunk
For programming languages, LSP support is crucial for autocompletion, linting, and formatting.
- Go to definition:
gd
- Show hover documentation:
K
- Show diagnostics (errors):
:LspDiagnostics
- Code actions:
:LspCodeAction
If you're using nvim-cmp
for completion, this allows you to autocomplete code suggestions:
- Tab: Cycle through completion suggestions.
- Enter: Select the completion.
Make your NeoVim stylish:
- Theme: Use a visually pleasing theme like
tokyonight.nvim
by adding it to yourinit.lua
and activating it viavim.cmd[[colorscheme tokyonight]]
. - Lualine: Configure
lualine
for a modern status line.
Example of configuring lualine
:
require('lualine').setup {
options = {
theme = 'tokyonight'
}
}
Custom key mappings can significantly speed up your workflow. Here are a few to make your life easier:
-- Save file with Ctrl+s
vim.api.nvim_set_keymap('n', '<C-s>', ':w<CR>', { noremap = true, silent = true })
-- Close file with Ctrl+q
vim.api.nvim_set_keymap('n', '<C-q>', ':q<CR>', { noremap = true, silent = true })
-- Quick exit from insert mode using 'jj'
vim.api.nvim_set_keymap('i', 'jj', '<Esc>', { noremap = true, silent = true })
NeoVim has excellent documentation. Use :help
to access it anytime. For example, to learn more about yank, you can run:
:help yank
The more you use NeoVim, the more natural its powerful shortcuts and keybindings will feel. Take time to practice, and soon, your speed and efficiency will dramatically increase.
Action | Command/Keybinding |
---|---|
Save and quit | :wq |
Quit without saving | :q! |
Enter insert mode | i , a , o |
Enter normal mode | Esc |
Move by word | w (forward), b (backward) |
Jump to start/end of line | 0 (start), $ (end) |
Yank (copy) | yy (line), yw (word) |
Paste | p (after), P (before) |
Undo/Redo | u (undo), Ctrl+r (redo) |
Open a file | :e filename |
Split window horizontally/vertically | :split / :vsplit |
Fuzzy file search (Telescope) | <leader>f |
Toggle file tree | <leader>e |
By mastering these commands and keybindings, you'll start to navigate and edit code in NeoVim far more effectively!
NeoVim includes a built-in terminal emulator, which allows you to run a terminal session directly within your editor. This is a powerful feature for staying inside the NeoVim environment while performing tasks like running scripts, compiling code, or interacting with your system shell.
To open a terminal in NeoVim, run the following command in normal mode (press Esc
to make sure you're in normal mode):
:term
This will open a terminal window in the current buffer.
When you're in the terminal, you’ll automatically be in Terminal Mode, which means the keybindings work as they would in a normal terminal shell.
- Normal Mode: To switch back to normal NeoVim mode from the terminal, press
Ctrl + \
, followed byCtrl + n
.
Once inside the terminal, you can run commands just as you would in your regular terminal:
- Type any shell command (like
ls
,git
,npm start
, etc.). - Use
Ctrl + d
to exit the terminal session.
You can split the NeoVim window and have the terminal in one section, while editing files in another. This helps to work with code and the terminal simultaneously.
- Split the terminal vertically:
:vsplit | term
- Split the terminal horizontally:
:split | term
After splitting windows or using a terminal buffer alongside other files, you can navigate between the windows (buffers) with:
- Ctrl + w w: Switch between windows.
- Ctrl + w + h/j/k/l: Move between windows in specific directions (
h
for left,l
for right,j
for down,k
for up).
To close a terminal, you can do the following:
- If you’re inside the terminal, exit the shell (e.g., type
exit
and hit Enter). - You can also close it from normal mode by closing the window:
:q
To make it easier to switch between terminal and normal mode, you can set up custom keybindings in your init.lua
or init.vim
configuration file:
-- Keybinding to quickly switch between terminal and normal mode
vim.api.nvim_set_keymap('t', '<Esc>', [[<C-\><C-n>]], { noremap = true, silent = true })
-- To quickly close a terminal from normal mode
vim.api.nvim_set_keymap('t', '<leader>q', [[<C-\><C-n>:q<CR>]], { noremap = true, silent = true })
This will let you press Esc
to exit Terminal mode and return to Normal mode.
-
Open a terminal in NeoVim:
:vsplit | term
-
Run your program inside the terminal (e.g.,
npm start
for a Node.js app). -
Use
Ctrl + w + w
to switch between your code and the terminal. -
Make edits in your code files in the other window, save the file with
:w
, and use the terminal to see the output or rerun the program.
Action | Command |
---|---|
Open a terminal | :term |
Switch from terminal to normal mode | Ctrl + \ followed by Ctrl + n |
Split window and open terminal | :split | term (horizontal) |
Split window and open terminal | :vsplit | term (vertical) |
Close terminal window | :q or exit |
Switch between terminal and other windows | Ctrl + w w |
This will allow you to fully utilize the terminal inside NeoVim, keeping your workflow efficient without leaving the editor.
Yes, there is a way to reverse the split behavior in NeoVim so that the terminal opens below instead of on top when you use :split
.
By default, :split
opens a new window above the current one. To reverse this behavior and make the new split open below the current window, you can use the :belowright
command.
To open the terminal below the current window, use:
:belowright split | term
belowright
: Forces the new split to open below the current window.split
: Opens a new split window.| term
: Executes the terminal command after the split.
You can also set this behavior for future splits using a keybinding if you prefer to always open the terminal below:
You can add this keybinding to your init.lua
or init.vim
:
vim.api.nvim_set_keymap('n', '<leader>t', ':belowright split | term<CR>', { noremap = true, silent = true })
Now, whenever you press <leader>t
, it will open the terminal in a split below your current window.
In NeoVim (and Vim), the <leader>
key is a configurable key that acts as a prefix for custom keybindings. It's used to define shortcuts for commands you frequently use, allowing you to personalize your workflow. The default value of <leader>
is typically the backslash (\
), but many users prefer to set it to a different key like the spacebar for easier access.
You can set the <leader>
key to something more convenient (like the spacebar) by adding this to your init.lua
or init.vim
configuration file:
vim.g.mapleader = " "
Now, whenever you see <leader>
, it will refer to the spacebar.
In the example I provided earlier, this line:
vim.api.nvim_set_keymap('n', '<leader>t', ':belowright split | term<CR>', { noremap = true, silent = true })
means that pressing the spacebar (<leader>
) followed by t
will run the command :belowright split | term
, which opens a terminal in a horizontal split below the current window.
- t:
<leader>
refers to the spacebar (if you configured it that way).t
is the key pressed after<leader>
, which runs the command.
You can use <leader>
to create custom keybindings for many actions. For example:
-
Saving the file:
vim.api.nvim_set_keymap('n', '<leader>w', ':w<CR>', { noremap = true, silent = true })
Pressing spacebar +
w
will save the file. -
Quitting NeoVim:
vim.api.nvim_set_keymap('n', '<leader>q', ':q<CR>', { noremap = true, silent = true })
Pressing spacebar +
q
will quit NeoVim.
<leader>
is a key that acts as a prefix for custom keybindings.- You can set it to any key (commonly spacebar) for easier access.
- Using
<leader>
in keybindings helps streamline your workflow by allowing you to define fast shortcuts for frequently used commands.
When you open a terminal with :split | term
, you are in Normal Mode, which is why the terminal isn't responsive right away. To enter Terminal Mode automatically after opening a terminal, you can adjust your command to:
:split | term | startinsert
This will do the following:
:split
: Opens a new horizontal split.:term
: Opens the terminal in that split.:startinsert
: Switches you to Insert Mode, which is how you interact with the terminal.
You can add a keybinding to make this process even smoother. Add this to your init.lua
or init.vim
:
vim.api.nvim_set_keymap('n', '<leader>t', ':belowright split | term | startinsert<CR>', { noremap = true, silent = true })
Now, whenever you press <leader>t
, it will open a terminal in a horizontal split below the current window and automatically switch to Terminal Mode, allowing you to start typing commands right away.
When you manually typed clea
, you were actually still in Normal Mode, where NeoVim interprets key presses as commands. The terminal wasn’t activated until you switched to Insert Mode (or pressed i
), which is what the terminal needs to interact with your shell. By using startinsert
, you ensure that NeoVim switches to Terminal Mode (or Insert Mode) automatically.
- Use
:split | term | startinsert
to open a terminal and automatically switch to insert mode. - You can also map this to a convenient keybinding like
<leader>t
to make the process faster.
:belowright split | exe 'term' | startinsert
This way, the term
command is executed as part of an Ex command, and then startinsert
is applied to enter Insert Mode.
If you’re using a keybinding, you can update it as follows:
The issue you're encountering occurs because the `|` (pipe) character in NeoVim is interpreted as chaining multiple Ex commands (NeoVim/Vim commands). However, you can't directly use `|` between certain commands in NeoVim like `term` and `startinsert`.
To fix this, you can use the `:exe` command to execute a sequence of commands more reliably. Here’s how you can modify the command:
```bash
:belowright split | exe 'term' | startinsert
This way, the term
command is executed as part of an Ex command, and then startinsert
is applied to enter Insert Mode.
If you’re using a keybinding, you can update it as follows:
vim.api.nvim_set_keymap('n', '<leader>t', ':belowright split | exe "term" | startinsert<CR>', { noremap = true, silent = true })
- :belowright split: Opens a split below the current window.
- :exe 'term': Executes the
term
command in a way that works with chaining. - startinsert: Switches to Insert Mode, enabling interaction with the terminal.
Now you should be able to open a terminal in a split and have it activate correctly without errors.
Let me know if this solves the issue!```
- :belowright split: Opens a split below the current window.
- :exe 'term': Executes the
term
command in a way that works with chaining. - startinsert: Switches to Insert Mode, enabling interaction with the terminal.
Now you should be able to open a terminal in a split and have it activate correctly without errors.