Nvim

up:: Programming ⚐

LSP

LSP servers

Inspiration

Keybindings after reset

gd: Go to definition gR: Open references gh: Reference/implements/definition window K: Hover doc

vim.keymap.set("n", "M", "J") -- mnemonic: [M]erge
vim.keymap.set("n", "<leader>h", "K") -- mnemonic: [h]over

vim.keymap.set("o", "ar", "a]") -- [r]ectangular bracket
vim.keymap.set("o", "ac", "a}") -- [c]urly brace
vim.keymap.set("o", "am", "aW") -- [m]assive word
vim.keymap.set("o", "aq", 'a"') -- [q]uote
vim.keymap.set("o", "az", "a'") -- [z]ingle quote

Write filename

"%p

Resources

Unread

Regex

  • Non-greedy: \{-}

Vim as a language

  • Verbs are actions that can be performed on nouns. Eg:
    • d: delete
    • c: change
    • y: yank (copy)
    • v: visually select (V for line vs. character)
  • Modifiers are used before nouns to describe how you’ll do something. Examples:
    • i: inside
    • a: around
    • NUM: number
    • t: to, searches in line and stops before it
    • f: searches for a thing in line and lands on it
    • / find a string (literal or Regex)
  • Nouns are objects you do something to. Examples:
    • w: word
    • s: sentence
    • p: paragraph
    • t: tag
    • b: block

You can also use nouns as motions. They define the size of your jump. Examples:

# Delete two words
d2w
# Change inside sentence (delete current and enter insert mode)
cis
# Yank inside paragraph (copy paragraph you're in)
yip
# Delete to next period
dt.

Insert mode

i Leave with Esc

Moving

  • 0: Beginning of the line

  • $: End of the line

  • ^: First non-blank character of line

  • t: to, searches in line and stops before it

  • f: find, for a thing in line and lands on it

  • ,: Repeat last t/f jump

  • ;: Backwards repeat last jump

  • '' (two backticks): Go back to where you just were

  • ) move forward one sentence

  • } move forward one paragraph

  • Ctrl-i jump to previous

  • Ctrl-o jump back to where you were

Move to line number

  • :<NUM>: / <NUM>gg / <NUM>G

Searching

  • *: Search for other instances of word under cursor
  • Search forward with /
  • n to go to next instance
  • N to go to previous
  • gn to jump to next search find
    • cgn changes the next find, making it great for repetitions with .

Edit mode

  • dd - Delete line
  • gg - Go to top
  • G - Go to bottom
  • } - Cursor after block of code
    • { - Above block of code
  • u - Undo
  • o - open new line below current one
    • O - open new line above current one

Replacing and changing

  • r - replaces one character under the cursor
    • R - enter replace mode until Esc is pressed
  • c - changes characters and can be combined with motions (eg. cw)

Command mode

  • :q! - Quit without saving
  • :wq - Quit and save.
    • Quicker alias: ZZ
    • :w - Save
  • :!<command> - execute shell script, eg :!ls

Substitutions

# Change "foo" to "bar" on every line
:%s /foo/bar/g
 
# Change "foo" to "bar" on current line
:s /foo/bar/g
 

Completion

  • Type the start of a command, hit CTRL-D for completing the command

Set filetype

set ft=dos

Word objects

  • words: iw and aw

  • double quotes: i" and a"

  • tags: it and at

  • sentences: is and as

  • paragraphs: ip and ap

  • single quotes: i' and a'

  • back ticks: i and a

  • parenthesis: i( and a(

  • brackets: i[ and a[

  • braces: i{ and a{

Shortcuts

  • ctrl-g: Shows location in file

Macros

  • qa: Start recording a macro named “a”
  • q: Stop recording
  • @a: Play back macro
    • or, with my German keyboard: öa

Replay macro on a visual selection by executing :normal @a

.vimrc

A minimalist take on Vim - Mijndert Stuij:

syntax on                           # enable syntax highlighting
set tabstop=2                       # set 1 tab to 2 spaces
set shiftwidth=2                    # set an indent to 2 spaces
set expandtab                       # convert tabs to spaces
set ai                              # automatic indentation
set number                          # enable line numbers
set hlsearch                        # highlight search matches
set ruler                           # show current row and column position
set backspace=2                     # fix backspace behaviour 
set wildmenu                        # enable autocompletion
set path=$PWD/**                    # set search path
set wildmode=longest:list,full      # set autocompletion mode
set pastetoggle=<leader>p           # toggle paste mode using \p
set nuw=3                           # set left margin
set noswapfile                       # disable swapfiles
set clipboard=unnamed               # copy to system clipboard
highlight LineNr ctermfg=DarkGrey   # set linenumbers to grey
nnoremap <esc><esc> :noh<return>    # clear search results using esc-esc
colorscheme nord                    # set colorscheme to nord

Remap ESC to jk

inoremap jk <ESC>

Some other basic stuff

syntax on # highlight syntax
set number # show line numbers
set noswapfile # disable the swapfile
set hlsearch # highlight all results
set ignorecase # ignore case in search
set incsearch # show search results as you type

Usage with German keyboard

From https://www.reddit.com/r/vim/comments/ilsnw1/comment/g3wc6yd/?utm_source=share&utm_medium=web2x&context=3r

nnoremap ß `
nnoremap ö @
inoremap üü {}<left>
inoremap öö []<left>

Plugins

Remappings to try

" H to move to the first character in a line
noremap H ^
" L to move to the last character in a line
noremap L g_

Keymappings

  • Clear search highlights — leader nh
  • Increment number — leader +
  • Decrement number — leader -
  • Split vertically — leader sv
  • Split horizontally — leader sh
  • Make windows equal width — leader se
  • Close split window — leader sx
  • Toggle window maximised — leader sm
  • Change surrounding — cs"'
  • Closing bracket (() — b
  • Closing bracket ({) — B
  • Replace copied text — grw
  • Comment out line — gcc
  • Comment out 2 lines down — gc2j
  • Toggle tree — leader e
  • Telescope find files in current directory — leader ff
  • Telescope find string in current directory — leader fs
  • Telescope find string under cursor in current directory — leader fc
  • Snippets close suggestion — control e

Surround

  • Surround with character — ys + motion + charater

  • Surround word with double quotes — ysw"

  • Delete surround from word — ds"

  • Change surround from word — ds"'

Modifiers

ModifiermacOSNvim
controlC-
optionM- or A-
shiftS-
commandD-

Global search to quickfix

:Ggrep findstring
:copen

Folding

zf - create fold
za - toggle fold