pwshub.com

A Noob's Neovim Journey: Pt. 1 "But Why?" — Beginner's Guide to Neovim: Understanding the Basics and Why I'm doing this

Introduction

If you’re a programmer, you’ve probably heard of Vim, either from your colleagues who type frantically on their niche mechanical keyboards at the office or from an internet meme at some point. A friend of mine said something that stuck with me about Vim:

If you love to code, you’d love Vim.

Similar to coding, Vim gives you the same sense of freedom; it’s yours and nobody else’s, updating to the latest version isn’t mandatory so if the official repository removes or adds something you don’t like, you can even fork the repository from the version you desire and create your own distro as you see fit.

Occasionally, I come across videos on YouTube where fellow developers showcase their terminal and Vim integrations, demonstrating incredible workflow speeds and workspace management without having to click on anything. I’d often think to myself, "Wow, that’s sick! I wonder how they do that…"

So, what is Vim exactly?

The acronym for Vim is Vi IMproved, a free and open-source text editor. It’s lightweight and capable of running on low-end machines. It’s a program that can run in a terminal with a graphical interface that is supported in every known platform. It is loved by plenty of developers for being extremely customizable while *possibly* improving productivity.

Vim encourages the use of keyboards. You can essentially configure commands to whatever keybindings you want, if you have small or large hands and a keybind doesn’t feel comfortable, just change it to something that makes more sense for YOU. I’ve always enjoyed understanding how things work and, as a child, I’d take my toys apart and put them back together. This innate curiosity set me on the path that eventually led me to software engineering. I have a similar feeling when it comes to Vim, so I first decided to give it a go to prove to myself that I can get comfortable with being uncomfortable.

Vim is for programmers who want to raise their game. In the hands of an expert, Vim shreds text at the speed of thought – Drew, Neil

In this article, I will touch on several aspects both text editors offer. Whenever I mention Vim, the same can be applied to Neovim. Learning how to use Vim can be overwhelming, and you might question why you should bother in the first place. I will explain why I switched to Neovim, provide some initial guidelines to help you understand key concepts, and hopefully, inspire you to give Neovim a try.

Why I uninstalled VS Code

I’ve used several Integrated Development Environments (IDEs) throughout my career, and for the most part, I’ve relied on VS Code as my primary choice. It’s rich in extensions and highly customizable, but I’ve always felt like it was lacking something. While it allowed customization within the IDE, the layout itself didn’t offer much flexibility, akin to a soldered processor in a modular system.

In my day-to-day, I often have multiple terminal instances running, and I’ve encountered random glitches with VS Code’s integrated terminal. This would force me to close the glitchy instance and open a new one to restore its UI.

Additionally, the integrated terminal’s limited space caused issues with CLI commands like docker ps, which would often break the output into multiple lines. Reviewing server logs was also cumbersome due to these limitations.

Despite attempting custom configurations for the internal terminal, the issue persisted. While I understand that it might’ve been a dependency incompatibility issue on my end, over time, this became a constant headache, leading me to resort to using an external terminal instead. This raised the question in my mind: “Why do I have to use two different tools to get my work done?”

Why Neovim instead of Vim?

Neovim is a fork of the Vim repository, created by the community to incorporate features that were being rejected by Vim’s creator, the late Bram Moolenaar. These rejected features include async support for plugins and other extensions.

Neovim was created by the Vim community as a more open alternative, where new features would be added and maintained by the community more democratically while still honoring Vim’s core features – this is something I can back!

Additionally, the community put in a lot of effort to create extensive and high-quality documentation, making Neovim more user-friendly for newcomers and providing native Lua support for plugin development. In my spare time, I dabble in a little bit of game development, so having some familiarity with Lua might come in handy someday, and this was a huge plus for me.

DIY

One of the reasons why I prefer Linux to Windows is because I can control what is and what isn’t installed on my machine, so instead of installing other Vim distros such as NvChad and LunarVim, I went with the out the box version of Neovim instead. No bloatware, just the essentials to get me started.

Getting Started

Before you install Neovim, let me go over some key concepts that differenciate Vim and Neovim from other text editors and IDEs. Vim can be separated into three parts: vim motions, the vim editor and vim plugins.

Vim Motions & Commands

Vim motions consist of modes and commands that help us developers navigate and edit code more efficiently, without taking our hands off the keyboard. Whether you want to visualize an existing file and navigate through it, create a new file or open up your terminal, this can all be done with a few finger strokes!

Let’s start with the basics: moving your cursor in vim is fundamentally different than how you would do so in other text-based applications. You can still use the arrow keys, but they are placed far away from the center of your keyboard, hell, they are even removed from smaller keyboards altogether! Instead, we use the h, j, kand l keys for conventional navigation.

Think of these keys as your arrow keys, they move the cursor left, down, up, and right respectively.

To hop around a bit faster, we use the w and b keys, they allow us to jump between words.

The w key moves the cursor forward to the beginning of the next word

The b key moves the cursor back to the start of the previous word

Multiple motions

To move around even faster we can specify the number of times we want the register commands, consider the following example:

With my cursor at the letter J in our first word, the following commands are used to navigate around:
3w to skip to the third word from the cursor
2b to go back two words from the cursor
3j to go down three lines from the cursor
1k to go up one line from the cursor
2k to go up two lines from the cursor

For more motions, run the command :h motion.txt or consult the documentation.

In order to actually use our text editor, let’s look into how the vim editor works. Let’s go over each of its modes.

Modes

I like to think of Vim’s modes as seperate modules, with each module having its own set of business rules. This helps me visualize them and their functions separately.

Normal Mode

By default, Vim/Neovim sessions open up in Normal Mode. While in this mode, you can navigate and control text using vim motions in order to copy/cut/paste characters, words, and lines. Normal mode also has a few *select* modes, but I found myself using visual mode instead for that. More on that below!
Shortcut → ESC key

Visual Mode

Visual mode works on top of normal mode, allowing us to visually select and manipulate text. It also has three different variants:

– character-wise visual mode
Micro-select individual characters.
Shortcut →v key
– line-wise visual mode
Select lines. This is commonly used to apply changes line-wise
Shortcut →V key
– block-wise visual mode
Select columns and rows. You have more freedom to move around in this mode than the previous two.
Shortcut →Ctrl-v keys

Insert Mode

As the name suggests, in this mode we insert text into our text editor like traditional IDEs.
Shortcut →ikey

Command Mode

The command line can be triggered at any moment while Vim or Neovim is open and in focus by pressing the colon : key. This will set our session into the Command mode and open up the command line function.
Shortcut → : key

There are way too many commands to list here, so for time’s sake I’ll only go into a few basic ones. It’s also important to note that installed plugins also have their own set of commands!

Commands

:q quits the current window, and warns you if you didn’t save your changes.
:qa quits all windows, and warns you if you didn’t save your changes.
:qa! quits all windows without warnings.
:w writes (saves) your file changes.
:wq writes your file changes and quits the current window.
:wa writes all file changes.
:wqa writes all file changes and quits all windows.

Buffers and Windows

Buffers

Buffers are files loaded temporarily in memory, similar to file tabs in IDEs. Think of buffers as data temporarily stored in an array (the array being your Vim editor). You can then access each buffer by reading them from memory (lightning fast!). An empty file is also considered a buffer, keep that in mind!

We typically open up quite a few different files while working on coding projects. Having to go back and forth between files can be annoying, but with buffers, we can organize which files we wish to display, hiding unwanted buffers in the meantime.

With more than one buffer loaded, we can cycle between buffers with :bnext and :bprevious. Each buffer has a unique number that will not change within a Vim session
:buffers
:ls

In this example, I have two buffers open. If I want to unload a specific buffer, instead of :q I would run :bd 19 or :bd 31. If a buffer number isn’t specified, :bd will unload the currently loaded buffer.

Buffers are also unloaded when you exit your vim sessions by default.

Windows

Windows are viewports for our buffers; it’s the GUI that we see and interact with. A window doesn’t exist without a buffer, meaning an empty buffer will still have a window.

We can, however, have many windows open on the same buffer, splitting the buffer into multiple windows and opening distinct files in each window. If we close a buffer, all of its respective windows will close.

Window manipulation in vim is super flexible, we can open up files vertically or horizontally within our buffer, resize, and move them around dynamically as well! Here are a few commands of these commands:

Splitting Windows
:split or :sp – Split the current window horizontally.
:vsplit or :vsp – Split the current window vertically.

Navigating Between Windows
Ctrl-w w – Move to the next window.
Ctrl-w h – Move to the window to the left.
Ctrl-w j – Move to the window below.
Ctrl-w k – Move to the window above.
Ctrl-w l – Move to the window to the right.

Resizing Windows
Ctrl-w = – Make all windows equal height and width.
Ctrl-w + – Increase the height of the current window.
Ctrl-w - – Decrease the height of the current window.
Ctrl-w > – Increase the width of the current window.
Ctrl-w < – Decrease the width of the current window.

Closing Windows
:q – Close the current window.
:close – Close the current window.
:only – Close all other windows except the current one.

To recap:
– A buffer can be displayed in one or more windows
– A window only exists to display a buffer
– If we unload a buffer from our buffer list with :bd, any windows displaying that buffer will be closed

There are plenty of plugins available to help manage buffers and windows, like telescope if you want something more visual.

Additional Tips

– Switching Buffers: Use :buffer {number} or :b {number} to switch to a specific buffer by its number
– Deleting Buffers: Use :bdelete {number} or :bd {number} to delete a specific buffer by its number
– Opening Files in Buffers: Use :e {filename} to open a file in a new buffer

Plugins

One of the standout features of Neovim is its robust support for plugins, which allows users to extend and customize their editing environment to their own specifications. Neovim supports a variety of plugin managers, such as Packer, Lazy, and Plug in order to install plugins painlessly. This is where the fun begins — where you can improve your personalized experience!

…but why?

Theme / Color scheme plugins

Whether you want to adjust your editor’s color scheme, add background images or opacity, chances are that there are plugins for what you’re looking for! Some people to keep their terminals as simple and retro as possible, and well, there are themes for that too!

Tokyonight Theme
This has been my go-to for a while now, I use this theme for my terminal theme as well. Its default colors are decent with dark mode and the only color scheme I changed for it was its default comments color since I usually use a slightly transparent editor.

Nvm-web-devicons
This plugin provides icons for each of your folders and file types, while also giving you the option to change their colors as well.

Transparent.nvim
Add transparency to your editor, toggle it on and off with direct commands.

Alpha-nvim
A programmable greeter for neovim, with customizable ASCII dashboard headers, shortcut menus, and more!

Lualine.nvim
A blazing fast and easy-to-configure Neovim status line written in Lua. Need more information available to you for each buffer? Want to see your git changes as well? This has got you covered!

Navigation

We want to move around fast, right? These few plugins will have you covered.

Nvim-tree
Strongly recommended!
A highly customizable file explorer for Neovim written in lua, complete with git integration, diagnostics integration, and more!

Telescope.nvim
Strongly recommended!
A highly extendable fuzzy finder for neovim, which is built on features from neovim core! Find files, buffers, keywords, and more, fast with live previews!

Toggleterm.nvim
Strongly recommended!
Persist and toggle multiple terminals in your editor, a dream come true for developers!

LSP

Language Server Protocols were developed for Microsoft VS Code, but have since been integrated into many other text editors and IDEs.

Mason.nvim
Strongly recommended!
A jack of all trades for installing and managing LSP servers, DAP servers, linters, and formatters!

Nvim-lspconfig
Strongly recommended!
Provides basic, default Nvim LSP client configurations for various LSP servers.

Mason-lspconfig.nvim
Strongly recommended!
Birdges the mason plugin with the lspconfig plugin.

Parsers

Nvim-treesitter
Strongly recommended!
Parses the text of your open file, building and updating a syntax tree based on your code as you write. This is extremely powerful, it is also dependency-free so that the runtime library (written in pure C) can be embedded in any application!

Completions

Sometimes less is more, enter completion plugins!

Nvim-cmp
A completion engine plugin written in lua, completion sources are installed from external repos.

Friendly Snippets
A snippet collection for many different programming languages, it’s maintained by many contributors to centralize outsourcing from a single repository!

Git

Gitsigns.nvim
Strongly recommended!
Git decorations implemented purely in Lua. Highly configurable, I use this constantly!

Vim-fugitive
Strongly recommended!
Created by one of Vim’s GOATs, fugitive integrates fully into designing commits, searching code, comparing branches, and more. Combine this with gitsigns and telescope for an awesome experience!

These are just examples that I personally use. In my next article I will go into depth on how to go about configuring a few of these!

Recommendations

Vim and Neovim come with built-in documentation and tutorials and that’s where I started. Run :Tutor at any time to pop up a neat tutorial!

If you want to ease into it before making the jump and actually installing Neovim, here are a few tools I used to get more comfortable with vim motions:

Vim adventures, a gamefied vim experience!
Vim Hero, learn vim with interactive examples, challenges, and games

If you’re used to VS Code and don’t want to fully commit, consider giving the VSCode Neovim extension a try.

If you want to configure neovim to start coding ASAP, check out our post that helps you configure LSP here.

Conclusion

Neovim offers a powerful and highly customizable environment for developers who are willing to invest the time to learn its intricacies. Its flexibility and efficiency can significantly enhance your coding workflow once you get the hang of it. Whether you’re looking to streamline your text editing or explore new ways to boost your productivity, Neovim has something to offer.

Stay tuned for Pt. 2, where I will guide you through creating a new Neovim configuration from scratch. We’ll dive deeper into setting up plugins, customizing keybindings, and optimizing your development environment. Thank you for reading along and happy coding!

References

We want to work with you. Check out our "What We Do" section!

Source: blog.codeminer42.com

Related stories
1 month ago - Google Analytics is often on a “need to know” basis, but why not flip the script? Paul Scanlon shares how he wrote a GitHub Action that queries Google Analytics to automatically generate and post a top ten page views report to Slack,...
1 month ago - Next allows you to do server-side data-fetching, but what happens when that data needs to change on the client? This brief tutorial shows how to re-fetch the props without doing a full server reload.
1 month ago - Get a sneak peek at the upcoming features in Python 3.13 aimed at enhancing performance. In this tutorial, you'll make a custom Python build with Docker to enable free threading and an experimental JIT compiler. Along the way, you'll...
1 month ago - In this tutorial, you'll learn how to use a Python virtual environment to manage your Python projects. You'll also gain a deep understanding of the structure of virtual environments created with the venv module, as well as the rationale...
1 week ago - In a previous article, I showed you how to create some fancy shapes that you can use as section dividers on your websites (a slanted divider, an arrow divider, and others). In this article, we will study and learn how to make more CSS...
Other stories
1 hour ago - Credit monitoring services are designed to keep a watchful eye on the status of your credit file. They provide all the information needed to ensure your credit profile looks healthy, including scores and reports, and alert you of any...
4 hours ago - OOP is a crucial concept that every developer should grasp, especially when getting ready for job interviews. It helps you organize code into modular and reusable sections, which simplifies the development, maintenance, and scaling of...
6 hours ago - Vivaldi 7.0 is out, and the makers describe it as not merely an update but ‘a new Vivaldi’ entirely. Those familiar with the browser will instantly see why, as Vivaldi finally gets an overdue UI redesign: there’s a new theme using...
12 hours ago - Securing your application goes beyond simply granting or denying access at the surface level. As a developer, you need to implement fine-grained authorization (FGA) to manage permissions at a more detailed, granular level. FGA allows you...
12 hours ago - Companies use dark patterns to boost conversions. But ethical UX design fosters trust through transparency and consent — benefiting users and brands alike. Learn how. The post Dark patterns in UX design: What they are and what to do...