Trick out your Vim Editor

vim-iconI’ve been a long-time vim user, dating back to when I figured out how to exit the damn thing.  Over the years I’ve tried various vimrc configurations, plugins and looks and even other editors but I’ve always come back to it for a few reasons: the main one being that you’re guaranteed it will be installed on any server you touch.  I’m sharing my vim setup here and how to use the same one if you like it.

 

Why Customize?
If you dabble in any development or write ops tools you’ll most likely want language-specific syntax highlighting, buffers, git support and some extended functionality.  The plugin architecture and availability of plugins are very plentiful if not downright overwhelming.  Here are the plugins I use currently:

How to Try this Setup
To quickly get the same setup as me you’ll need to run the following commands.  I have provided a tarball of my ~/.vim and ~/.vimrc that already have the above plugins setup and configured.

Install any necessary packages first.  I use Fedora, so substitute yum for CentOS/RHEL or your appropriate package manager.  If these aren’t packaged in RPM you can manually add them via the Plug Vim plugin manager.

dnf install git vim-enhanced

Backup your Current VIM Files
(as normal user)

Backup your current ~/.vim and ~/.vimrc just in case you hate it, then you can revert everything and complain about it in the comments.

mv ~/.vim ~/.vim-bak ; mv ~/.vimrc ~/.vimrc-bak

Download vimfiles
The regular version comes without the full code completion engine for most languages, quality-of-life highlighting, git awareness and Python syntax via docstring lookups is included via jedi.

Download vim files and extract them.

cd ~
wget https://github.com/sadsfae/misc-dotfiles/raw/master/vimfiles.tar
tar -xvf vimfiles.tar

Setup Vim Plugin Manager
Open Vim and use the Plug package manager to install and update all the plugins.  You can just use things right away but it’s better to clone and install the latest versions.

vim
:PlugClean

This above clean the current git repos, which are currently set to my file hierarchy.

:PlugInstall

The above will install the latest version of the plugins.

plug-update

Use :PlugUpdate to update all the plugin git repos going forward when you like.

:PlugUpdate

Set System Variables
Set some variables to not anger your system (like crontab or git).

git config --global core.editor /usr/bin/vim
echo "export EDITOR=vim" >> ~/.bashrc

That’s it, you should be ready to go.

What it Looks Like
Utilizing nerdtree (control + n to open/close) which is useful when editing many files.
Use T to open files as new vim tabs in the background, see tips below for more commands.

vim4

Working with SCM
Fugitive plugin adds useful git branch information and functionality.

vim3

Code View with Python
Using vim-jedi (control + space auto-completes possible library syntax and shows docstring info).
Yes, I think you can change the background color.

vim-jedi

Things work in tmux as well.

Screenshot_2016-06-30_15-01-08

Control P – Search
The cntrlp plugin provides full search/autocomplete of files inside current git repository or directory hierarchy.

cntrl-p-vim-search

Email with Mutt
mutt compose is freshened up a bit with vim.

mutt-compose-vim

Updating Plugins with Plug
You can run :PlugUpdate to update your current plugins, or add new ones in your ~/.vimrc to install new ones:

plug-update

Useful Vim Commands

This is a very small list, check other resources if you want to do something specific but here are some of the options I use frequently.

  • :tabe <path/to/file> – opens a new or existing file in a new tab
  • :tabnew – opens a new tab
  • gt – switches to the next tab
  • gT – switches to the previous tab
  • :set paste – enables paste mode, I turn this off by default for wordwrap in mutt
  • :set nopaste – turns off paste mode.
  • :set list – turns on whitespace indicators
  • :set nolist – turns off whitespace indicators
  • :set number – turns on line numbers
  • :set nonumber – turns off line numbers
  • ctrl + space – toggles code completion for Python or string completion of used strings elsewhere.
  • ctrl + n – toggles nerdtree file browser on/off
    • T  – opens files in new vim tab
    • ctrl + w + w – toggles nerdtree focus
    • r – refreshes current directory listings
    • R – refreshes root directory listings
    • m – filesytem actions: create, move
      • a – create a file or directory
      • m – move file or directory
      • d – delete file or directory
      • c – copy file or directory
  • :sp – creats a horizontal split pane
  • :vsp – creates a vertical split pane
  • :sp <path/to/file> – opens an existing or new file in a horizontal pane
  • :vsp <path/to/file> – opens an existing or new file in a vertical pane
  • ctrl + ww – jumps between split panes
  • ctrl + p – powerful file search/completion
  • :%s/dog/cat – searches for dog and replaces it with cat
  • v + direction arrows (or pgdn/pgup) – visual mode and text selection
    • y – yanks (copies) selection to a buffer
    • P – pastes the selection where your cursor is located
    • :s/dog/cat – you can also search/replace within highlighted text

Feedback
Am I missing any useful vim plugins?  Let me know in the comments and I’ll try them out and update the guide here.

About Will Foster

hobo devop/sysadmin, all-around nice guy.
This entry was posted in open source, sysadmin and tagged , , , , , . Bookmark the permalink.

Have a Squat, Leave a Reply ..

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s