Learn Practice Daily golf Reference

How to make Vim settings permanent (your vimrc)

:set lasts until you quit. To keep a setting, write it in your config file: set number in ~/.vimrc for Vim, vim.opt.number = true in ~/.config/nvim/init.lua for Neovim. Once the file exists, :e $MYVIMRC and ↵ opens it.

The ninja chiselling fresh lines into a stone tablet headed ~/.vimrc, while behind it a chalked :set number on a small blackboard is being wiped half away by a sponge.

You switched on line numbers yesterday with :set number, and they were just what you wanted. This morning you opened Vim and they'd gone. Nothing broke. A :set changes the Vim you're typing in, and only until you quit. Every new Vim starts again from its defaults.

What Vim does at every start is run one file of yours, your config, called the vimrc. Each line in it is a command, as if you'd typed it after a colon, so a line that says set number turns the numbers on every time Vim opens.

Where your vimrc is

Vim looks for these in your home folder, reads the first one it finds, and ignores the rest:

  • ~/.vimrc
  • ~/.vim/vimrc
  • ~/.config/vim/vimrc, in recent versions, and only when neither of the others exists

On Windows the file is $HOME/_vimrc or $HOME/vimfiles/vimrc. Neovim reads ~/.config/nvim/init.vim or ~/.config/nvim/init.lua (on Windows, the same names in ~/AppData/Local/nvim/). Keep one or the other. With both, Neovim says E5422: Conflicting configs and loads only the Lua one.

You don't have to remember which. Vim keeps the path of the file it loaded in $MYVIMRC, so :echo $MYVIMRC prints it and :e $MYVIMRC opens it for editing, in Vim and Neovim alike.

Practise the keys

Find it and open it

Type each command as you would in Vim, Enter included. Nothing here opens a real file.

Which keys do it?
Find out which config file Vim loaded when it started.
scroll here, then press it
1 of 2
Find out which config file Vim loaded when it started.

No vimrc yet? Make one

If :echo $MYVIMRC prints an empty line, Vim found no config, so you make the file by its real path. In Vim, type :e ~/.vimrc, add your lines, and save with :w. In Neovim, type :e ~/.config/nvim/init.lua. The nvim folder may not exist yet, and then a plain :w fails with E212, so save the first time with :w ++p, which makes the folder too. From the next start on, $MYVIMRC points at your file.

In Vim, the moment a ~/.vimrc exists a few of its friendlier defaults switch off, syntax colours among them. Turn on syntax highlighting has the lines that bring them back.

What to put in it first

Start with a few lines you understand rather than someone's whole file. These fix what most people notice in their first week:

  • set number relativenumber numbers your line and shows how far away every other line is. Line numbers explains the pair.
  • set tabstop=4 shiftwidth=4 expandtab indents with four spaces. Tab size has the two-space and real-tab versions.
  • set ignorecase smartcase makes a search ignore case until you type a capital letter.
  • set hlsearch incsearch lights up matches while you type a search and keeps them lit afterwards. :noh turns them off until your next search.
  • inoremap jk <Esc> lets you leave Insert mode by typing j then k quickly.

A line starting with " is a comment, which is a good place to note why a line is there. The vimrc is a text file like any other, so you write it with the same keys you edit everything else with.

Try it here

Add the search lines

Your vimrc already has the numbers and the indents. The two search lines are missing.

o opens a new line below the cursor and starts typing there. Type set ignorecase smartcase, then press Esc.vimrc
2" ~/.vimrc
1set number relativenumber
3set tabstop=4 shiftwidth=4 expandtab
NORMALLoading the editor…0.0sLn 3, Col :1
Open this text in the playground

This editor needs a keyboard. Read on, and play it at your computer.

Load your changes without restarting

Saving the file changes nothing in the Vim you have open. It reads the vimrc when it starts, so either restart it or run the file yourself. :source % runs the file you're editing (% stands for its name), which makes it the thing to type right after :w in your vimrc. From any other file, :so $MYVIMRC does the same, :so being short for :source. Both work in Neovim, on init.lua too.

Reloading only adds settings. Delete the set hlsearch line, save, :source %, and the highlights stay on, because nothing in the file turns them off now. :set nohlsearch or a restart does that.

To see what a setting is right now, put a question mark after its name. :set hlsearch? answers hlsearch or nohlsearch, and :set tabstop? answers tabstop=4. When the answer isn't what your file says, :verbose set tabstop? also names the file and line that set it last, which is how you catch a plugin or a language's own settings changing yours.

Practise the keys

Reload and check

Type each command the way this page writes it, Enter included.

Which keys do it?
You've just saved your vimrc and you're still in it. Run it now, without restarting.
scroll here, then press it
1 of 4
You've just saved your vimrc and you're still in it. Run it now, without restarting.

Write the same settings in Neovim's init.lua

A Vim line such as set number is a Lua error in init.lua ('=' expected near 'number'), and Neovim then runs none of the file. Each option is an assignment instead, where on and off are true and false and a number is just the number:

  • vim.opt.number = true
  • vim.opt.tabstop = 4
  • vim.keymap.set("i", "jk", "<Esc>") for the j k mapping

To bring a Vim line across as it is, wrap it: vim.cmd("set ignorecase smartcase"). Or name the file init.vim instead, and every line from this page works there unchanged. Your Vim config, line by line puts the Vim and Lua spellings side by side and goes further into mappings and the leader key.

TypeWhat it does
:echo $MYVIMRC ↵Print the path of the config Vim loaded. Empty means it found none.
:e $MYVIMRC ↵Open that config to edit it.
:e ~/.vimrc ↵Start Vim's config the first time, then :w.
:e ~/.config/nvim/init.lua ↵Start Neovim's, then :w ++p to make the folder as well.
:source % ↵Run the file you are editing, so a saved change takes effect now.
:so $MYVIMRC ↵Run your config from any other file.
:set tabstop? ↵Show an option's current value.
:verbose set tabstop? ↵Show the value and the file and line that last set it.
:set hlsearch& ↵Put an option back to its default.
vim --clean / nvim --cleanStart without your config, to get back in when a bad line breaks it.