Barbarian Meets Coding
barbarianmeetscoding

WebDev, UX & a Pinch of Fantasy

10 minutes readwizardsusevim

Wizards Use Vim: Getting Started With Vim

Excellent! Now that you are aware of the most common pitfalls on the path to Vim mastery let’s get yourself setup. In this chapter you’ll learn how to get started with a vanilla development environment in Vim that you can use and improve throughout the rest of our journey.

There are different paths that you can follow to get started with Vim, and each corresponds with different factions within The Order of Vim. Each with their own idiosyncrasies and approaches to using Vim, some of them purists, some others pragmatists, but all tied by the love and awesomeness of Vim.

The factions are in constant turmoil, some will rise, some will perish and others just remain through the ages. These are the most significant ones today:

The Factions

The High Arcanists of Vim represent the biggest and most ancient faction, filled with its own subfactions and groups. They are the followers and users of the original version of Vim, kicking ass since 1991. It is the most stable choice, it can run on a terminal and it provides support for a very thin GUI layer via GVim and MacVim. It’s actively developed under the direction of The Benevolent Supreme Archanist Bram Moolenaar and saw the latest significant release in May 2018 with Vim 8.1 and support for an integrated terminal. You can follow their exploits in the vim mailing list.

The Neovim Witches and Warlocks, a faction which broke away from the High Arcanists with a vision in mind. To refactor the original Vim and make it more maintanible and approachable to new contributors. And thus came Neovim. Neovim has risen as a modernized Vim alternative faithful to the ideas and essence of the original. Neovim itself only supports a text interface but it is designed as a platform that can be consumed by other graphical editors (such as Visual Studio Code or Oni). It’s actively developed on GitHub.

The Bards, Filis and Skalds who prefer the pragmatism of using Vim Plugins in modern editors are a minor faction. Vim is so awesome that lots of other popular editors support some kind of Vim-like mode. This is a popular choice for software developers that value the productivity boost of using Vim but enjoy the great out-of-the-box experience of modern popular editors. Some examples of editors that support this type of hybrid setup are:

The Oni Alchemists are a very obscure minor faction whose members experiment to achieve the impossible: Combining the best parts of Vim and modern editors into a single editor Oni to rule them all. Oni is a graphical editor built with React and TypeScript which uses Neovim behind the scenes to give you all the power of Vim but still aims to provide a great out-of-the-box experience. At present it is in very early stages but I think it is worthy to be aware that it exists and to keep an eye out for how it develops.

Your Vim Setup Throughout This Book

In this book we are going to focus in the core features of Vim and build a strong foundation that you yourself can build upon. And for that, the best approach is to use a vanilla version of Vim stripped of any plugins and extraneous configurations. So I recommend you to use either Vim or Neovim for the remainder of the book.

A great combination to follow as you embark in this journey is to use:

  1. Vim or Neovim to learn more about Vim as you read the book
  2. And combine that with a Vim plugin in your editor of choice so that you can go slowly incorporating what you learn in a real world scenario

That’s not only great practice but it will mean that you can start reaping the benefits and seeing an improvement in your day to day development from day one.

But Why then use Vim to learn instead of a Vim plugin? There’s a couple of reasons:

  1. I can expect Vim to have all the things I want to teach you in this book whereas plugins usually have a limited set of features which differs in each platform.
  2. With Vim you have access to the invaluable :help command, powerful oracle of Vimness and keeper of its most concealed secrets.

So Vim or Neovim? That is the question. Vim and Neovim are highly compatible, so much so that they can use the same configuration files and the same plugins. You’re welcome to use any of them but in this book we’ll use Neovim. That’s because I myself am fiery Warlock, and because of the following reasons:

  1. Neovim comes with better defaultsbetter-defaults out-of-the-box which is nicer for first-time users because it means that we don’t need to spend as much time setting things up.
  2. Neovim comes with a nicer VimTutor that you can run from within Neovim itself using the :Tutor command. This improved version of the vimtutor also gives you actual visual feedback when you complete the exercises. Moreover, this new tutoring feature called vim-tutor-mode is a framework for creating new Vim tutorials that can be used even by plugin writers to write tutorials for their plugins.
  3. Neovim comes in a single package with everything you need whereas with Vim you need to specify which feature set you want at compile time. For instance, it is highly likely that the version of Vim that comes with your computer is outdated and doesn’t have all the features you’ll need.
  4. Neovim has this nifty feature called :checkhealth that helps you troubleshoot your Vim setup. It has built-in checks for user configuration and performance, and plugins can hook up to it and provide their own checks. This is great for first-time users and veterans alike as dealing with configuration problems can be a true pain in the ass.

But, of course, you are free to choose the path that you want and I’ll never know (or will I?).

Setting Up Neovim

Setup Neovim by following these steps:

  1. Install Neovim in your OS of choice (see expanded instructions below)
  2. Setup a minimal Vim configuration
  3. Open Neovim by typing nvim in the terminal
  4. Victory! Start Typing!

Install Neovim on Mac OSX

The simplest way to get Neovim in OSX is to use Homebrew:

$ brew install neovim

Install Neovim on Ubuntu or Debian

To install the latest version of Neovim in Ubuntu or Debian use the following:

$ sudo apt-get update
$ sudo apt-get install software-properties-common -y
$ sudo add-apt-repository ppa:neovim-ppa/stable
$ sudo apt-get update
$ sudo apt-get install neovim -y

Install Neovim on Windows

The simplest way to install Neovim on Windows is to use the Chocolatey package manager. Type:

PS> choco install neovim -y

Install Neovim on Other Operative Systems

The Neovim Wiki has thorough instructions on how to install Neovim in lots and lots of operative systems, using a package manager or building from source. If your operative system is not one of the above, then please teleport to Neovim’s Wiki and follow the instructions.

A Super Minimal And Optional Vim Configuration

Since we will be using Neovim which has a nice set of defaults from the get-go we could start using it right away. Nonetheless, there are a small number of optional additional configurations that will make Neovim user experience even better.

In order to configure Vim in a persistent fashion you need to create a configuration file (commonly referred to as vimrc). In Neovim this file is located at:

  • For Unix based systems in: ~/.config/nvim/init.vim
  • For Windows in: ~/AppData/Local/nvim/init.vim

Create your vimrc, paste the following configuration and restart Neovim:

""" Tab Completion
set wildmode=list:longest " Setup Tab completion to work like in a shell

""" Search
set ignorecase   " case-insensitive search
set smartcase
" but case-sensitive if expression contains a capital letter

""" Buffers
set hidden       " Handle multiple buffers better
" You can abandon a buffer with unsaved changes without a warning

""" Terminal
set title        " show terminal title

""" Editor
set scrolloff=3  " show 3 lines of context around cursor
set list         " show invisible characters

""" Global Tabs and Spaces configurations
set expandtab    " use spaces instead of tabs
set tabstop=2    " global tab width
set shiftwidth=2 " spaces to use when indenting

Can’t Stand The Default Colorschemes?

I’ve found that a nice color scheme has a huge impact in how comfortable newcomers are with Vim. If you care about aesthetics then try installing this popular color scheme based on Atom’s One Dark and One Light color schemes: vim-one

Start by creating a new package to add this plugin in your Neovim configuration folder as follows:

$ mkdir ~/.config/nvim/pack/wizardsusevim/start && cd "$_"

This will create a new package inside your Neovim configuration folder and will also change your current directory to the one you’ve just created.

Now use git to clone the onedark GitHub repo inside that folder:

$ git clone https://github.com/rakr/vim-one

This is the equivalent to downloading the color scheme inside that package that you’ve just created.

Now run Neovim and you should be able to set the new colorscheme typing the following command:

:coloscheme one

To make the new color scheme persistent across sessions of Vim update your vimrc by adding the following configuration:

" Set the colorscheme to onedark
colorscheme one

" Set whether you want the dark or light colorscheme
" The dark one is much better :D
set background=dark " for the dark version
" set background=light " for the light version

" Use 24-bit color
set termguicolors

In later chapters, we’ll dive deeper into how to install and update plugins such as this one using a plugin manager. Plugin managers are way more convenient than creating folders and cloning repos by hand. But for now, this will do. Enjoy your colors!

Exercise Apprentice of Vim!

Oh yeah! This book is going to have exercises. There’s nothing more conductive to learning than doing. So let’s do.

For starters I want you to:

  1. Run the vimtutor. If you opted for Vim type vimtutor in your terminal. If you chose Neovim type :Tutor.

Would you Like To Learn More and Edit Text Like a Wizard? Get the Book!

This is a weird programming book. On one hand it is a awesome book on the superb vim text editor. On the other a spell book of sorts set in a world of fantasy where some people can wield JavaScript to affect the world around them. To essentially program the world and bend it to their will.

Welcome to the world of JavaScript-mancy, may you enjoy your stay, learn and have a lot of fun.

Draft 3 Wizards Use Vim Cover

From Vim 8.0 onwards if you don’t have a vimrc Vim will load a default configuration that is friendlier to beginners (like enabling syntax highlihting). You can find more information via :h default.vim. This is another great example which shows that competition has improved Vim.



Jaime González García

Written by Jaime González García , dad, husband, software engineer, ux designer, amateur pixel artist, tinkerer and master of the arcane arts. You can also find him on Twitter jabbering about random stuff.Jaime González García