Skip to main content

From NeoVim to LunarVim

Guangzhou, China

<!-- TOC -->

<!-- /TOC -->

LunarVim - an IDE layer for Neovim with sane defaults.

Installation

> Prerequisites: > > * Make sure you have installed the latest version of Neovim v0.9.0+. > * Have git, make, pip, python, npm, node and cargo installed on your system.

LV_BRANCH='release-1.3/neovim-0.9' bash \<(curl -s https://raw.githubusercontent.com/LunarVim/LunarVim/release-1.3/neovim-0.9/utils/installer/install.sh)

Optional:

sudo npm install -g neovim tree-sitter-cli
sudo pacman -S python-pynvim

Updating LunarVim

  • Inside LunarVim :LvimUpdate
  • Updating Plugins inside LunarVim :LvimSyncCorePlugins
  • From the command-line lvim +LvimUpdate +q

Post install

LunarVim uses icons from Nerd Fonts. If you don't want to use them set lvim.use_icons to false.

mkdir -p ~/.local/share/fonts
cd ~/.local/share/fonts && curl -fLO https://github.com/ryanoasis/nerd-fonts/raw/HEAD/patched-fonts/DroidSansMono/DroidSansMNerdFont-Regular.otf

After installing your font, you will have to refresh your font cache by doing fc-cache -f -v.

Adding lvim to PATH by adding an alias to .zshrc, .bashrc, etc.

# add lvim & cargo
export PATH="$HOME/.local/bin:$PATH"
export PATH="$HOME/.cargo/bin:$PATH"

Cheat Sheet

Keybinds

ExitingNavigating
:qClose fileh j k lArrow keys
:qaClose all files<C-U> / <C-D>Half-page up/down
:wSave<C-B> / <C-F>Page up/down
:wq / :xSave and close fileWords
ZZSave and quitb / wPrevious/next word
ZQQuit without checking changesge / ePrevious/next end of word
Exiting insert modeLine
Esc / <C-[>Exit insert mode0 (zero)Start of line
<C-C>Exit insert mode, and abort current command^Start of line (after whitespace)
Clipboard$End of line
xDelete characterCharacter
ddDelete line (Cut)fcGo forward to character c
yyYank line (Copy)FcGo backward to character c
pPasteDocument
PPaste beforeggFirst line
"*p / "+pPaste from system clipboardGLast line
"*y / "+yPaste to system clipboard:{number}Go to line {number}
vi(Select everything inside parenthesis{Jump to beginning of paragraph
va(Select everything including parenthesis}Jump to end of paragraph
ya(Copy everything including parenthesisctrl+dJump half page up
viwSelect entire wordctrl+uJump half page down
viWSelect entire word with included punctuationzzCenter view port
Find & Replace{number}GGo to line {number}
:%s/foo/bar/gReplace foo with bar in whole document{number}jGo down {number} lines
Editing{number}kGo up {number} lines
aAppendWindow
AAppend from end of linezzCenter this line
iInsert modeztTop this line
oInsert mode on next linezbBottom this line
OInsert mode on previous lineHMove to top of screen
sDelete char and insertMMove to middle of screen
SDelete line and insertLMove to bottom of screen
CDelete until end of line and insertSearch
rReplace one characternNext matching search pattern
REnter Replace modeNPrevious match
uUndo changes*Next whole word under cursor
<C-R>Redo changes#Previous whole word under cursor
ci(Delete everything within parenthesis and enter edit mode
ca"Delete everything including outer double-quotes and enter edit mode
Visual modeTab pages
vEnter visual mode:tabedit [file]Edit file in a new tab
<leader>+bbGo to previous tab
<leader>+bnGo to next tab
<C+h>Move cursor to the left window
<C+l>Move cursor to the right window
<C+j>Move cursor to the lower window
<C+k>Move cursor to the upper window
<C+>Toggle terminal on/off
VEnter visual line mode:tabfind [file]Open file if exists in new tab
<C-V>Enter visual block mode:tabcloseClose current tab
In visual mode:tabsList all tabs
d / xDelete selection:tabfirstGo to first tab
sReplace selection:tablastGo to last tab
yYank selection (Copy):tabnGo to next tab
:tabpGo to previous tab

Operators

Operators let you operate in a range of text (defined by motion). These are performed in normal mode:

  • d: Operator
  • w: Motion
Operators listExamplesCombine operators with motions to use them.
dDeletedd(repeat the letter) Delete current line
yYank (copy)dwDelete to next word
cChange (delete then insert)dbDelete to beginning of word
\>Indent right2ddDelete 2 lines
\<Indent leftdipDelete a text object (inside paragraph)
=Autoindent(in visual mode) dDelete selection
uUndod2wDelete two words.
ctrl+rRedu
g~Swap case
gUUppercase
guLowercase
!Filter through external program

Configuration

You can configure LunarVim by using the configuration file located in:

cat ~/.config/lvim/config.lua

-- Read the docs: https://www.lunarvim.org/docs/configuration
-- Video Tutorials: https://www.youtube.com/watch?v=sFA9kX-Ud_c&list=PLhoH5vyxr6QqGu0i7tt_XoVK9v-KvZ3m6
-- Forum: https://www.reddit.com/r/lunarvim/
-- Discord: https://discord.com/invite/Xb9B4N

For some reason the old default config was removed - I made a copy of it here config.example.lua. To keep this main file lean we can create custom config files in sub-directories, add all configuration there - maybe git-source them - and import those files into main:

mkdir -p ~/.config/lvim/lua/custom
lvim ~/.config/lvim/lua/custom/options.lua //add your config here

Now either use the require() or reload() (for hot-reloading on save) to add your custom configuration to ~/.config/lvim/config.lua:

reload('custom.options')

Custom Keybinds

--move selected line with K or J
vim.keymap.set("v", "K", ":m '\<-2\<cr\>gv=gv")
vim.keymap.set("v", "J", ":m '\>+1\<cr\>gv=gv")

--scroll with ctrl-d and ctrl-j while keeping centered
vim.keymap.set("n", "\<C-d\>", "\<C-d\>zz")
vim.keymap.set("n", "\<C-u\>", "\<C-u\>zz")

--find next with n and N while keeping centered
vim.keymap.set("n", "n", "nzzzv")
vim.keymap.set("n", "N", "Nzzzv")

--use system clipboard when copying with SPACE+y
--you may need to install xclip (X11) or wl-clipboard (wayland)
vim.keymap.set("n", "\<leader\>y", "\"+y")
vim.keymap.set("v", "\<leader\>y", "\"+y")
vim.keymap.set("n", "\<leader\>Y", "\"+Y")

Python

Now we can start adding configuration to our options file - for example for Python development, e.g. a Python Language Server:

lvim.builtin.treesitter.ensure_installed = {
"python",
}

Type in :Mason and verify that pyright has been installed (I had to first open an *.py for the installation to start and I was seeing error messages along the way.)

From NeoVim to LunarVim

To add auto-formating we can run :MasonInstall black and add the following configuration:

local formatters = require "lvim.lsp.null-ls.formatters"
formatters.setup { { name = "black" }}
lvim.format_on_save.enabled = true
lvim.format_on_save.pattern = { "*.py" }

For linting we can use flake8 that can be installed with :MasonInstall flake8 with the following configuration:

local linters = require "lvim.lsp.null-ls.linters"
linters.setup { { command = "flake8", args = { "--ignore=E203" }, filetypes = { "python" } } }

<!-- Further, there are two plugins that come recommended - one for switching virtual environments and an eye-candy one:

lvim.plugins = {
"ChristianChiarulli/swenv.nvim",
"stevearc/dressing.nvim",
}

require('swenv').setup({
post_set_venv = function()
vim.cmd("LspRestart")
end,
})

lvim.builtin.which_key.mappings["C"] = {
name = "Python",
c = { "\<cmd\>lua require('swenv.api').pick_venv()\<cr\>", "Choose Env" },
}

The two configuration below the plugin block make sure that the language server is restarted after switching to a new Python virtual environment and that typing SPACE + C opens a dialogue you can select your environment from.

> Hmmm - switching environments does not work for me - the dialogue does not open. I will have to check later what the issue is. -->

Restart LunarVim and you should see that everything is now loaded when you open a Python file and will start complaining :)

From NeoVim to LunarVim

You can auto-format your code with black using SPACE + l + f.

Other

:TSInstall json
:TSInstall javascript
:TSInstall typescript
:TSInstall tsx