neovim-tutorial

Catalog

Part 2: Neovim Setup

Part 4: VSCode Vim

Part 5: Lazyvim

Part 6: Chatgpt


Part 1: Vim Shortcuts

Tutorial Video Links: Youtube, Bilibili

Installation:

Open VScode, search Vim in Extensions, and click install. imge



In the following keymappings, n stands for NORMAL mode, i stands for INSERT mode, v stands for VISUAL mode.

Open and Close Files

Mode Shortcut Description
n :w Write(save) current buffer
n :q Close current buffer (would fail if you don't save first)
n :wq Save and close current buffer
n :q! Exit current buffer without saving
n :qa! Exit all open buffers without saving
n :wqa Save and exit all open buffers

Navigation

Mode Shortcut Description
n, v h Move left
n, v j Move down
n, v k Move up
n, v l Move right
n, v w One word forward
n, v b One word backward
n, v ^ Beginning of line
n, v $ End of line
n, v gg Beginning of file
n, v G End of file
n, v { One paragraph backward
n, v } One paragraph forward
n, v f + [char] Move to next occurence of [char] in current line (Covered in Part 2 video)
n, v F + [char] Move to prev occurence of [char] in current line (Covered in Part 2 video)
n, v Ctrl+u Move Up half a Page (Covered in Part 2 video)
n, v Ctrl+d Move Down half a Page (Covered in Part 2 video)
n, v Ctrl+b Move Up a Full Page (Covered in Part 2 video)
n, v Ctrl+f Move Down a Full Page (Covered in Part 2 video)
n :[num-of-line] + Enter Go to a specific line
n, v /[search-item] + Enter + n Find pattern and go to next match

Enter INSERT Mode from NORMAL Mode

Mode Shortcut Description
n i Insert before cursor
n a Append after cursor
n I Insert at the beginning of the line
n A Append at the end of the line
n o Insert to next line
n O Insert to previous line
n c + [Navigation] Delete from before the cursor to [Navigation] and insert. Examples are as follow
n c + w Delete from before the cursor to end of current word and insert
n c + i + w Delete current word and insert
n c + $ Delete from before the cursor to end of the line and insert
i <Esc> Go back to Normal Mode, remap to jk recommended

Enter INSERT Mode from NORMAL Mode

Mode Shortcut Description
n i Insert before cursor
n a Append after cursor
n I Insert at the beginning of the line
n A Append at the end of the line
n o Insert to next line
n O Insert to previous line
n c + [Navigation] Delete from before the cursor to [Navigation] and insert. Examples are as follow
n c + w Delete from before the cursor to end of current word and insert
n c + i + w Delete current word and insert
n c + $ Delete from before the cursor to end of the line and insert
i <Esc> Go back to Normal Mode, remap to jk recommended

Edit in NORMAL Mode

Mode Shortcut Description
n dd Delete(cut) current line
n d + [Number] + d Or [Number] + dd Delete(cut) following [Number] of lines
n d + [Navigation] Delete(cut) from before the cursor to [Navigation], similar to c + [Navigation] above
n yy Yank(copy) current line
n y + [Number] + y Or [number] + yy Yank(copy) following [Number] of lines
n y + [Navigation] Yank(copy) from before the cursor to [Navigation], similar to c + [Navigation] above
n p Paste from what you delete or yank
n x Delete(cut) the character under the cursor
n u Undo changes
n Ctrl+r Redo changes
n :%s/[foo]/[bar]/g Find each occurrence of [foo] (in all lines), and replace it with [bar]. More substitute commands here.

VISUAL mode shortcuts

Mode Shortcut Description
n v Enter Visual Character Mode
n V Enter Visual Line Mode
V-Line >; < Increase Indent; Decrease Indent
n Ctrl+v Enter Visual Block Mode
v <Esc> Exit Visual Mode, remap to jk recommended

Vim settings under VSCode

  1. Press Ctrl/Cmd+Shift+p in VSCode
  2. Find Preferences: Open User Settings (JSON), open settings.json
  3. Configure the file, all options are here

Part 2: Neovim

Tutorial Video Links: Youtube, Bilibili Speical Thank: My Neovim Tutorial won't be possible without Josean dev's amazing video

Install NeoVim, go to ~/.config/ directory, and create a nvim folder with the folder structure as following

├── init.lua
├── lua
   └── usr
       ├── core
       │   ├── colorscheme.lua
       │   ├── keymaps.lua
       │   └── options.lua
       ├── plugins
       │   ├── lsp
       │   │   ├── lspconfig.lua
       │   │   ├── lspsaga.lua
       │   │   ├── mason.lua
       │   │   └── null-ls.lua
       │   ├── autopairs.lua
       │   ├── comment.lua
       │   ├── gitsigns.lua
       │   ├── lualine.lua
       │   ├── nvim-cmp.lua
       │   ├── nvim-tree.lua
       │   ├── telescope.lua
       │   └── treesitter.lua
       └── plugins-setup.lua

Pre-requisite: Make sure you have installed ripgrep and one of the NerdFonts.

  1. Package Manager: Packer

  2. Essentials: plenary

  3. Colorscheme: tokyonight

  4. File Explorer: Nvim-tree, Telescope, Telescope-fzf

  5. Highlighting: tree-sitter

  6. Auto Completion: nvim-cmp, cmp-buffer, cmp-path, friendly-snippet,

  7. Comments: Comment

  8. Mode Status Bar: lualine

  9. Window Maximizer: vim-maximizer

  10. AutoPairing: nvim-autopairs

  11. Surround: vim-surround\

  12. Fast Navigation: hop\

    ...

Steps to install a new package

  1. Add the package under Packer in lua/usr/plugins-setup.lua, make sure it is installed before proceeding, type :PackerSync in plugins-setup.lua if not installed
  2. For some packages: Add a new file under lua/usr/plugins/, or whatever folder structure you have, configure the package as you want
  3. Import the package in init.lua

Keyboard Shortcuts I Use

These are the customized shortcuts I configured, which I use often. Feel free to map your own key mappings.

Package Shortcut Description
General <leader>sx Close current window
AutoCompletion General Tab Go to next suggestion
Nvim-Tree <leader>e Tree Toggle
Nvim-Tree o Open a folder or a file
Nvim-Tree v Open another file to the right
Nvim-Tree a Add a new file in the current directory
Nvim-Tree r Rename file under the cursor
Nvim-Tree d Delete file under the cursor
Comment gcc Comment/Uncomment current line
Comment gc5j Comment/Uncomment next 5 line
Vim-Surround ysiw" Surround the word under cursor with ", remember iw is a text object
Telescope <leader>ff Find files in current working director
Telescope <leader>fs Find string in current working directory
Telescope <leader>lds Lsp document symbols. Show all functions, structs, class, modules in current files
Telescope <leader>gc List all git commits
Telescope <leader>gs List current changes per file with diff views
Telescope Ctrl+c Close Telescope window while in INSERT mode
Vim-Maximizer <leader>sm Maximize/Unmaximize current window the cursor is on
Hop <leader>hc HopChar2, Type 2 chars and then jump to the match you want
Hop <leader>hcc HopChar2MW, Same as above, but in multiple windows

Other tips: Termtoggle plugins

Part 3: Neovim Language Protocol Server(LSP) Setup

Tutorial Video Links: Youtube, Bilibili

Plugins I use

  1. LSP manager: Mason, Mason-Lspconfig
  2. LSP configuration: lspconfig, cmp-nvim-lsp
  3. LSP Operation: LspSaga, trouble
  4. Other LSP servers: rust-tools, typescript
  5. LSP icons: lspkind
  6. Formatting & Linting: null-ls, mason-null-ls

Keyboard Shortcuts I Use

These are the customized shortcuts I configured, which I use often. Feel free to map your own key mappings.

Package Shortcut Description
Lsp config / Lsp saga gd Peek Definition of object/function under cursor
Lsp config / Lsp saga q Quit Peek Definition while in NORMAL mode
Lsp config / Lsp saga gD Go to Declaration of object/function under cursor
Lsp config / Lsp saga Ctrl + o Go back to prev buffer(Ex. Go back from Declaration)
Lsp config / Lsp saga Ctrl + i Go to next buffer
Lsp config / Lsp saga gr Show all References of the object/function under cursor
Lsp config / Lsp saga K Show Documentation under the cursor
Lsp saga ]d Go to next diagnostic
Lsp saga [d Go to prev diagnostic
Lsp saga <leader>rn Rename a variable/class/struct
Lsp saga <leader>tt Open a Floating Terminal
trouble <leader>xd Open diagnostic for current file
trouble q quit trouble diagnostic while in NORMAL mode

Part 4: VScode Vim

Tutorial Video Links: Youtube, Bilibili
Remember to remap the leader key to space in settings.json. For the emulated Vim plugins, some of them need to turn them on in settings.json as well, refer to the doc to see if that is required.

Edit in NORMAL

Package Shortcut Description Remap Needed?
Vim gb Mutlicursor operation
VScode Ctrl+n Rename all the pattern that is under the cursor keybindings.json
Vim Commentary gcc / gc5j Toggle comment on this line / next 5 line(not including current line)
EasyMotion <leader><leader>s+[char] Quick navigation to [char] on the screen(more motion on doc)
Vim-surround ys[motion][symbol], ysiw) Yank [symbol] around [motion], the example means surround the word under the cursor with ()
Vim-surround cs[symbol][newsymbol] Change [symbol] to [newsymbol] when your cursor is within the [symbol]
Vim Sneak <operator>z<char><char> Do [operator] until the next occurence of <char><char>
Vim Sneak 3dzqt An example: Delete everything until the next 3rd occurence of qt

Tab, File and Window Navigation

Package Shortcut Description Remap Needed?
VSCode ctrl + d Toggle peek definition keybindings.json
Vim gd Go to Definition
Vim gh Hover doc
VScode cmd/ctrl+p Search file by name and open
Vim tp, tn, tf, tl In the current editor group: prev tab, next tab, first tab, last tab settings.json
VScode ctrl+z Switch focus between editor and terminal keybindings.json
VScode cmd/ctrl+j Hide Terminal
VScode cmd/ctrl+b Toggle File explorer

Editor Group Navigation

Package Shortcut Description Remap Needed?
VSCode ctrl + \ Split Editor keybindings.json
VSCode ctrl + h/l Move to left/right editor group keybindings.json
VSCode cmd/ctrl + k + w Close all tabs in active editor group

Part 5: Lazyvim

See Lazy folder for my Lazyvim setup

LazyVim doc: here
Screenshot of the Installation Page: imge todo.nvim: here
New/Updated shorcuts I use the most:

Package Shortcut Description Notes
Telescope <leader>sg live grep string Different from Part 2
Telescope <leader>sk All shortcuts
Telescope <leader>sh All help pages
todo [t / ]t Prev/Next todo comment
Lspsaga <leader>gp Peek definition Different from Part 3
Lspconfig <leader>cr Rename Different from Part 3
trouble <leader>xx Open diagnostic for current file Different from Part 3
trouble <leader>xT Todo/Fix for current working space Use with todo.nvim
tabnine <C-n> AI autocomplete Tab is taken by nvim-cmp

Part 6: Chatgpt

OpenAI get API key: here

chatgpt.nvim

Repo link: here

CodeGPT (in VScode)

Official link: here