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:

  • number shows line numbers down the left side
  • relativenumber shows how many lines away each other line is from your cursor, which makes counts like 5j easier to plan
  • hlsearch highlights all matches when you search
  • incsearch highlights matches as you type your search, before you even press Enter
  • ignorecase makes searches case-insensitive by default
  • smartcase overrides ignorecase if your search includes an uppercase letter, so /Foo still matches exactly and /foo matches anything
  • expandtab inserts spaces instead of literal tab characters
  • shiftwidth and tabstop set how many spaces count as one indent level (2 here, change it to whatever your projects use)
  • syntax on turns 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

  1. Create your config file if it doesn’t exist yet (~/.vimrc for Vim, ~/.config/nvim/init.vim for Neovim).
  2. Add the settings block above.
  3. Restart Vim and confirm line numbers appear on the left.
  4. Search for something with / and confirm matches are highlighted.
  5. Search for a capitalized word and an all-lowercase word, and notice the difference in how smartcase treats each one.

← Files and Buffers