Configuring Vim
Vim’s defaults are old, dating back to a time when terminals and expectations were different. A handful of settings fix the most common rough edges without turning this into a whole side project.
Where the config file lives
For classic Vim, it’s ~/.vimrc. For Neovim, it’s
~/.config/nvim/init.vim (or init.lua, if you’re using Neovim’s Lua
config system). If the file doesn’t exist yet, create it. Vim reads it
automatically on startup.
Neovim note: Neovim also supports configuration in Lua
(init.lua), which is more common in newer setups and plugin
ecosystems. Everything in this chapter uses Vimscript, which works in
both Vim and Neovim, so you’re not locked out of anything by starting
here.
A sane starting point
Open your config file and add these lines:
set number
set relativenumber
set hlsearch
set incsearch
set ignorecase
set smartcase
set expandtab
set shiftwidth=2
set tabstop=2
syntax on
Here’s what each one does:
numbershows line numbers down the left siderelativenumbershows how many lines away each other line is from your cursor, which makes counts like5jeasier to planhlsearchhighlights all matches when you searchincsearchhighlights matches as you type your search, before you even press Enterignorecasemakes searches case-insensitive by defaultsmartcaseoverridesignorecaseif your search includes an uppercase letter, so/Foostill matches exactly and/foomatches anythingexpandtabinserts spaces instead of literal tab charactersshiftwidthandtabstopset how many spaces count as one indent level (2 here, change it to whatever your projects use)syntax onturns on color highlighting for code
Save the file and restart Vim (or run :source ~/.vimrc to reload it
without restarting) to see the changes take effect.
Growing this over time
You don’t need to figure out the perfect config today. Add one setting
when something bothers you enough to look up the fix. A .vimrc built up
gradually, one annoyance at a time, tends to fit how you actually work
better than one copied wholesale from someone else’s dotfiles.
Try it
- Create your config file if it doesn’t exist yet
(
~/.vimrcfor Vim,~/.config/nvim/init.vimfor Neovim). - Add the settings block above.
- Restart Vim and confirm line numbers appear on the left.
- Search for something with
/and confirm matches are highlighted. - Search for a capitalized word and an all-lowercase word, and notice the
difference in how
smartcasetreats each one.