Learn Practice Daily golf Reference
Plugins & your editor·Lesson 4 of 4·9 min·0/6 exercisesPro

Your first Neovim config

The season finale: a config of your own, init.lua with a few options that matter, a j k mapping you write yourself, Space as your leader key, and the result to copy into your editor.

Scene

Tuesday, 11:30 a.m. The dojo you trained in all season came set up: relative numbers on, indenting and a wrap width chosen, a few lines kept above and below the cursor, and surround and gc built in. What you learned there is the shared core, the grammar every Vim speaks, on every server and every colleague's machine. Your own editor can be your own dojo, set up the way you train.

Today you write the first lines of a config. Not someone else's thousand-line one: yours.

From memory

Warm-up from earlier chapters

Two from earlier chapters, from memory, before the new idea. Two wrong tries show the answer.

Highlight whole lines.
press the keys…
1 of 2
Highlight whole lines.

The rules on the wall

A config is a file the editor reads every time it starts. For Neovim it's ~/.config/nvim/init.lua, written in Lua; for Vim it's ~/.vimrc. Options go in as vim.opt.name = value: vim.opt.relativenumber = true turns on the gutter's ruler from track 1, and vim.opt.ignorecase = true with vim.opt.smartcase = true makes searches ignore case until you type a capital.

Try it

Two lines of your own

Add vim.opt.relativenumber = true below the number line, and vim.opt.smartcase = true at the end of the file.

Fix the marked spots · 2 left
1-- ~/.config/nvim/init.lua
2vim.opt.number = true
+vim.opt.relativenumber = true
1vim.opt.ignorecase = true
+vim.opt.smartcase = true
NORMALLoading the editor…0.0s2:1

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

A handful worth having

A short list most people end up with: number and relativenumber (where am I, how far is it), ignorecase and smartcase, clipboard = "unnamedplus" (yanks and puts use the system clipboard), expandtab, shiftwidth = 2 and tabstop = 2 (spaces, two at a time), undofile = true (undo survives closing the file), scrolloff = 8 (always some lines above and below the cursor). Add each one when you miss it, not before.

Your own shortcuts

Mappings go in with vim.keymap.set(mode, keys, what). The most popular first one: vim.keymap.set("i", "jk", "<Esc>") leaves Insert mode when you type j k quickly, without reaching for Esc. "i" is the mode (Insert), "jk" what you type, "<Esc>" what it stands for.

Try it

Write the mapping

Your config so far is below. Add the mapping as its last line, exactly: vim.keymap.set("i", "jk", "<Esc>"). Type the <Esc> in it as five characters.

Pro
The rest of this lesson is in Pro

The first exercise of every lesson is free. Pro unlocks the rest, every track, for good.

See Pro

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

Try it

jk to leave

The line you just wrote is on for this exercise. Each line gets a ; at the end: A, ;, then j k to leave Insert mode.

Pro
The rest of this lesson is in Pro

The first exercise of every lesson is free. Pro unlocks the rest, every track, for good.

See Pro

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

A key that starts your commands

The leader is a key that starts commands of your own. Space is a popular choice, because in Normal mode it only moves one character right: vim.g.mapleader = " " at the top of the file, then vim.keymap.set("n", "<leader>;", "A;<Esc>") makes ␣ ; add a semicolon to the end of the line, wherever the cursor is.

Leader mappings stay out of Vim's way, not out of each other's: two of yours, or a plugin's and yours, can both want <leader>f, and the last one set wins. :verbose nmap <leader> lists what's taken and where it was set.

Try it

Space, semicolon

With that mapping on for this exercise: ␣ ; on each line, j to the next.

Pro
The rest of this lesson is in Pro

The first exercise of every lesson is free. Pro unlocks the rest, every track, for good.

See Pro

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

Your config, so farFinish the exercise above to see it.
From memory

Your dojo, from memory

With the two mappings from this lesson on, press the keys.

Leave Insert mode the new way.
scroll here, then press the keys2 keys
1 of 2
Leave Insert mode the new way.