What it is
Neovim is a highly configurable, extensible, and modern text editor designed for efficient coding and text manipulation, often used as a powerful IDE replacement.
Installation
Linux
# Using package manager (e.g., apt for Debian/Ubuntu)
sudo apt update
sudo apt install neovim
# Using Homebrew (if installed)
brew install neovim
# Building from source (recommended for latest features)
git clone https://github.com/neovim/neovim.git
cd neovim
git checkout stable # Or master for development version
make CMAKE_BUILD_TYPE=Release
sudo make install
macOS
# Using Homebrew (recommended)
brew install neovim
# Building from source
git clone https://github.com/neovim/neovim.git
cd neovim
git checkout stable # Or master for development version
make CMAKE_BUILD_TYPE=Release
sudo make install
Windows
# Using Scoop (recommended)
scoop install neovim
# Using Chocolatey
choco install neovim
# Building from source (requires CMake and potentially MSVC)
# Refer to Neovim's official build instructions for detailed steps.
Core Concepts
- Modes: Neovim operates in different modes. The most common are:
- Normal Mode: The default mode. Used for navigation, deletion, copying, pasting, and issuing commands. Keystrokes are interpreted as commands.
- Insert Mode: Used for typing text. You enter Insert Mode from Normal Mode by pressing
i,a,o, etc. PressEscto return to Normal Mode. - Visual Mode: Used for selecting blocks of text. Enter with
v(character-wise),V(line-wise), orCtrl-v(block-wise). Commands in Normal Mode can be applied to the selection. - Command-line Mode: Used for entering Ex commands (commands starting with
:). You enter this mode by pressing:in Normal Mode. The command is typed at the bottom of the screen. PressEnterto execute.
- Buffers: An in-memory representation of a file. When you open a file, Neovim creates a buffer for it. You can have multiple buffers open simultaneously.
- Windows: A viewport onto a buffer. You can split your editor window into multiple windows, each displaying a different buffer or the same buffer at a different location.
- Tabs: A collection of windows. Often used to group related sets of windows for a specific task.
- Configuration: Neovim is configured using Lua (primarily
init.lua) or Vimscript (init.vim). The main configuration file is typically located at~/.config/nvim/init.luaor~/.config/nvim/init.vim. - Plugins: Neovim’s functionality can be extended with plugins, managed by plugin managers like
packer.nvim,vim-plug, orlazy.nvim.
Commands / Usage
Basic Navigation (Normal Mode)
h,j,k,l: Move cursor left, down, up, right.w,b: Move cursor forward/backward to the start of the next/previous word.e,ge: Move cursor to the end of the next/previous word.0,^,$: Move to the beginning of the line (first char), beginning of the line (first non-whitespace), end of the line.gg: Move to the first line of the buffer.G: Move to the last line of the buffer.{,}: Move to the previous/next paragraph.Ctrl-f,Ctrl-b: Scroll down/up one full screen.Ctrl-d,Ctrl-u: Scroll down/up half a screen.zt,zz,zb: Redraw screen with current line at top, middle, bottom.{line_number}ggor{line_number}G: Go to a specific line number (e.g.,10Ggoes to line 10).f{char}: Move forward to the next occurrence of{char}on the current line.F{char}: Move backward to the previous occurrence of{char}on the current line.t{char}: Move forward to the character before the next occurrence of{char}on the current line.T{char}: Move backward to the character after the previous occurrence of{char}on the current line.;: Repeat the lastf,F,t, orTsearch.,: Repeat the lastf,F,t, orTsearch in the opposite direction.
Entering Other Modes (from Normal Mode)
i: Enter Insert Mode before the cursor.a: Enter Insert Mode after the cursor.I: Enter Insert Mode at the beginning of the line.A: Enter Insert Mode at the end of the line.o: Enter Insert Mode on a new line below the current line.O: Enter Insert Mode on a new line above the current line.s: Delete character under cursor and enter Insert Mode.S: Delete the whole line and enter Insert Mode.v: Enter Visual Mode (character-wise).V: Enter Visual Mode (line-wise).Ctrl-v: Enter Visual Mode (block-wise).:: Enter Command-line Mode./: Start searching forward.?: Start searching backward.n: Repeat last search in the same direction.N: Repeat last search in the opposite direction.
Editing Text (Normal Mode)
x: Delete character under the cursor.X: Delete character before the cursor.dd: Delete the current line.d{motion}: Delete text according to{motion}(e.g.,dwdeletes to the next word,d$deletes to the end of the line).yy: Yank (copy) the current line.y{motion}: Yank text according to{motion}.p: Put (paste) yanked or deleted text after the cursor.P: Put (paste) yanked or deleted text before the cursor.c{motion}: Change text according to{motion}(deletes and enters Insert Mode).s: Change character under cursor (deletes and enters Insert Mode).r{char}: Replace character under cursor with{char}.R: Enter Replace Mode (overtype characters).u: Undo last change.Ctrl-r: Redo last undone change..: Repeat the last change.J: Join the current line with the next line.~: Toggle case of character under cursor.
Operators and Motions
Many commands are formed by an operator followed by a motion.
- Operators:
d(delete),y(yank),c(change),gU(uppercase),gu(lowercase),>(indent),<(unindent),!(filter). - Motions:
w,b,e,$,^,gg,G,f{char},t{char},{line_number},Ctrl-f, etc.
Examples:
d2w: Delete the next two words.y$: Yank from the cursor to the end of the line.c3j: Change the current line and the two lines below it (deletes and enters Insert Mode).>G: Indent the entire file.!G: Filter the entire file through an external command (e.g.,!sort).
Text Objects (Normal Mode)
These allow you to operate on semantic blocks of text. Use an operator followed by a text object.
iw: Inner word (e.g.,diwdeletes the inner word).aw: A word (including trailing space).i(ori): Inner parentheses block (e.g.,ci(changes inside the parentheses).a(ora): A parentheses block (including the parentheses).i{ori}: Inner curly braces block.a{ora}: A curly braces block.i[ori]: Inner square brackets block.a[ora]: A square brackets block.i": Inner double quotes.a": A double quotes block.i': Inner single quotes.a': A single quotes block.it: Inner tag block (for HTML/XML).at: An tag block.
Examples:
ci": Change inside the nearest double quotes.yap: Yank a paragraph.dap: Delete a paragraph.
Buffers Management (Command-line Mode)
:ls: List all open buffers.:b {buffer_number}: Switch to buffer{buffer_number}(e.g.,:b 2).:b {buffer_name}: Switch to buffer with name{buffer_name}(e.g.,:b my_file.txt).:bn: Switch to the next buffer.:bp: Switch to the previous buffer.:bd: Delete (close) the current buffer.:e {filename}: Edit a new file (creates a new buffer if file doesn’t exist).:wa: Write (save) all modified buffers.:w: Write (save) the current buffer.:q: Quit Neovim (if no modified buffers).:q!: Quit Neovim, discarding all changes.:wqor:x: Write and quit.
Window Management (Normal Mode & Command-line)
Ctrl-w sor:split: Split window horizontally.Ctrl-w vor:vsplit: Split window vertically.Ctrl-w h,Ctrl-w j,Ctrl-w k,Ctrl-w l: Move cursor to the left, down, up, right window.Ctrl-w w: Move to the next window (cycles through).Ctrl-w qor:quit: Close the current window.Ctrl-w c: Close the current window (same asCtrl-w q).Ctrl-w =: Make all windows equal height and width.Ctrl-w +,Ctrl-w -: Increase/decrease window height.Ctrl-w >,Ctrl-w <: Increase/decrease window width.Ctrl-w r: Swap current window with the next one.:tabnew: Open a new tab.Ctrl-w T: Move the current window to a new tab.gt,:tabnext: Go to the next tab.gT,:tabprev: Go to the previous tab.:tabclose: Close the current tab.
Search and Replace (Command-line Mode)
/{pattern}: Search forward for{pattern}.?{pattern}: Search backward for{pattern}.:{range}s/{pattern}/{replacement}/[flags]: Substitute{pattern}with{replacement}in the specified{range}.{range}:.(current line),$(last line),%(entire file),1,10(lines 1 to 10),'<,'>(visual selection).[flags]:g(global, replace all occurrences on the line),c(confirm each replacement),i(case-insensitive).
:%s/foo/bar/g: Replace all occurrences of "foo" with "bar" in the entire file.:%s/foo/bar/gc: Replace all occurrences of "foo" with "bar" in the entire file, confirming each.:10,20s/foo/bar/g: Replace all occurrences of "foo" with "bar" on lines 10 through 20.:'<,'>s/foo/bar/g: Replace all occurrences of "foo" with "bar" within the visual selection.
Macros (Normal Mode)
q{register}: Start recording a macro into register{register}(e.g.,qarecords into register 'a').q: Stop recording a macro.@{register}: Play back the macro in{register}(e.g.,@a).@@: Repeat the last executed macro.{count}@{register}: Play back the macro{count}times (e.g.,10@a).
Configuration & Settings (Command-line Mode)
:set {option}: Enable an option (e.g.,:set number).:set no{option}: Disable an option (e.g.,:set nonumber).:set {option}={value}: Set an option to a value (e.g.,:set tabstop=4).:set?: Show the value of the current option.:set: Show all options that are set.:syntax enableor:syntax on: Enable syntax highlighting.:filetype plugin indent on: Enable filetype detection, plugins, and indentation.:set hlsearch: Highlight all search matches.:set incsearch: Show search matches incrementally as you type.:set nuor:set number: Show line numbers.:set relativenumber: Show relative line numbers.:set autoindent: Automatically indent new lines.:set smartindent: Smarter auto-indentation.:set expandtab: Use spaces instead of tabs.:set tabstop=4: Set the width of a tab character.:set shiftwidth=4: Set the width for indentation commands.:set cursorline: Highlight the current line.:set wrap/:set nowrap: Enable/disable line wrapping.
External Commands (Command-line Mode)
:!{command}: Execute an external shell command. Output is displayed afterward. (e.g.,:!ls -l).:[range]!{command}: Filter the lines in{range}through the external command. (e.g.,:%!sort).
Marks (Normal Mode)
m{a-z}: Set a lowercase mark{a-z}at the current cursor position.m{A-Z}: Set an uppercase mark{A-Z}(marks across files).`{mark}`: Move cursor to the position of{mark}.`'`: Move cursor to the start of the line where the last change was made.:.: Move cursor to the current line.:{mark}: Move cursor to the start of the line containing{mark}.:``: Go back to where you were before the last jump.:.`` `: Go forward to where you were after the last jump.
Autocompletion (Insert Mode)
Ctrl-n: Complete using the keyword below the cursor.Ctrl-p: Complete using the keyword above the cursor.Ctrl-x Ctrl-n: Whole word completion (buffers).Ctrl-x Ctrl-f: File name completion.Ctrl-x Ctrl-o: Omni completion (language-specific, requires configuration).Ctrl-x Ctrl-s: Spelling suggestion.
Common Patterns
-
Opening a file and jumping to a line:
nvim +123 path/to/your/file.txtOpens
file.txtand places the cursor on line 123. -
Saving and quitting:
:wqor
ZZ(in Normal Mode)
-
Finding and replacing in the whole file:
:%s/old_text/new_text/gcReplace "old_text" with "new_text" globally and confirm each change.
-
Running a command on a visual selection: Select text in Visual Mode, then type:
:!sortThis will pipe the selected text to the
sortcommand and replace the selection with the output. -
Indenting code:
vip> >Select inner paragraph (
vip), then indent (>) it. (Repeat>to indent further). Or, select lines in Visual Mode and press>. -
Copying a line to the system clipboard: Requires Neovim compiled with clipboard support (
+clipboard) and a clipboard tool (xclip,wl-clipboard,pbcopy)."+yyYank the current line into the system clipboard register (
+)."+pPaste from the system clipboard register (
+). -
Reformatting code (e.g., with
prettier)::%!prettier --stdin-filepath %Pipe the entire buffer (
%) toprettier, using the current filename (%) for context. -
Opening a terminal within Neovim:
:terminalOpens a horizontal terminal. Use
Ctrl-\ Ctrl-nto exit terminal mode and return to Normal Mode. -
Navigating between quickfix/location lists:
:copen " Open quickfix window :lnopen " Open location window :cnext " Go to next error :cprev " Go to previous error :lnext " Go to next item :lprev " Go to previous item -
Configuring with Lua: Create
~/.config/nvim/init.luaand add Lua code like:vim.opt.number = true vim.opt.relativenumber = true vim.opt.tabstop = 4 vim.opt.expandtab = true -- Example of mapping a key vim.keymap.set('n', '<leader>ff', '<cmd>Telescope find_files<cr>')
Gotchas
- Mode Switching: Forgetting you’re not in Normal Mode is the most common source of errors. Always be mindful of your current mode.
Escis your friend. - Undo Tree: Neovim has a powerful undo tree.
uundoes,Ctrl-rredoes. You can visually navigate undo history with plugins or:earlier {N}sand:later {N}s. - Registers: Yanked/deleted text goes into registers.
"followed by a register name selects it (e.g.,"ayyyanks into registera,ppastes from the default register). Unnamed register ("") is used by defaultyandp. Black hole register (_) discards yanked/deleted text ("_dd). System clipboard is+and*. - Plugins and Configuration: Neovim’s power comes from plugins and configuration. A bare Neovim installation is very basic. Setting up a plugin manager (like
lazy.nvim,packer.nvim,vim-plug) and configuring it correctly is essential for a productive workflow. ~/.config/nvim: This is the standard location for Neovim configuration. Ensure yourinit.luaorinit.vimis placed here.- Autocommands: Neovim uses autocommands (
autocmd) to trigger actions based on events (e.g.,BufWritePre,FileType). Understanding these is key for advanced customization. - Clipboard Integration: Ensure Neovim was compiled with clipboard support (
nvim --version | grep clipboard) and that you have the necessary system tools (xclip,wl-clipboard,pbcopy). Use the+register for system clipboard. - Terminal Mode (
:terminal): When inside a terminal buffer,i,a, etc., behave differently. UseCtrl-\ Ctrl-nto exit terminal mode and return to Normal Mode.