As a developer, the editor you use is a vital part of your toolkit – it deserves customization and refinement to perfectly fit your needs for a productive coding environment.

In this comprehensive 4000 word guide, we will fully unlock Vim‘s potential by installing plugins and crafting a tailored configuration. By the end, you‘ll know how to customize Vim to boost your productivity as a developer.

Checking Prerequisites

Before we start modifying Vim, let‘s go over some prerequisites:

Check Vim Version

We need a sufficiently recent Vim version to ensure full compatibility with the latest plugins. Check your version by:

vim --version

Aim for Vim 8.0+ installed. On Linux, Mac OSX will generally provide a compatible system Vim.

For Windows, you may need to install Vim separately. Make sure to get the latest release, not Vim 7.x.

Required Compilers

Many Vim plugins contain native extensions that require compilation. So we need the necessary compiler toolchain installed:

  • GCC – for compiling C based extensions
  • Python – for plugins with Python extensions
  • Node.js – for Javascript based plugins
  • Go – (optional) for Go based plugins

On Debian/Ubuntu:

sudo apt install build-essential python3-dev nodejs

RHEL/CentOS:

sudo yum groupinstall ‘Development Tools‘ 
sudo yum install python3-devel nodejs

Arch Linux:

sudo pacman -S base-devel python nodejs 

Package Manager

We‘ll also utilize the system package manager for installing any plugin dependencies:

Popular package managers:

  • apt (Debian/Ubuntu)
  • yum (RHEL/CentOS)
  • pacman (Arch Linux)
  • Homebrew (Mac OSX)
  • chocolatey (Windows)

So first update packages:

# apt systems
sudo apt update

# yum systems 
sudo yum update 

# etc

With Vim verified and tools in place, we are ready to proceed!

Plugin Managers

While we could install Vim plugins manually, using a plugin manager simplifies things tremendously:

Benefits

  • Automates installation, updating and removal
  • Handles dependency resolution
  • Organizes plugins systematically
  • Integrates cleanly with Vim

The most popular plugin managers are:

Here is an overview comparison:

Plugin Manager Installation Speed Interface Dependencies Notes
Vim-Plug Single File Very Fast Declarative Supported Actively maintained, simple config
Vundle Git Repo Fast Config Style Supported Mature & popular, slightly complex config
Dein Git Repo Very Fast Mix Supported Written in Vimscript, robust features
Pathogen Git Repo Fast Imperative Not Handled Basic features only

I recommend Vim-Plug for its simplicity. But Vundle and Dein are also excellent choices.

Let‘s install Vim-Plug:

curl -fLo ~/.vim/autoload/plug.vim --create-dirs \
    https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim

This downloads plug.vim into the standard location.

Editing vimrc

Now we can configure Vim to load the plugin manager.

Open vimrc with:

vim ~/.vimrc

And add these lines:

call plug#begin(‘~/.vim/plugged‘)

" Put plugin installations here

call plug#end()

This tells Vim to source plug.vim and designates ~/.vim/plugged as the plugin install location.

We‘ll add plugins to install between plug#begin and plug#end.

Essential Plugin List

Let‘s install some essential plugins to enhance our environment.

1. File Explorer: NERDTree

Browsing files and directories is vital for web development and editing projects. The NERDTree plugin provides a sidebar file explorer, akin to IDEs like VS Code and PyCharm.

Plug ‘scrooloose/nerdtree‘

NERDTree offers extensive features like:

  • Directory tree navigation
  • Opening/closing files and directories
  • Support for git status icons
  • Filesystem manipulation through move/delete commands

2. File Finder: CtrlP

Manually browsing to open files can be slow, especially on large projects. CtrlP provides intelligent fuzzy finding for rapid file opening.

Plug ‘ctrlpvim/ctrlp.vim‘

CtrlP has many options:

  • Text based search to quickly target files
  • Regex powered filtering
  • Most recently used files list

Together with NERDTree, we have comprehensive project navigation.

3. Status Line: Vim-Airline

While Vim shows the current mode in the status bar, vim-airline improves this substantially for greater polish and visibility.

Plug ‘vim-airline/vim-airline‘

vim-airline configurations include:

  • Configurable themes
  • Section toggling
  • Buffer & encoding info
  • Git branch integration

The refined status line increases Vim‘s aesthetics.

4. Git Integration: Vim-Fugitive

Since Git is ubiquitous, Vim should integrate with it. Vim-fugitive adds extensive Git functionality like:

Plug ‘tpope/vim-fugitive‘

Key features:

  • View file diff with Gdiff
  • Open the git commit window via Gcommit
  • See git status output with Gstatus
  • Log viewing, staging hunks, blaming and more!

No more jumping to the terminal for common Git operations.

5. Snippet Support: UltiSnips

While Vim has omni-completion, writing boilerplate code is tedious. Snippets enhance productivity by expanding placeholder tags into full code blocks.

For example, for<tab> could expand into:

for (let i = 0; i < array.length; i++) {

}

Saving keystrokes.

Plug ‘SirVer/ultisnips‘

UltiSnips has:

  • Powerful Python based snippet engine
  • Visual Placeholder interfaces
  • Flexible tabstops and format specifiers
  • Ships with snippets for many languages

6. Autocompletion: Coc.nvim

Vim‘s autocompletion is limited compared to IDEs. The Coc engine upgrades completion with:

  • As-you-type suggestions
  • Documentation previews on hover
  • Go to definition
  • Powered by Language Servers
Plug ‘neoclide/coc.nvim‘, {‘branch‘: ‘release‘}

We‘ll configure Coc for our coding languages below.

Linting, Debugging and more

Additionally useful plugins:

  • dense-analysis/ale – Asynchronous linting
  • puremourning/vimspector – Debugger integration
  • itchyny/lightline.vim – Statusline
  • junegunn/fzf – Fuzzy finder
  • jiangmiao/auto-pairs – Auto insert brackets and quotes

Refer to Awesome Vim Plugins for more great plugins!

With essentials covered, save vimrc and lets install plugins via Vim-Plug.

Installing Plugins using Vim-Plug

From inside Vim run:

:source %  " Reload vimrc
:PlugInstall " Install configured plugins

This clones the plugin repositories into ~/.vim/plugged then installs file as needed.

Repeat :PlugInstall whenever you add new plugins.

Configuring installed plugins

Most plugins work out of the box but may require tweaking for preferences or extra functionality.

Let‘s configure selected plugins:

NERDTree

Map NERDTree toggle to ,e:

map ,e :NERDTreeToggle<CR>

Show hidden files:

let NERDTreeShowHidden=1

Open NERDTree automatically on certain directories:

autocmd StdinReadPre * let s:std_in=1
autocmd VimEnter * if argc() == 0 && !exists("s:std_in") | NERDTree | endif

UltiSnips

Enable snippets for languages:

let g:UltiSnipsSnippetDirectories=["UltiSnips", "python-snippets"] 

Key maps:

imap <c-e> <plug>(ultisnips_expand)
smap <c-e> <plug>(ultisnips_expand)

Vim-Fugitive

Set status column for Gstatus:

set statuscolumn=80

Use ,gs mapping for Gstatus:

nmap ,gs :Gstatus<CR>

Coc.nvim

Configure language servers:

" Python
let g:coc_global_extensions = [‘coc-python‘]

" Javascript/Typescript
let g:coc_global_extensions += [‘coc-tsserver‘, ‘coc-eslint‘, ‘coc-prettier‘]  

Refer to plugin docs for more specific configuration.

Now for some general Vim productivity enhancements.

Boosting Productivity with Vim Options

Beyond plugins, tweaking Vim options is vital for productivity:

Display relative line numbers:

set number relativenumber

Highlight searches:

set hlsearch

Enable mouse mode for scrolling and resizing:

set mouse=a

Show matching brackets/parenthesis:

set showmatch

Use case insensitive search unless capital letters are used:

set ignorecase smartcase

For JavaScript, show 4 spaces with tabs:

autocmd Filetype javascript setlocal shiftwidth=4 tabstop=4

And many more tweaks are possible – explore options with :help {option}.

Some colorscheme options:

" Enable syntax highlighting 
syntax enable  

" Set editor theme
let g:onedark_terminal_italics=1
colorscheme onedark

" Set airline theme
let g:airline_theme=‘onedark‘

Themes affect the look – pick visually pleasing ones!

With good options, using Vim is already more pleasant. Lets dive deeper into understanding plugins.

Understanding Vim‘s Plugin Architecture

To effectively manage plugins long term, understanding how Vim structures and loads plugins is helpful.

Here is a simplified overview:

  • Vim maintains script autoload directories like ~/.vim/autoload
  • On startup, Vim sources ~/.vimrc
  • Plugins place executable code (vimscript/python/ruby) under autoload
  • The runtimepath lists directories Vim searches
  • As plugins load, they append to Vim features and global state
  • Functions like :PlugInstall pull in new plugins

Specifically, the runtimepath handles plugin inclusion:

set runtimepath^=~/.vim runtimepath+=~/.vim/after
let &runtimepath.=‘,~/.vim/plugged‘

Paths prefixed with ^ load first, those appended with $ load last.

So plugin managers prepend their path, then tweak order with after/ prefixes.

Understanding runtimepath helps debug load issues.

Additionally, learn Vimscript for advanced configuration – Custom commands, autocmd events, key mappings etc.

Troubleshooting Plugin Issues

Plugin issues manifest as:

  • Feature not available/working
  • Runtime errors on startup
  • Performance slowdowns
  • Functionality conflicts between plugins

Try these troubleshooting steps:

  • Narrow down errors via binary search by disabling plugins halves
  • Check runtimepath order for path issues
  • Toggle verbosity with vim -V20 for activation logs
  • Review documentation for plugin usage instructions
  • Search github issues for commonly reported issues
  • Try updated versions if resolvable bugs exist
  • As last resort uninstall/reinstall problematic plugin

Learning to debug plugin problems will prevent headaches!

Vim 8 Native Package Support

So far we have used Vim-Plug to manage plugins. However, since Vim 8, built-in package management is also available.

Benefits include:

  • Tight version locking
  • Isolation from runtimepath
  • Configuration encapsulation
  • No dependencies on git/external tools
  • Native performance

Packages use isolated directories like ~/.vim/pack/vendor/start/{plugin} avoiding runtimepath clutter.

Managing packages requires using :packadd {plugin-name} instead of runtimepath changes.

Learn more with :help package.

For now, continue using Vim-Plug until the ecosystem standardizes on native packages. But good to know for future Vim versions.

Organizing configurations

As vimrc grows with plugins and tweaks, keeping things tidy is valuable:

Suggestions:

  • Modularize configurations relating to a topic into self contained Vimscript files
  • Have a small vimrc act as index importing configurations files
  • Prefix related settings with common tag like g:webdev_configs
  • Use autoload for complex plugin configuration requiring vim boot completion
  • Utilize after directories like ~/.vim/after/plugin for ordering

Example structure:

~/.vimrc
~/.vim/autoload/plug.vim
~/.vim/plugins.vim
~/.vim/settings/
~/.vim/settings/basic.vim
~/.vim/settings/formatting.vim 
~/.vim/settings/mappings.vim

Keep things logically organized!

Conclusion

And with that we‘ve built a customized powerhouse Vim setup, installing plugins for enhanced functionality while tailoring configurations for productivity.

Some takeaways:

  • Use a plugin manager like Vim-Plug for easy plugin handling
  • Methodically install plugins improving upon Vim weaknesses
  • Tweak options optimizing interface ergonomics
  • Understand runtimepath for debugging plugin issues
  • Organize vimrc and configs using conventions for maintainability

Learning to effectively customize Vim will boost both your productivity and enjoyment. This was just a sample of possibilities – explore more plugins and keep tweaking to build your ideal editing environment!

Similar Posts