Vim - Plug-ins
Vim - Extending Functionality with Plugins
Section titled “Vim - Extending Functionality with Plugins”Plugins are scripts that extend Vim’s built-in features or add entirely new capabilities. The Vim community has created a vast ecosystem of plugins for everything from syntax highlighting and code completion to Git integration and project management. Modern Vim (version 8+) includes a native package management system that simplifies plugin handling.
This chapter covers:
- Understanding Vim’s native package management.
- Installing, updating, and removing plugins.
- Examples of useful plugin categories.
Vim’s Native Package Management (Vim 8+)
Section titled “Vim’s Native Package Management (Vim 8+)”Since Vim 8, there’s built-in support for loading plugins from specific directory structures, eliminating the need for older plugin managers like Pathogen for basic use cases. This system relies on the packpath option (which usually includes ~/.vim on Linux/macOS or ~/vimfiles on Windows).
Plugins are organized into ‘packages’. A package is simply a directory containing one or more plugins. The standard structure is:
~/.vim/pack/{package_name}/start/{plugin_name}/~/.vim/pack/{package_name}/opt/{plugin_name}/{package_name}: A name you choose for a collection of plugins (e.g.,common,colors,myplugins).start: Plugins in astartsubdirectory are loaded automatically when Vim starts.opt: Plugins in anoptsubdirectory are optional and can be loaded manually using the:packadd {plugin_name}command.{plugin_name}: The directory containing the actual plugin files (e.g.,nerdtree,vim-airline).
For Windows, replace ~/.vim/ with ~/vimfiles/ (e.g., C:\Users\YourName\vimfiles\pack\...).
Installing a Plugin
Section titled “Installing a Plugin”Let’s install the badwolf colorscheme plugin as an example. We’ll put it in a package named colorschemes.
- Create the directory structure (if it doesn’t exist):
- On Linux/macOS:
-
$ mkdir -p ~/.vim/pack/colorschemes/start
- On Windows (using Command Prompt):
-
C:\> mkdir "%USERPROFILE%\vimfiles\pack\colorschemes\start"
- Clone the plugin repository into the
startdirectory: Most Vim plugins are hosted on Git repositories like GitHub. -
$ cd ~/.vim/pack/colorschemes/start/
$ git clone https://github.com/sjl/badwolf.git
8. (This creates ~/.vim/pack/colorschemes/start/badwolf/)
That’s it! Because it’s in a start directory, Vim will automatically load it. You can now use the colorscheme:
:colorscheme badwolfYou don’t need to modify your vimrc’s runtimepath for plugins installed this way.
Updating a Plugin
Section titled “Updating a Plugin”If a plugin was installed using Git, updating is straightforward. Navigate to the plugin’s directory and pull the latest changes:
$ cd ~/.vim/pack/colorschemes/start/badwolf$ git pullRemoving a Plugin
Section titled “Removing a Plugin”To remove a plugin, simply delete its directory:
$ rm -rf ~/.vim/pack/colorschemes/start/badwolfPlugin Managers (Alternative)
Section titled “Plugin Managers (Alternative)”While native package management is great, dedicated plugin managers like vim-plug, Vundle, or dein.vim offer more advanced features like easier plugin listing in vimrc, conditional loading, lazy loading, and streamlined update/removal commands. They typically manage plugins by cloning them into a single directory (e.g., ~/.vim/plugged/) and automatically managing the runtimepath.
If you plan to use many plugins or need sophisticated management, exploring a plugin manager is recommended. However, for a few plugins, the native system is often sufficient.
Categories of Useful Plugins
Section titled “Categories of Useful Plugins”The world of Vim plugins is vast. Here are some common categories and examples to give you an idea:
- File Explorers: For navigating your project’s file system.
-
NERDTree: A popular tree explorer.
-
netrw: Vim’s built-in file explorer (enable with:edit .or:Explore).
- Fuzzy Finders: For quickly finding files, buffers, or text.
-
fzf.vim(requiresfzfexternal tool): Extremely fast and versatile.
-
CtrlP: Another popular choice.
- Status Lines & Appearance: Enhance Vim’s status line and overall look.
-
vim-airline,lightline.vim: Powerful status line customizers.
-
- Countless colorscheme plugins (like
badwolfshown above).
- Countless colorscheme plugins (like
- Git Integration: Work with Git repositories without leaving Vim.
-
fugitive.vim: A comprehensive Git wrapper.
- Code Completion & Language Support (LSP):
-
- For advanced autocompletion, linting, and language-specific features, Language Server Protocol (LSP) clients are common.
-
coc.nvim: A powerful LSP client (works in Vim and Neovim).
-
- Simpler completion plugins like
vim-mucompleteor language-specific plugins exist.
- Simpler completion plugins like
- Snippets: Insert common code blocks quickly.
-
ultisnips(requires Python),neosnippet.vim.
Practical Application: Plugins transform Vim from a powerful text editor into a tailored development environment. For example, a web developer might install plugins for HTML/CSS/JavaScript syntax, autocompletion, a file explorer, and Git integration to streamline their workflow.