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.  This will also work on OSX by using the tar I provide, see below for further setup instructions needed in ~/.vimrc.

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

Install Powerline Fonts
At this point you’re ready to go, however you may want to install the powerline fonts, they are quite nice.  This works on Linux or OSX.

git clone https://github.com/powerline/fonts

The installation script installs the fonts locally.

cd fonts
sh install.sh

OSX Only – Edit ~/.vimrc
You need a few more lines in ~/.vimrc if you’re using OSX.

"""""""" OSX ONLY
set guifont=Inconsolata\ for\ Powerline:h15
let g:Powerline_symbols = 'fancy'
set encoding=utf-8
set t_Co=256
set fillchars+=stl:\ ,stlnc:\
set term=xterm-256color
set termencoding=utf-8

if has("gui_running")
 let s:uname = system("uname")
 if s:uname == "Darwin\n"
 set guifont=Inconsolata\ for\ Powerline:h15
 endif
endif

"""""" END OSX

"" SET UNICODE
set encoding=utf-8
set guifont=Source\ Code\ Pro\ for\ Powerline "make sure to escape the spaces in the name properly

OSX Only – iTerm2 Profile
If you use the iTerm2 terminal on OSX you’ll need to now set the font appropriately in your profile.

Use Italics for Comments
To use italics for code and config comments follow this guide, otherwise you’ll want to comment out this line in your ~/.vimrc

highlight Comment cterm=italic

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).

Things work in tmux as well, yes you can change the background color.

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/SRE
This entry was posted in open source, sysadmin and tagged , , , , , , , . Bookmark the permalink.

Have a Squat, Leave a Reply ..

This site uses Akismet to reduce spam. Learn how your comment data is processed.