paint-brush
A Beginner's Guide to Vimby@aviavinav0
777 reads
777 reads

A Beginner's Guide to Vim

by Avi AvinavSeptember 20th, 2022
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Vim is one of the oldest contestants in the text editor war, ever since its release in 1991. It has been a friend to many and foe to many, wanna find out what it will be for you? Then, let's try it out! In this article, I'll be discussing why to use Vim, how to set it up, and some basics of Vim. I am not comparing Vim with any other text editor, I am just telling you what Vim has to offer. Vim is fully customizable** - It means you have full control, some people even make it look like vscode (I personally like to keep it minimal though)

Companies Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - A Beginner's Guide to Vim
Avi Avinav HackerNoon profile picture


Vim, one of the oldest contestants in the text editor war, ever since its release in 1991. It has been a friend to many and a foe to many, wanna find out what it will be for you? Then, let's try it out!


I have been using Vim for about a month and a half now, and I have been loving it (the first week was a bit painful though).


In this article, I'll be discussing why to use Vim, how to set it up, and some basics of Vim.


Why Use Vim?

Now, don't fight with me, I am not comparing Vim with any other text editor. I am just telling you what Vim has to offer:

  • Fully customizable - It means you have full control, some people even make it look like vscode (I personally like to keep it minimal though)
  • Productivity - Well, in the beginning, your productivity would decline but as you start getting used to Vim and its keybindings, it will increase for sure
  • Keyboard-driven (you can use a mouse though if you set the preference) - This may or may not be a good thing for you
  • Looks cool


Neovim

Don't freak out if you haven't heard of Neovim, it's nothing scary. Neovim is just an implementation of Vim but more focused on extensions and plugins, a bit less customizable than Vim. Often people don't say they use Neovim, they just call it Vim.


It really doesn't make much of a difference, just that Neovim has more support for plugins. Also, the default keybindings and commands for both are the same, so, if you learn Neovim keybindings you can as well use Vim.


Installation

Mac & Linux

For Mac & Linux machines, do either of the following depending on your package manager:


sudo apt install neovim


or


brew install neovim


Vim (not Neovim) is installed by default on most Linux machines. You can run it by typing vi or vim.


Just in case, you opened it right now without knowing the keybindings and are now freaking out, just type :q! and press enter to exit.


Windows

For Windows users, you have two options:

  1. You can use wsl (Windows Subsystem for Linux) and follow the same steps as for Linux systems. I won't go into details about installing wsl here.
  2. Use Powershell (not Windows Powershell), you can download it from the Microsoft store, and install Neovim using scoop (A package manager for windows):


To install scoop, type the following into your terminal:


winget install scoop


winget is another package manager that comes pre-installed in windows nowadays.


scoop install neovim


If it tells you to install additional buckets, do it too.


Close and reopen your terminal and you can open Neovim by typing nvim.


Just in case, you opened it right now without knowing the keybindings and are now freaking out, just type :q! and press enter to exit.


First Look

This is how it looks right now:


Right now that doesn't look very good, does it?


Let's configure it!


Configuration

Now, there are many good pre-configured neovim setups out there like NvChad and AstroNvim, but I advise you to first at least try out your own configuration. I personally don't use the pre-configured ones, I prefer to use my own configuration.


You will have to create a configuration file for neovim first.


For Linux & Mac Users, the config file should be created at ~/.config/nvim.


For Windows Users, the config file should be created at ~/AppData/Local/nvim.


(In the above ~ represents the home directory)


Open your terminal in your respective directories. Create the file, its name shall be init.vim. In this file, we will be using vimscript to write our configuration.


Quick note: You can also use init.lua instead of init.vim, more about that towards the end, in this article I will be following with init.vim.


Now, we are going to configure Neovim using Neovim (sounded pretty weird to me the first time).


Basic Rules

Go to the directory mentioned above inside your terminal, and type nvim init.vim and press enter.


Quick note: If you just type nvim without mentioning any file, it will open up something like a welcome page. If you type nvim file.md and file.md does not already exist, Neovim will open it and if you save it the file will be created. The same rules apply to Vim too.


Now, no need to click anything, forget you have a mouse, once that intimidating text editor is open in front of you, just press i (no shift key, no ctrl key, just plain simple little i), this lets you enter insert mode. Now, you can edit your file normally. To exit insert mode or rather any mode in Vim in general, press the escape or ESC key, this doesn't exit the editor just returns you to normal mode (the default mode).


You can also use arrow keys to navigate the file for now.


Quick Note: when in normal mode, to move left press 'h', to move right press 'l', to move up press 'k', and to move down press 'j'.


Before jumping on to plugins let's set up some basic rules/settings:


set number                 " Sets line numbers
set autoindent             " Sets auto indentation
set tabstop=2              " Sets tabstop
set shiftwidth=2           " For proper indentation
set smarttab               " Affects how tab key presses are interpreted
set softtabstop=2          " Control how many columns Vim uses when you hit tab key
set mouse=a                " This lets you use your mouse
set wrap                   " Sets up line wrapping


Quick tip: If you don't understand what a certain setting does, just open Vim and in normal mode type, :help <property_name>, for example for shiftwidth, do :help shiftwidth.


Next, press the ESC key to exit insert mode. Now type :wq (no need for clicking anywhere or pressing all those ctrl & shift keys) and press enter, this will write to the file (basically, it will save the file) and then exits the editor.


If you use just type :w, it just writes to the file, it doesn't exit the editor.


If you use just type :q, it exits the editor, but it won't let you do that if you have unsaved changes, in that case, do :q!, this will revert all unsaved changes and exit the editor forcefully.


If you want a property I haven't mentioned here, just search it up, I am sure you will find it.


Let's set up the plugins now!


Setting Up Plugins

Now, there are a bunch of plugin managers (Vim/Neovim requires plugin managers to install plugins) out there but for this tutorial, we will stick with vim-plug.


For Unix/Linux, you can install vim-plug with:


sh -c 'curl -fLo "${XDG_DATA_HOME:-$HOME/.local/share}"/nvim/site/autoload/plug.vim --create-dirs \
       https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim'


For Windows(Powershell), you can install vim-plug with:


iwr -useb https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim |`
    ni "$(@($env:XDG_DATA_HOME, $env:LOCALAPPDATA)[$null -eq $env:XDG_DATA_HOME])/nvim-data/site/autoload/plug.vim" -Force


Check out their install instructions for more.


Now open init.vim in neovim again, and navigate to the end of the file, you can also use arrow keys, don't press i this time, press o, this will make a new line and enter you to insert mode.


Now type in the following (after the basic rules):


call plug#begin()

" Your plugins go here

call plug#end()


Quick note: You can use " to write single-line comments in vimscript. For example " This is a comment.


All our plugins will go in between the call plug#begin and call plug#end.


Here are some of the plugins I use:

  • coc.nvim - I will elaborate a bit more on this later
  • presence.nvim - a plugin to let you show that you are using Vim on Discord
  • lightline.vim - a light and configurable statusline/tabline plugin for Vim
  • nerdtree - a tree explorer plugin for vim, more about this later
  • nerdtree-git-plugin - a plugin to show git status in nerdtree
  • vim-devicons - a plugin to show icons for your files
  • auto-pairs - completes your parentheses, brackets and quotes
  • copilot.vim - If you have GitHub Copilot, then you can use it with this plugin


You can list your plugins like this:


call plug#begin

Plug 'neoclide/coc.nvim', {'branch': 'release'}
Plug 'andweeb/presence.nvim'
Plug 'itchyny/lightline.vim'
Plug 'ryanoasis/vim-devicons'
Plug 'preservim/nerdtree' |
           \ Plug 'Xuyuanp/nerdtree-git-plugin'
Plug 'jiangmiao/auto-pairs'
Plug 'copilot.vim'

call plug#end


Quick note: Always remember, before you install a plugin, do check out its docs before installing, because sometimes they may have special instructions on the setup for a plugin.


Once it's done, save the file and exit Neovim (remember, use :wq and press enter). Reopen Neovim. To install these plugins, type :PlugInstall in normal mode and press enter. This will open another pane and start installing all the plugins, once it's done. Close the pane (:q), then close init.vim too. This exits Neovim, now, re-enter and all the plugins will start to work.


Quick Tip: to move between panes, press ctrl+w, and then h to move to the left pane and l to move to the right pane


coc.nvim & LSP

What is LSP?

Well, LSP (Language Service Provider) is the thing that gives you language support (language support in an editor means it points out your mistakes and offers suggestions while coding for a specific language). It is also built-in neovim since version 0.5. We won't be configuring the native LSP here, instead, we will use coc.nvim.


What is coc.nvim?

coc.nvim is an ecosystem that builds beyond the native LSP, meaning you don't have to configure anything. It brings autocomplete suggestions and some other stuff. It's recommended for beginners as there is not much setup. We already installed coc.nvim earlier as a plugin.


Well, with that out of the way, we can install the support of coc for our used languages. For example, if you want to install the support for python, just do :CocInstall coc-python. Most languages will have their support as coc-[language-name] but just to be sure you should just google it, you will easily find it.


Keybindings

Now, take a moment and feel proud, you have conquered a bit of the fear of vim.


In vim/neovim, you can set up your own keybindings apart from the ones already there.


For example, let's say I want to set up a keybinding for closing a file in Neovim. So, in general, I would have to type :q.


To do that at the end of your init.vim file (after the call plug#end) add the following:


nnoremap <C-q> :q<CR>


What does this mean?

nnoremap is a declaration that you are setting a keybinding, so you will have to put that before declaring any keybindings.


Here, in <C-q>, the "C-" stands for the ctrl key, followed by whatever key you want to use in combination with it. For example, <C-q> means ctrl+q and <C-k> means ctrl+k.


In between, <C-q> & <CR> is the command for which this key mapping is.


<CR> represents the enter key. So, nnoremap <C-q> :q<CR> means you have to press ctrl+q and it will be the same as typing :q and then pressing enter.


Since, we installed plugins, like nerdtree, they brought their own commands for us, for example, now you have access to :NERDTreeToggle and :CocInstall.


So here are some keybindings you can set up for nerdtree:


nnoremap <leader>n :NERDTreeFocus<CR>
nnoremap <C-n> :NERDTree<CR>
nnoremap <C-t> :NERDTreeToggle<CR>
nnoremap <C-f> :NERDTreeFind<CR>


This keybinding is from their readme on their GitHub repo, but you are free to change it, you can make <C-n> to <C-e> if you prefer, whatever suits you. Just keep in my mind, not to have conflicting keybindings.


You must be wondering though, what is that <leader> over there. That stands for \, so nnoremap <leader>n :NERDTreeFocus<CR>is means you have to type
and it will run:NERDTreeFocus command.


NERDTree

After setting up the keybindings for nerdtree, you can press, ctrl+t to open it up, you can navigate it just like you would a file with arrow key or [h, j, k, l] keys. To delete or rename a file, press m, and it will open up a menu listing out how to do it. To create a file, navigate to the node you want it to be in, and then press m to open up the menu.


That about wraps it up.


Themes

You can install Themes the same way you did with plugins. You can search about the themes, there are many out there. I personally don't use any theme because my terminal's theme is good enough for me.


Don't worry, I have listed some resources for you to find a good theme at the end of this article.


Lua & Vimscript

Earlier, I stated that you could have put your config in init.lua too. Many people prefer to use Lua because it's more logical than Vimscript.


If you wish to convert your init.vim to init.lua, you can check out this article by Olivier Roques.


Final look

If you have followed everything in this article, your init.vim should look something like this now:


" Basic Rules

set number
set autoindent
set tabstop=2
set shiftwidth=2
set smarttab
set softtabstop=2
set mouse=a
set encoding=UTF-8
set wrap


" Plugins

call plug#begin

Plug 'neoclide/coc.nvim', {'branch': 'release'}
Plug 'andweeb/presence.nvim'
Plug 'itchyny/lightline.vim'
Plug 'ryanoasis/vim-devicons'
Plug 'preservim/nerdtree' |
           \ Plug 'Xuyuanp/nerdtree-git-plugin'
Plug 'jiangmiao/auto-pairs'
Plug 'copilot.vim'

call plug#end


" Keymaps

nnoremap <leader>n :NERDTreeFocus<CR>
nnoremap <C-n> :NERDTree<CR>
nnoremap <C-t> :NERDTreeToggle<CR>
nnoremap <C-f> :NERDTreeFind<CR>


This is how it looks now:


Take a deep breath, and feel a sense of accomplishment, you are done. Now, go ahead experiment with it and make Vim your own.


Common Commands in Vim

  • :w - Save file
  • :q - Close file
  • :q! - Force close file and revert unsaved changes
  • :wq - Save and close the file
  • ESC key - Go back from any mode to normal mode
  • i - Enter insert mode
  • o - Next line and insert mode
  • s - delete a character and insert
  • dd - delete line


Here is a vim cheat sheet from devhints. You can check it out for more commands.


Some Tips

  • Do not give up on Vim, at least try it for a week or two before deciding to throw it away. I know it's pretty annoying at first but a lot of people end up loving it after using it.
  • Do not close Vim, don't exit the terminal without quitting Vim, can corrupt files sometimes
  • Do not use ctrl+z, freezes my Vim for some reason
  • You can use shell commands by prefixing things with !
  • Windows may not have some features that Linux will offer but you can install wsl.
  • Increase your typing speed, it will help you out in general too.
  • Use aliases for speeding up your development process.
  • Practice. That's the best way to get used to Vim.


Some Useful Tools

  • Use a directory jumper, so you don't have to keep cding all the time. I use z
  • You can use a terminal multiplexer for Linux and Mac devices. tmux is a famous one and the only one I know. Never heard of terminal multiplexers on Powershell though


More Resources

  • awesome-neovim - A collection of wonderful neovim plugins & themes for you to try
  • r/vim - A wonderful community if you have any questions regarding Vim
  • r/neovim - Similar to r/vim
  • neovim docs - The official documentation for Neovim
  • vim docs - The documentation for Vim
  • nerd fonts - It lets you have more glyphs and font ligatures


If someone wants to point out something that I missed or any other resource or just has doubts about something I wasn't able to explain properly, please comment down below.


Hope you liked it!


Also published here.