A Simple .vimrc for Vim Beginners
What $HOME and ~ mean, where Vim looks for your settings, and a small starter .vimrc with every line explained.
This post was originally published in Japanese on Qiita.
When you look up how to configure Vim, most explanations jump straight into terms like $HOME and ~/.vimrc, which can be hard to follow when you’re just starting out. This post starts from those basics.
Once you’re comfortable with Vim, retyping :set commands every time you start it gets tedious. That’s what .vimrc is for: a settings file Vim reads automatically on startup.
Vim reads two vimrc files on startup: a system vimrc shared by every user on the machine, and your own user vimrc. The one you edit is the user vimrc, the .vimrc file in your home directory, written as ~/.vimrc or $HOME/.vimrc.
Understanding $HOME and ~
First, let’s find your home directory, which is where .vimrc lives. Run this in Terminal:
echo $HOME
/Users/tetsuya
$HOME is an environment variable that holds the path to your home directory. The shell replaces it with that path before running the command, and echo prints what it receives.
$HOME vs. ~
~ is a shorthand the shell expands to your home directory, so $HOME and ~ point to the same place, and ~/.vimrc means “.vimrc in my home directory”. To check, cd to each and run pwd (print working directory):
cd $HOME
pwd
/Users/tetsuya
cd ~
pwd
/Users/tetsuya
Use whichever you like. Running cd with no arguments takes you home too.
Creating and editing .vimrc
From your home directory, open .vimrc:
vim .vimrc
If the file doesn’t exist yet, Vim creates it when you save. Files whose names start with a dot (.) are hidden files, and ls doesn’t list them by default. Use ls -a to see them.
When you’re done editing, press Esc to return to Normal mode, then type :wq to save and quit.
My .vimrc
Here are the settings I use. Feel free to copy them as-is.
" ##### Display #####
set number " Show line numbers
set title " Show the file name in the terminal's title bar
set showmatch " Briefly jump to the matching bracket when you type one
syntax on " Turn on syntax highlighting
set tabstop=2 " Display a tab character as 2 spaces wide
set smartindent " Indent new lines automatically, following the code's structure
" ##### Search #####
set ignorecase " Ignore case when searching
set smartcase " ...unless the search contains an uppercase letter
set wrapscan " Wrap around to the top after reaching the end
Comments in .vimrc
A line starting with " (a double quote) is a comment, and Vim ignores it. You can also put a comment at the end of a set line. A few commands, such as key mappings, treat a trailing " as part of the command, so keep comments on their own line there.
" This is a comment, so Vim ignores it
set number " A comment at the end of a line