Vim for Wimps Pt.1

 

Getting Started with Neovim

If you've ever felt overwhelmed by the thought of using Vim, you're not alone. Vim's reputation for being difficult to learn often scares people away, but the truth is, once you get the hang of it, Vim can dramatically boost your productivity. In this series, "Vim for Wimps," we'll break down the essentials of getting started with Neovim (nvim), from setup to mastering basic motions and commands, and setting up Which Key to help you learn keyboard shortcuts.

 

Vim vs. Neovim

Before diving in, it's important to understand the differences between Vim and Neovim. Neovim is essentially an improved version of Vim, designed to address some of its limitations and modernize the experience.

Key Differences:

  • Extensibility: Neovim offers better support for plugins and scripting, making it easier to extend and customize.

  • Performance: Neovim has a built-in asynchronous job control, which can handle background processes more efficiently.

  • Modern Features: Neovim includes features like built-in terminal emulation and improved support for modern GUIs.

  • Community and Development: Neovim is actively maintained and has a growing community contributing to its development.

While Vim remains a powerful and reliable editor, Neovim provides a more modern, extensible, and performant alternative.

 

LazyVim: A Powerful Plugin Manager

LazyVim is a lazy-loading plugin manager for Neovim that helps optimize your setup by only loading plugins when they are needed. This can significantly improve startup time and overall performance. LazyVim makes it easy to manage and configure plugins, allowing you to tailor Neovim to your specific needs without sacrificing speed.

 

Setting Up Neovim with LazyVim

You can checkout the starter template for LazyVim here.

Linux/MacOS

Make a backup of your current Neovim files (optional):

bash
    
# required
mv ~/.config/nvim{,.bak}

# optional but recommended
mv ~/.local/share/nvim{,.bak}
mv ~/.local/state/nvim{,.bak}
mv ~/.cache/nvim{,.bak}
    
  

Clone the starter:

bash
    
git clone https://github.com/LazyVim/starter ~/.config/nvim
    
  

Remove the .git folder, so you can add it to your own repo later:

bash
    
rm -rf ~/.config/nvim/.git
    
  

Start Neovim!

bash
    
nvim
    
  

Windows

Instructions for Windows are typically similar, but might require adjustments specific to your system configuration.

 

Differences in Editing: Vim vs. Typical Code Editors

Vim's editing model is fundamentally different from typical code editors. Here's a breakdown of the key differences:

Copying and Pasting

  • Vim: Uses y (yank) to copy and p to paste. Visual mode can be used to select text before yanking.

    • Example: vwy yanks the current word, and p pastes it.

  • Typical Editors: Use Ctrl+C to copy and Ctrl+V to paste.

Deleting Text

  • Vim: Uses d to delete text. The scope of deletion can be specified by following d with a motion.

    • Example: dw deletes from the cursor to the end of the word.

  • Typical Editors: Use Delete or Backspace keys to remove text.

Undo and Redo

  • Vim: u is used to undo, and Ctrl+r is used to redo.

  • Typical Editors: Use Ctrl+Z to undo and Ctrl+Y to redo.

Moving Around

  • Vim: Navigation is primarily done in normal mode using keys h, j, k, l and word motions w, e, b.

  • Typical Editors: Use arrow keys or Ctrl+Arrow for word movements.

Inserting Text

  • Vim: Enter insert mode with i, a, o.

  • Typical Editors: Directly type to insert text, no mode switching required.

 

Basic Motions and Commands

Understanding basic motions and commands is crucial for navigating and editing efficiently in Vim. Neovim, like Vim, operates in different modes, each designed for specific tasks.

Normal Mode

Normal mode is the default mode for navigation and manipulation of text. Here are some essential commands:

  • h, j, k, l: Move left, down, up, and right respectively. These keys correspond to the arrow keys but allow you to navigate without leaving the home row.

  • w: Move the cursor forward to the start of the next word.

  • e: Move the cursor forward to the end of the current word.

  • b: Move the cursor backward to the start of the previous word.

  • gg: Jump to the beginning of the file.

  • G: Jump to the end of the file.

  • dd: Delete the entire current line.

  • yy: Yank (copy) the entire current line.

  • p: Paste the yanked or deleted text after the cursor.

Insert Mode

Insert mode is used for inserting text into the document. To enter insert mode, press i from normal mode. Here are some key insert mode commands:

  • i: Insert text before the cursor.

  • I: Insert text at the beginning of the current line.

  • a: Insert text after the cursor.

  • A: Insert text at the end of the current line.

  • o: Open a new line below the current line and enter insert mode.

  • O: Open a new line above the current line and enter insert mode.

Visual Mode

Visual mode allows you to select text. There are three types of visual mode:

  • v: Start character-wise visual mode, allowing you to select individual characters.

  • V: Start line-wise visual mode, allowing you to select whole lines.

  • Ctrl-v: Start block-wise visual mode, allowing you to select a rectangular block of text.

While in visual mode, you can use normal mode commands to manipulate the selected text, such as d to delete or y to yank (copy).

Command Mode

Command mode is used for executing commands that affect the entire document or session. To enter command mode, press : from normal mode. Some useful command mode commands include:

  • :w: Save the current file.

  • :q: Quit the current window.

  • :wq: Save and quit.

  • :h [command]: Open help for a specific command.

 

LazyVim Plugins

LazyVim comes with a ton of optional plugins that can be configured through the lazy interface. While we'll cover additional plugins in a future article, for now, let's start by committing commands to memory with the help of Which Key.

 

Setting Up Which Key

Which Key is a plugin that displays available keybindings in a popup, helping you learn and remember shortcuts.

Create a Which Key Configuration File:

Add a new file for Which Key in the plugins folder called which-key.lua:

bash
    
touch ~/.config/nvim/lua/[user_name]/plugins/which-key.lua
    
  

Set Up Which Key:

Add the following configuration to which-key.lua:

which-key.lua
    
return {
  "folke/which-key.nvim",
  event = "VeryLazy",
  init = function()
    vim.o.timeout = true
    vim.o.timeoutlen = 300
  end,
  config = function()
    local wk = require("which-key")
  end
}
    
  

This is all that is needed to get Which Key up and running. We will add more to this configuration in the follow-up Pt.2.

 

Setting Up the Leader Key

In Vim, the leader key is a way to create custom shortcuts. By default, Vim uses \ as the leader key, but it's common to remap it to space for easier access. We'll set up the leader key in the config/keymaps.lua file provided by the LazyVim starter template.

Set Up the Leader Key:

Add the following configuration to your config/keymaps.lua file:

keympas.lua
    
vim.g.mapleader = ' '
vim.g.maplocalleader = ' '
    
  

This configuration sets the leader key to space, allowing you to create custom shortcuts starting with the space key.

Example Keybindings:

Once the leader key is set up, you can create custom keybindings using it. For example, to save a file with space + w, you can add the following to your config/keymaps.lua:

which-key.lua
    
return {
  "folke/which-key.nvim",
  event = "VeryLazy",
  init = function()
    vim.o.timeout = true
    vim.o.timeoutlen = 300
  end,
  config = function()
    local wk = require("which-key")

    wk.register({
      [""] = {
        ["w"] = { ":w", "Write file" },
    })
  end
}
    
  
 

Conclusion

Getting started with Neovim doesn't have to be intimidating. By setting up LazyVim, mastering basic motions and commands, and using Which Key to learn shortcuts, you'll be well on your way to becoming proficient with Vim. Stay tuned for the next part of "Vim for Wimps," where we'll dive deeper into more advanced features and customizations.

Previous
Previous

Productivity Super Stack

Next
Next

The Origin Story