Skip to main content

Top five Vim plugins for sysadmins

My list of the top five Vim plugins—plus a bonus.
Image
Top 5 Vim plugins for sysadmins

Photo by Castorly Stock from Pexels

Last year I wrote the article 5 useful Vim plugins for developers for Opensource.com. This article follows a similar idea but focuses on plugins that make sysadmins more productive, regardless of what scripting, programming language, or frameworks they use. There's another class of Vim plugins focused on code completion and syntax checking that are intentionally not covered in this article. They will be the topic for another post.

Also, this article does not provide installation instructions for these plugins. You can use Vim 8's native package manager or your favorite plugin manager. If you want a no-frills plugin manager for Vim, take a look at Pathogen. It provides a standard way to install plugins by cloning their repositories directly under the .vim/bundle directory with automatic help generation.

1. Vim Airline

Vim Airline is a plugin that replaces the standard Vim status line with a fancy and useful status bar. Vim Airline is convenient and presents helpful information about the file you're working on, including file name and save status, Vim mode, file type, encoding, position, word count, and more.

Vim Airline is fast and lightweight. It displays relevant information without compromising Vim's performance. Using Vim Airline improves your productivity by providing information that would require additional time to obtain at a glance.

After installation, Vim Airline enables a basic configuration that does not require any changes to Vim's configuration files. Here is the standard status bar with Airline:

Image
The standard Airline status bar

It integrates with other plugins natively to present additional information. For example, if you have Vim Gitgutter (see #4 Vim Gitgutter below) or Vim Fugitive (see #5 Vim Fugitive below), Airline also displays Git-related information, such as branch name and diff status.

Image
Airline displaying Git-related information, including branch name and diff status

To use special icons, such as the Git branch icon, install the Powerline fonts package and enable it with Vim Airline. Then add this line to your .vimrc configuration file:

let g:airline_powerline_fonts=1

In addition to the status bar, Vim Airline optionally displays a tab line at the top containing the name of all open tabs or buffers:

Image
Airline displaying a tab line containing all open tabs or buffers

To enable this feature, add this line to .vimrc:

let g:airline#extensions#tabline#enabled = 1

Finally, Vim Airline provides themes to give additional flexibility to the status bar functionality and appearance. For more information, check the repository vim-airline-theme.

For more information about this plugin and additional options that may be useful for your use case, consult the official repository.

2. NERDTree

NERDTree is a filesystem explorer for Vim that allows you to browse the directory structure, helping you find and manage files and directories directly within Vim's interface. NERDTree is a replacement for Vim's native file explorer netrw. NERDTree includes additional features and sensible defaults, making it easier to get started without adding any configuration to your .vimrc file.

After installing, you can use NERDTree by typing the command :NERDTree. It opens up as a side window with the current directory selected. To close it, you can type the command :NERDTreeClose. You can also use :NERDTreeToggle to switch between open and closed windows. You can even map this command to another key to make it easier to open and close NERDTree. For example, to use the function key F5 to control NERDTree, add this mapping to your configuration file:

map <F5> :NERDTreeToggle<CR>

Now press F5 to open NERDTree:

Image
NERDTree basic display

With NERDTree open, you can navigate the directory structure using the Vim navigation keys j and k. Press Enter on a directory to expand or collapse it. By default, NERDTree shows the directories in a tree format. Press Enter on a regular file to open it using the original window. You can also press i to open the file using a horizontal split or s for a vertical split. Press ? to see the help with a complete list of commands.

Another useful function is pressing m to see the menu. You can then use NERDTree to execute filesystem operations such as creating, renaming, copying, and moving files and directories:

NERDTree Menu. Use j/k/enter, or the shortcuts indicated
=========================================================
> (a)dd a childnode
  (m)ove the current node
  (d)elete the current node
  (r)eveal the current node in file manager
  (o)pen the current node with system editor
  (c)opy the current node
  print (p)ath to screen
  (l)ist the current node

NERDTree improves your productivity by allowing you to find and manage files within Vim without requiring access to the command line or another file system explorer application.

[ You might also enjoy Linux basics: A beginner's guide to text editing with vim ]

3. Fzf Vim

NERDTree is a great plugin to help you navigate the directory structure and find files to work in Vim. However, it's not helpful if you have a large directory structure or if you're looking for a file without knowing exactly where it's located. For such cases, a fuzzy finder comes in handy.

fzf.vim is a plugin that wraps the command-line fuzzy finder program fzf, allowing you to use it directly within Vim. Fzf is a fast and portable fuzzy finder application written in Go. It is a requirement for the Vim plugin, so make sure you install it first.

In addition to Fzf, you can install the program bat to enable syntax highlighting in the preview window displayed by Fzf.

By default, fzf.vim opens the search window at the bottom of the screen. To change this and move the window to the middle, increasing the size of the preview window, add these two lines to your .vimrc configuration file:

let g:fzf_preview_window = 'right:50%'
let g:fzf_layout = { 'window': { 'width': 0.9, 'height': 0.6  }  }

Now, run a fuzzy search with all files in the current directory by typing the command :Files. fzf.vim opens a window listing all available files:

Image
fzf.vim displaying a window listing all available files

To filter the available options, start typing any characters from any part of the file name, extension, or directory. Fzf automatically filters the results for you. For example, if you remember the file you want is a "YAML" file, start typing y then m and see how the results change:

Image
Search for a &quot;YAML&quot; file by typing y then m with changing results

Notice that it does not require the characters to be together or in the same order you typed. This is the goal with fuzzy finding. When you're happy with the filter, you can use the arrow keys to select and preview the remaining entries and press Enter to open the desired one. You can also use Ctrl+T, Ctrl+X, or Ctrl+V to open the file in a new tab, split, or vertical split, respectively.

Using fzf.vim to search for files is a great help. However, fzf.vim can do much more than locate files. It wraps the fzf command with other Vim features or other external commands, allowing you to search for more than files in the current directory structure.

For example, use the command :Buffers to search a list of current open buffers. With :GFiles, fzf.vim lets you search all files managed by the current Git repository. The command :Lines allows you to run a fuzzy search in all lines from every loaded buffer. Pressing Enter takes you to the specific line in the correct buffer:

Image
Press Enter to go to the specific line in the correct buffer

fzf.vim even allows you to search for Vim keybindings with the command :Maps. If you forgot to which keybinding you mapped a specific command, start fzf.vim, type a few letters of the command, and it shows you the mapped keybinding. Pressing Enter on the entry executes the command. For example, to find which key we mapped for Vim NERDTree in Vim NERDTree (see #2 NERDTree above):

Image
Find which key was mapped for Vim NERDTree

Fzf is fast, and the fzf.vim plugin's integration with Vim makes it an excellent tool to assist with finding files and other items. This plugin has many other features. Make sure to consult the help by typing the command :h fzf for a complete list.

4. Vim Gitgutter

The plugin Vim Gitgutter is useful for those using Git as a version control system. It shows the output of Git diff as symbols in the "gutter"—the sign column where Vim presents additional information, such as line numbers. For example, consider this Ansible task file as the current committed version in Git:

  1 ---
  2 # tasks file for mywebapp
  3 
  4 - name: Include OS-specific variables.
  5   include_vars: "{{ ansible_os_family }}.yaml"
  6  
  7 - name: Install http package
  8   yum:
  9     name: "{{ httpd_package }}"
 10 
 11 - name: Ensure httpd svc started
 12   service:
 13     name: "{{ httpd_service }}"
 14     state: started
 15     enabled: yes

After making some changes, Vim Gitgutter displays the following symbols in the gutter:

    1 ---
    2 # tasks file for mywebapp
    3 
    4 - name: Include OS-specific variables.
    5   include_vars: "{{ ansible_os_family }}.yaml"
    6 
~   7 - name: Ensure httpd package installed
    8   yum:
    9     name: "{{ httpd_package }}"
+  10     state: present
   11 
   12 - name: Ensure httpd svc started
   13   service:
   14     name: "{{ httpd_service }}"
_  15     state: started

The - symbol shows that a line was deleted after line 15. The ~ character shows that line seven was modified, and the + sign indicates that line ten was added.

In addition, Vim Gitgutter allows you to navigate between "hunks"—individual changes made in the file—with [c and ]c, or even stage individual hunks for commit by pressing Leader+hs.

Vim Gitgutter also integrates automatically with Vim-Airline, providing a summary of changes in the status line:

Image
Vim Gitgutter integrates with Vim-Airline, showing a summary of changes in the status line

Vim-Airline enables this configuration by default, and you don't need to add anything to your .vimrc file to make it work.

Vim Gitgutter provides immediate visual feedback for changes, and it's a great addition to your toolbox if you use Git.

5. Vim Fugitive

Vim Fugitive is another great plugin for those trying to incorporate Git into the Vim workflow. This plugin is a Git wrapper that allows you to execute Git commands directly from Vim's interface. This plugin has many features, so make sure to check the Github page for additional information.

Here's a basic Git workflow example using Vim Fugitive. Consider the Ansible tasks file we used as an example in Vim Gitgutter (see #4 Vim Gitgutter above). You can use git blame in Vim by typing the command :Gblame:

7d960351 (Ricardo Gerardi   2020-09-29 14:09:59 -0400)|    1 ---
7d960351 (Ricardo Gerardi   2020-09-29 14:09:59 -0400)|    2 # tasks file for mywebapp
7d960351 (Ricardo Gerardi   2020-09-29 14:09:59 -0400)|    3
7d960351 (Ricardo Gerardi   2020-09-29 14:09:59 -0400)|    4 - name: Include OS-specific variables.
7d960351 (Ricardo Gerardi   2020-09-29 14:09:59 -0400)|    5   include_vars: "{{ ansible_os_family }}.yaml"
7d960351 (Ricardo Gerardi   2020-09-29 14:09:59 -0400)|    6
00000000 (Not Committed Yet 2020-09-29 17:40:03 -0400)|~   7 - name: Ensure httpd package installed
7d960351 (Ricardo Gerardi   2020-09-29 14:09:59 -0400)|    8   yum:
7d960351 (Ricardo Gerardi   2020-09-29 14:09:59 -0400)|    9     name: "{{ httpd_package }}"
00000000 (Not Committed Yet 2020-09-29 17:40:03 -0400)|+  10     state: present
7d960351 (Ricardo Gerardi   2020-09-29 14:09:59 -0400)|   11
7d960351 (Ricardo Gerardi   2020-09-29 14:09:59 -0400)|   12 - name: Ensure httpd svc started
7d960351 (Ricardo Gerardi   2020-09-29 14:09:59 -0400)|   13   service:
7d960351 (Ricardo Gerardi   2020-09-29 14:09:59 -0400)|   14     name: "{{ httpd_service }}"
7d960351 (Ricardo Gerardi   2020-09-29 14:09:59 -0400)|_  15     state: started

You can see who committed all lines, except lines seven and ten, which have not been committed yet. Now, check the repository status by typing :Gstatus:

  1 Head: master
  2 Push: origin/master
  3 
  4 Unstaged (1)
  5 M main.yaml
~
___________________________________________________________________________________________
~   7 - name: Ensure httpd package installed
    8   yum:
    9     name: "{{ httpd_package }}"
+  10     state: present
   11 
   12 - name: Ensure httpd svc started
   13   service:
   14     name: "{{ httpd_service }}"
_  15     state: started

Vim Fugitive opens a split window with the result of git status. You can stage a file for commit by pressing the - key on the line with the file's name. You can reset the status by pressing - again. The message is updated to reflect the new status:

  1 Head: master
  2 Push: origin/master
  3 
  4 Staged (1)
  5 M main.yaml

Notice that the status on line four changed from Unstaged to Staged. Now you can use the command :Gcommit to commit the changes. Vim Fugitive opens another split that allows you to enter a commit message:

  1 Update Ansible tasks file
  2 # Please enter the commit message for your changes. Lines starting
  3 # with '#' will be ignored, and an empty message aborts the commit.
  4 #
  5 # On branch master
  6 # Changes to be committed:
  7 #       modified:   main.yaml
  8 #

Save the file with :wq to complete the commit. Fugitive displays a confirmation message with the new commit hash:

[master f7908cf] Update Ansible tasks file
 1 file changed, 2 insertions(+), 2 deletions(-)
Press ENTER or type command to continue

You can use :Gstatus again to see the result and :Gpush to update the remote repository with the new commit:

  1 Head: master
  2 Push: origin/master
  3
  4 Unpushed to origin/master (1)
  5 f7908cf Update Ansible tasks file

If you like Vim Fugitive and want to learn more, the official repository has links to screencasts showing additional functionality and workflows.

Bonus: Vim Floaterm

Vim Floaterm is a plugin that allows you to use the Vim 8 terminal feature as a popup window.

Sometimes you have to type commands in a terminal to complete your tasks. Before Vim 8 provided the :terminal feature, the solution was switching to another screen or exiting Vim to use the command line. With the built-in Vim terminal, you can open a terminal window directly from Vim. However, this window takes part of your editing area or is too small, and you need to change it constantly.

Vim Floaterm solves these issues by creating the terminal as a floating window that you can show or hide whenever you need to use it. Use the command :FloatermNew to create a new floating terminal and :FloatermHide to hide it. You can also use :FloatermToggle to toggle between the states. A floating terminal looks like this:

Image
A Floaterm floating terminal

To make it easier to show and hide the floating terminal, you can map the command to another key. For example, to use the function key F12 to control Floaterm, add this configuration to your .vimrc file:

let g:floaterm_keymap_toggle = '<F12>'

You can also increase the size of the floating terminal by providing the width and height as a percentage of the Vim screen size with these two configuration lines:

let g:floaterm_width = 0.9
let g:floaterm_height = 0.9

This configuration creates a floating terminal that takes 90% of Vim's screen size. Floaterm provides many additional features and integration with other plugins and command-line tools. Consult the help with :h floaterm-contents or the official repository for more information.

[ Want to test your sysadmin skills? Take a skills assessment today. ] 

What's next?

In this article, we reviewed five plugins—plus a bonus—that help sysadmins improve their workflows and increase their productivity when using Vim.

Vim is a superb editor that provides a number of features out of the box. Using plugins adds new features, making it easier to fit Vim to specific workflows.

The plugins in this article focus on general functionality, such as locating files, providing information, and dealing with version-controlled files in Git. There are other classes of plugins to manage different aspects of Vim configuration. In the next article, we'll talk about code completion and syntax checking.

Special thanks to my colleague David Ward for introducing the plugins fzf.vim and Vim Floaterm to me.

Check out these related articles on Enable Sysadmin

Topics:   Linux   Linux Administration   Visual editor  
Author’s photo

Ricardo Gerardi

Ricardo Gerardi is a Senior Consultant at Red Hat Canada where he specializes in IT automation with Ansible and Openshift. More about me

Related Content

OUR BEST CONTENT, DELIVERED TO YOUR INBOX