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.
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.
Warm-up from earlier chapters
Two from earlier chapters, from memory, before the new idea. Two wrong tries show the answer.
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.
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.
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.
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.
The first exercise of every lesson is free. Pro unlocks the rest, every track, for good.
See ProThis exercise needs a keyboard. Read on, and play it at your computer.
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.
The first exercise of every lesson is free. Pro unlocks the rest, every track, for good.
See ProThis 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.
Space, semicolon
With that mapping on for this exercise: ␣ ; on each line, j to the next.
The first exercise of every lesson is free. Pro unlocks the rest, every track, for good.
See ProThis exercise needs a keyboard. Read on, and play it at your computer.
Your dojo, from memory
With the two mappings from this lesson on, press the keys.