banner
innei

innei

写代码是因为爱,写到世界充满爱!
github
telegram
twitter

I started tinkering with NeoVim again, wondering if it can replace VSC this time.

image

What can using NeoVim bring? What are its advantages compared to traditional editors?

The following content may not be read by many people, so I decided to write this part first. After reading it, you can decide whether to configure it, as this is a very lengthy yet enjoyable process, and you need to get used to the horizontal movement of hjkl and a lot of shortcuts.

The biggest advantage of using Vim is that you can abandon the mouse. When writing code, you won't experience pauses due to needing to use the mouse for positioning, allowing you to focus more on coding without interruptions.

Secondly, it is very fast, with a startup time of under 100ms, very low memory usage, and quick response times, unlike some IDEs that struggle even with 96GB of memory.

With the support of plugins, you can quickly address the following common issues.

  1. Selection

In VSC, selecting a block of code from one place to another may require dragging with the mouse to complete the selection. In NeoVim, using Treesitter's increase selection, you only need to press Enter a few times.

Additionally, NeoVim + Treesitter provides many text objects that make selecting and modifying text objects very convenient.

  1. Adding/modifying/deleting parentheses

In VSC, I don't know how others usually operate, but I would select with the mouse, use keyboard shortcuts to cut, then add parentheses and paste. This takes a lot of time. In NeoVim, you can use mini-surround.

  1. Quick movement

If you see a keyword in the current view in VSC and want to move the cursor there to type, you might need to use the mouse. But in NeoVim, using the capabilities of flash.nvim, for example, if I want to move to console.log and delete that line.

  1. Quick movement in context
  1. Fuzzy matching

Fuzzy matching based on paths and file contents is something VSC cannot do.


Summary of Previous Events#

I have been using NeoVim (hereinafter referred to as nvim) for 3 years. I remember back then it was still nvim 0.4.x, and more than half a year later, it was still stuck on the 0.5 dev version, waiting for the arrival of 0.5 stable.

At that time, I was still writing Vue 2, and nvim did not have telescope.nvim or nvim-lsp. Although it was no longer the era of YouCompleteMe, nvim still did not have a very efficient lsp. At that time, we were all using coc.nvim, which is an lsp powered by NodeJS. Because NodeJS's runtime and the interface adopted by VSC are consistent, it is very convenient to port VSC plugins to coc. However, due to coc's performance concerns and the rapid changes in the coc ecosystem, I ultimately chose to give up using nvim as my main editor.

In the past two years, nvim has entered the lua era. During this time, I occasionally paid attention to the nvim community, tinkering with fun plugins. However, since the original configuration was mostly in VimScript, it was difficult to migrate to lua. Although both can be mixed, I had long forgotten the concerning startup speed and numerous shortcuts.

image

This time, after seeing a video from theniceboy about his experience migrating nvim configurations to lua, I felt inspired to start configuring nvim again, and this time I completely rebuilt my original configuration of over a thousand lines from scratch.

Getting Started#

I use kitty as the terminal emulator for nvim. Kitty supports tilde, is fast enough, and renders characters better than iTerm2. Compared to Alacritty, its configuration is simpler. Plus, its icons are cute.

The nvim community has always been very active, and the plugin ecosystem is vast. Currently, there are the following out-of-the-box configurations in the community:

From the number of stars, AstroNvim has the most, and there are many community-maintained preset plugin configurations. Moreover, the main configuration and user configuration are separated, making it easier to maintain later and less of a mental burden. New users are recommended to use this directly and familiarize themselves with the preset keybindings.

Note that the preset keybindings for each of the above are different, so once you get used to one, it will be hard to switch to another. You need to choose the one that suits you best.

I use LazyNvim.

git clone https://github.com/LazyVim/starter ~/.config/nvim
rm -rf ~/.config/nvim/.git

After installation, you can start using it. It has basic functionalities, and lsp, telescope, and treesitter are all configured. Then you can watch this video to familiarize yourself with the basic operations:

I won't elaborate on this; those who don't want to tinker won't see this part anyway, and those who want to tinker have already gone to read the official documentation. My rambling here is of little use, as it is not a tutorial post.

Key Movement#

As a vim user with a qwerty keyboard layout, moving with hjkl is essential. I use Hammerspoon to map the system keys, mapping up, down, left, and right to <Caps-hjkl>. By the way, I have now changed the CapsLock key to also function as the Control key in the settings. Control is still Control; I have completely abandoned CapsLock.

image

The mapping configuration for hjkl is as follows:

https://gist.github.com/Innei/695460015ba94e8d163d3665d707c6b4

Migrating Common Configurations#

As mentioned earlier, I previously had over a thousand lines of configuration. Although I started from scratch this time, some configurations still need to be set.

Currently, I have configured the following options, while using the default values for the rest provided by LazyNvim.

local opt = vim.opt
opt.clipboard = "unnamedplus"
opt.wrap = true
opt.clipboard = ""

opt.spelllang = "en,cjk"
-- vim.opt.spell = true
opt.spelloptions = "camel"
opt.scrolloff = 5
opt.indentexpr = ""
opt.foldmethod = "indent"
opt.foldlevel = 99
opt.foldenable = true
opt.foldlevelstart = 99

I have enabled code folding, disabled system clipboard synchronization, enabled line wrapping for code, and set the cursor to maintain a distance of 5 lines from the bottom. That's about it.

Then there are some regular keymaps; ; to : is still very convenient. y to +"y means only y will copy to the system clipboard. to <C-g>u is quite special, as it can solve the issue of nvim having too large an undo range in insert mode. This way, a space will act as a memory point. Similarly, you can do the same for =.

https://github.com/Innei/nvim-config-lua/blob/main/lua/config/keymaps.lua

There are also some keymaps prepared for kitty. When using kitty, you can create some character mappings specifically for nvim. For example, I configured in kitty that when I press P in nvim, it sends the character sequence of P, and then I can map <M-p> in nvim to :Telescope fd, achieving the same functionality as VSC's P.

Additionally, I have set up , S, , D, C, , X, and so on.

https://github.com/Innei/dotfiles/blob/master/tag-base/config/kitty/keymap.py

(This inspiration comes from sxyazi)

Plugin Sharing#

Plugins are the primary productivity tool for nvim as a main editor. I won't recommend the ones that come with LazyNvim; everyone uses them, and the defaults are fine.

better-escape.nvim#

This can eliminate the waiting for characters after pressing jk. It's usable but not very necessary.

fedepujol/move.nvim#

This allows for block-level movement of selections.

LunarVim/bigfile.nvim#

This can speed up handling large files. However, I still recommend using VSC to open large files, as it currently has the best support for large files, with the fastest opening speed and smoothest experience.

dstein64/nvim-scrollview#

This can add a scrollbar indicator to the right side of nvim.

AndrewRadev/switch.vim#

This allows for easy toggling between false and true.

Wansmer/treesj#

This merges multiple lines into one based on AST or vice versa. It's quite convenient.

mbbill/undotree#

I haven't used it much, but it's quite useful at critical moments for tracing undo history.

gbprod/yanky.nvim#

Clipboard history.

Eandrju/cellular-automaton.nvim#

A useless thing, just for fun.

Code rain.

Bekaboo/dropbar.nvim#

Similar to VSC's breadcrumb navigation, requires nvim > 0.10.

This article was synchronized and updated to xLog by Mix Space. The original link is https://innei.in/posts/Z-Turn/nvim-lua-config-init

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.