Vim and Neovim
Created: January 03, 2018 (Updated: August 12, 2020)
Sessions
| Description |
Command |
| Save a session |
:mksession PATH.vim |
| Open a session |
:source PATH.vim |
| Open a session before Vim starts. |
vi -S PATH.vim |
Run Terminal Commands in Vim
:! COMMAND
Spaces Rather Than Tabs
| Description |
Command |
| Enable spaces rather than tabs |
set expandtab |
| INT spaces for one tab |
set tabstop=INT |
| Replace existing tabs |
retab |
| INT spaces for indentation |
set shiftwidth=INT |
| One-liner for the previous options |
set tabstop=INT shiftwidth=INT expandtab |
User-Defined Commands
:command COMMAND_NAME COMMAND_TO_EXECUTE
COMMAND_NAME must start with an uppercase letter.
- Must be in command mode (
:) to use the command.
- Can be added to
init.vim or .vimrc without the leading colon for
persistence.
Line Numbers
| Description |
Command |
| Turn on line numbers |
set number |
| Turn off line numbers |
:set nonumber |
| Toggle line numbers |
:set nu! |
number can be shortened to nu.
nonumber can be shortened to nonu.
File Explorer
| Description |
Command |
| Open the file explorer window |
:Explore |
| Explore and open in a new tab |
:Texplore |
| Explore and vertical split |
:Vexplore |
| Explore and horizontal split |
:Hexplore |
- Can be shortened to
Ex, Tex, etc.
System Clipboard
| Description |
Command |
| Copy the current line |
` “ + y MOTION` |
| Paste |
" + p |
Macros
| Description |
Command |
| Record a macro. LETTER: [a-zA-Z]. |
q LETTER COMMANDS q |
| Execute a macro |
[multiplier]@<letter> |
Marks
| Description |
Command |
| Set a mark |
m LETTER |
| Jump to the mark |
{APOSTROPHE | GRAVE} LETTER |
Jump to a Column
COLUMN_NUMBER|
- Will not work on blank lines.
Tabs
| Description |
Command |
| Open multiple files in separate tabs |
vi -p FIRST [SECOND] [...] |
| New tab |
:tabnew |
| Next tab |
:tabn or gt |
| Previous tab |
:tabp or gT |
Close a tab; optionally close tab i if specified |
:tabclose [i] |
| List all tabs |
:tabs |
Move current tab to position i |
:tabm i |
| Copy the current window to a new tab |
:tab split |
| Close all other tabs |
:tabonly |
| Close all tabs without saving |
:qa |
| Close and close all tabs |
:wqa |
Windows
| Description |
Command |
| Open multiple files in separate windows |
vi -o {FIRST} [SECOND] [...] |
| Vertical split window |
^w v or :vsp |
| Horizontal split window |
^w s or :sp |
| New window with no contents |
^w n |
| Next window |
^w w |
| Move between windows |
^w MOVEMENT_KEY |
| Increase or decrease window width |
^w {<|>} |
| Increase or decrease window height |
^w {+|-} |
| Reset window sizes |
^w = |
Find and Replace (Substitute)
| Description |
Command |
| Current line only |
:s/FIND/REPLACE_WITH/g |
| Every line |
:%s/FIND/REPLACE_WITH/g |
| Every line with confirmation |
:%s/FIND/REPLACE_WITH/gc |
Between START_LINE and END_LINE |
:START_LINE,END_LINEs/FIND/REPLACE_WITH/g |
Vim Less-like Syntax Highlighting
- Vim and Neovim come with a script that can make the editor act like
less.
- Unlike
less, this script does syntax highlighting.
- Path:
/usr/share/nvim/runtime/macros/less.sh
- Preferred symlinks:
sudo ln -s /usr/share/nvim/runtime/macros/less.sh /usr/bin/vimless
sudo ln -s /usr/share/nvim/runtime/macros/less.sh /usr/bin/vless
sudo ln -s /usr/share/nvim/runtime/macros/less.sh /usr/bin/vess
- Vim will throw a “Cannot make changes, ‘Modifiable’ is off” error if the insert button is pressed in Vim less mode. In order to override this:
:set modifiable
:set ma # Short version
Highlight Column 80
:set textwidth=80
:set colorcolumn=+1
Styling:
:hi ColorColumn guibg=#2d2d2d ctermbg=246
Highlight Search Matches
| Description |
Command |
| Turn on |
:set hlsearch |
| Turn off |
:nohlsearch |
| Toggle |
:set hls! |
vimrc/init.vm
- For Vi/Vim the user-specific configuration file is:
- For Neovim, the user-specific configuration file is:
- My init.vim.
Enable Arrow and Backspace Keys in Insert Mode
In init.vim or .vimrc:
set nocompatible
set backspace=2
Enable Mouse Mode
Reload Files
Discard changes and reload the file:
Automatic discard and reload:
:set autoread # doesn't work?