blog-content/2025-05-21_nvim.md

144 lines
8.4 KiB
Markdown
Raw Normal View History

2025-05-20 22:16:52 -07:00
______________________________________________________________________
date: 2025-05-21
2025-05-20 21:54:53 -07:00
title: Exploring Neovim
2025-05-20 22:16:52 -07:00
tags:
- software
- linux
- development
______________________________________________________________________
I don't know exactly what it was that prompted me to start looking at `vim` and `nvim`
about a week ago, but I ended up down that rabbithole this week. For the uninitiated,
[vim](<https://en.wikipedia.org/wiki/Vim_(text_editor)>) is a modal text editor and
2025-05-21 08:56:04 -07:00
[Neovim](https://neovim.io/) is a popular fork of vim. I have no strong opinion regarding
vim vs Nvim vs emacs, but I found some [good tutorials for nvim](https://www.youtube.com/playlist?list=PLsz00TDipIffreIaUNk64KxTIkQaGguqn),
2025-05-20 21:54:53 -07:00
so Neovim is the editor I chose.
## Background Information
2025-05-20 22:16:52 -07:00
2025-05-20 21:54:53 -07:00
I have worked as a software developer for more than five years, which begs the question:
what have I been using for a text editor and why am I bothering to write about something
so trivial? Well, I have used [nano](https://www.nano-editor.org/) for the most part when
2025-05-21 08:56:04 -07:00
I need to edit something in a terminal, but my preference has been to paste things into a
2025-05-20 21:54:53 -07:00
gui application for most tasks. As for why I'm writing this, I actually learned a lot
2025-05-20 22:16:52 -07:00
about the tools that make IDEs like VSCode and PyCharm work (spoiler alert: those same
2025-05-21 08:56:04 -07:00
tools work with `nvim`) and I think it is important to understand the tools we rely on.
2025-05-20 21:54:53 -07:00
2025-05-21 08:56:04 -07:00
That said, I have no previous knowledge about how modal text editors work and prior
to the past week or so, I was just as trapped in `vim` if I managed to accidentally opened
it (yes, I have searched "how to quit vim"). There are a few cases where it would have
been very helpful if I had learned this sooner; some distibutions of Debian that I have used
include `vim`, but not `nano` and I found myself using `cat` and `sed` to make minor
changes just to get online to install `nano`.
2025-05-20 21:54:53 -07:00
## Some `nvim` Basics
2025-05-20 22:16:52 -07:00
2025-05-21 08:56:04 -07:00
This post will *not* be a detailed how-to on using `nvim`, but I feel that I should at
2025-05-20 22:16:52 -07:00
least cover some basics. Having used `k9s` and `tmux`, I came into this with some
2025-05-20 21:54:53 -07:00
familiarity typing `:` to get at a command input. I also quickly discovered that
2025-05-21 08:56:04 -07:00
tab-completion works in this field. The best way to get started is to open `nvim` and type
2025-05-20 21:54:53 -07:00
`:Tutor` and then work through the interactive tutorial. I personally still use the
2025-05-21 08:56:04 -07:00
arrow keys for navigation which *do* work in `nvim`, although `hjkl` is more
2025-05-20 21:54:53 -07:00
efficient for keeping your fingers on the right keys. I also noted that `home`, `end`,
2025-05-21 08:56:04 -07:00
etc also work in addition to the `vim` navigation keys `^` and `$`.
2025-05-20 21:54:53 -07:00
## Lua Scripts
2025-05-20 22:16:52 -07:00
2025-05-20 21:54:53 -07:00
Beyond basic text editing, the real reason to use `nvim` is Lua script support. I have
already updated my [public dotfiles repository](https://forge.mcknight.tech/d_mcknight/dotfiles/src/commit/1e6512df4903965b6b9acf361b638c19bce9d78b/nvim)
with my nvim configuration. For the most part, I followed the excellent tutorials I linked
above from [typecraft on YouTube](https://www.youtube.com/playlist?list=PLsz00TDipIffreIaUNk64KxTIkQaGguqn).
2025-05-20 22:16:52 -07:00
Personally, I only went so far as implementing LSP integration and left the code
2025-05-20 21:54:53 -07:00
completion and debugging tasks for an IDE to handle. I may add these to my `nvim`
config in the future, but for now I just want to spend some more time getting used to
`nvim` as a text editor.
To quickly review some of the functionality I find myself using thanks to plugins:
2025-05-20 22:16:52 -07:00
2025-05-20 21:54:53 -07:00
- neotree (mapped to `<space>f` in my config) gives me a file tree to navigate to different files and show file status info, similar to VSCode
- treesitter provides syntax highlighting for everything I've edited so far, and it automatically pulls new definitions as needed
- Telescope provides a UI for searching file names and contents, though I admittedly haven't used it much
2025-05-21 08:56:04 -07:00
- gitsigns (mapped to `<space>gb`) provides inline highlighting of changes and git blame support, just like what I use in VSCode
- which-key provides hints after starting a command and is very helpful for remembering some of my less-used key bindings
2025-05-20 21:54:53 -07:00
### Language Server Protocol (LSP)
2025-05-20 22:16:52 -07:00
2025-05-21 08:56:04 -07:00
Prior to using `nvim`, I thought syntax highlighting and code completion was all
2025-05-20 21:54:53 -07:00
built into IDEs. In fact, the [Language Server Protocol](https://microsoft.github.io/language-server-protocol/)
is a standard method by which a program (language server) can receive information about a
2025-05-20 22:16:52 -07:00
file and respond with information for auto-completion, definitions, references, and all
2025-05-21 08:56:04 -07:00
the other things that differentiate an IDE editor from a basic text editor. With this
2025-05-20 21:54:53 -07:00
knowledge, I better understand now how IDE language plugins work and how it is that VSCode
seems to support every language in existence.
For `nvim`, I am using `mason`, `nvim-lspconfig`, and `none-ls` to implement LSP
functionality. This combination of packages provides a system for managing language
2025-05-21 08:56:04 -07:00
servers, syntax highlighting, code definitions, and auto-formatting.
2025-05-20 21:54:53 -07:00
### Tmux integration
2025-05-20 22:16:52 -07:00
2025-05-21 08:56:04 -07:00
Since [I use tmux](https://blog.mcknight.tech/2024/03/27/Shell-Customizations/#tmux),
I am also using [vim-tmux-navigator](https://github.com/christoomey/vim-tmux-navigator) so
2025-05-20 22:16:52 -07:00
that I can use the same `<ctrl>`+`Arrow` shortcut to swich between tmux panes and nvim
2025-05-21 08:56:04 -07:00
buffers. The one repository includes both a `tmux` plugin and a `nvim` plugin, which is
2025-05-20 21:54:53 -07:00
how the same key input is passed to the appropriate program, depending on where focus
2025-05-21 08:56:04 -07:00
currently is. So far, I don't really work in multiple nvim windows except that I often
2025-05-20 21:54:53 -07:00
open neotree to have a file tree to the left of my vim editor window. I'm not sure how
2025-05-20 22:16:52 -07:00
this might change in the future, but for now it is still very useful to move between
2025-05-20 21:54:53 -07:00
the logical areas of my terminal in a consistent way.
## Workflow Changes
2025-05-20 22:16:52 -07:00
In making the transition to `nvim`, I have found it a lot easier to manage my
2025-05-20 21:54:53 -07:00
[dotfiles repository](https://forge.mcknight.tech/d_mcknight/dotfiles/src/branch/main)
with `nvim` since I don't usually bother attaching that directory to an IDE. Now, I get
2025-05-20 22:16:52 -07:00
syntax highlighting and visual change indicators, making it easier to roll back bad
2025-05-20 21:54:53 -07:00
changes and make fewer mistakes in the first place! I also made some changes to my
Alacritty and tmux configs to achieve a more consistent look and feel within the various
terminal applications.
2025-05-21 08:56:04 -07:00
I am also using `nvim` to write and edit this post! I am particularly happy with the
markdown linter `mdformat` which helps easily insert line breaks so that I don't have
to do it manually.
Not seen in my code repositories, one of the major changes I made is swapping the
2025-05-20 21:54:53 -07:00
`esc` and `caps lock` keys on both my laptop and desktop keyboard. For my own reference
and in case anyone wants to do the same on their Framework 13 Laptop, this was a pretty
easy change using the [fw-ectool](https://www.howett.net/posts/2021-12-framework-ec/).
```shell
sudo fw-ectool raw 0x3E0C d1,d1,b4,b4,w76 # Map `esc` command to `CL` key
sudo fw-ectool raw 0x3E0C d1,d1,b7,b5,w58 # Map `CL` command to `esc` key
```
For my desktop, I have a keyboard with QMK support, so that was straightforward to change
the keymap on. Now, apart from muscle memory sometimes reaching for the `esc` key, it is
much easier to exit modes in `nvim`. I've long held out from remapping keys on keyboards
2025-05-21 08:56:04 -07:00
since I then have to remember where things are when using any other computer, but at
2025-05-20 22:16:52 -07:00
this point I think its worth it so I'm not twisting my left wrist every time I need to
2025-05-20 21:54:53 -07:00
reach `esc`.
## What to do next?
2025-05-20 22:16:52 -07:00
2025-05-20 21:54:53 -07:00
Working through this `nvim` setup has gotten me thinking more about how I can optimize
my daily workflow. I might look into CLI file management tools next as I do find myself
2025-05-20 22:16:52 -07:00
using a GUI file explorer regularly, sometimes just to end up copying a path to paste
2025-05-20 21:54:53 -07:00
into an open terminal. I briefly skimmed some search results and came up with a bunch of
imperfect options, but it may be worth some deeper research and testing.
Another possibility I've been thinking of is trying out a tiling window manager. The
more I think about what this looks like though, the more problems seem to arise like
2025-05-20 22:16:52 -07:00
"how will that work with screen-sharing in Zoom?" and "can I use Wayland on my work
2025-05-20 21:54:53 -07:00
computer with Nvidia graphics?" It's an interesting concept, but I don't know that it
2025-05-21 08:56:04 -07:00
would be a huge benefit compared to my current setup of gTile with Cinnamon desktop.
2025-05-20 21:54:53 -07:00
Yet another thing I could do is start using [GNU Stow](https://www.gnu.org/software/stow/)
to manage my dotfiles. I have my own little script for linking some of my dotfiles, but I
2025-05-20 22:16:52 -07:00
recently learned that there's actually a tool made for that very purpose! I'm a firm
2025-05-20 21:54:53 -07:00
believer in *not* re-inventing the wheel, so if there's an existing tool available, I
2025-05-21 08:56:04 -07:00
would rather use it instead of maintaining something just for myself.