Friday, November 7, 2014

Some vim goodness ...

As you would expect of a long term linux user, I am familiar with vim. Most who use it know that a PhD is needed to actually drive it with any amount of competency.

For this reason I am currently using Sublime Text 3 (on Mac), which by the way is awesome (http://www.sublimetext.com/3).

The team I am currently working with are highly skilled and are big users of vim so I have made it my mission to start the transition back to it and thought I would share some of the learnings that I have recently made.

1. Working with split screens


When working with things like Puppet and Ansible, it is often very useful to have 2 files open at once, either to copy something that was done before or when tracing down an issue.

So here is the scenario, I have file1 and file2 and I want to get them on the screen with a vertical split.

Firstly we open file1:

vim file1

Then split the screen vertically:

ESC + :vs

This will split the screen vertically, your cursour will be on the left hand side.

To move between the split you just use:

CTRL + w then L or R arrow

You can do the ":vs" more than once so can have more than 1 split going in a window, moving around it is the same.

To close down a split screen simply exit vim using something like:

ESC + :q (or :wq if you want to write changes)

2. Multi line changes


This is a very powerful action and one that I rely on every day in Sublime Text. This method uses vim's visual blocking or highlighting.

Here are a couple of examples on how do use this:

Indent a block of text 4 spaces

place the cursor on the first character of the first line
ctl+v (vertical select)
arrow down rows
shift+i
Press space 4 times
esc (apply to all lines)
In action: https://www.youtube.com/watch?v=o4ZvVZDyBV4

Amendment:
It has also been bought to my attention, if you want to just do a single indent (ie 2 spaces) you can use `>` (Thanks MichaelD)

Delete 4 spaces from the beginning of a block of text

ctl+v (vertical select)
use the arrows to highlight a block
press x (x usually is used to delete a single character)
In action: https://www.youtube.com/watch?v=fQrX4OYI2_w

3. vim customisation

There have been a number of things whilst using Sublime Text that I have really gotten use to, spaces used in tabs, line numbers etc etc.

vim has some very powerful customisations, this is controlled by a file called .vimrc and usually lives in the root of a users home directory ie ~/.vimrc

Similar to ruby's bundler vim has Vundle, this allows you to nicely manage plugins for vim.

https://github.com/gmarik/Vundle.vim

Here is a really good article on how to set it up

https://www.digitalocean.com/community/tutorials/how-to-use-vundle-to-manage-vim-plugins-on-a-linux-vps

Once you have vundle installed it is a simple matter of installing extra plugins for use. A good start is a colour theme, I am a fan of the colour scheme w0ng/vim-hybrid.

To install it you do:

vim then :PluginInstall w0ng/vim-hybrid

Then you add it to your ~/.vimrc.

Here is what my current file looks like:

Bundle 'w0ng/vim-hybrid'
color hybrid
syntax on
set number
set tabstop=2 softtabstop=0 expandtab shiftwidth=2
Hybrid is a really nice colour scheme that has great syntax highlighting for things like ruby, puppet and ansible. It is also really good when using vimdiff.

You can find a heap of plugins to install here

http://vim-scripts.org/vim/scripts.html

I also like to have the line numbers showing by default, this is done with `set number`. I also like to have softtabs set to 2 (ie tabs are achieved with spaces).

Given the complexity of vim you can do some amazing things with this customisation, many I have yet to discover but will share once I do.

Until next time.

No comments:

Post a Comment