Level up VIM skills

Level up VIM skills

Over two years I’ve been using VIM as the main development tool. From plugin for Visual Studio to complete IDE for JS and Go projects. In this post, I’m going to level up your VIM experience by sharing my best practices, most used hotkeys and awesome plugins. If you are interested in then welcome into my VIM-world. But if don’t know what the hell it is or you don’t have enough confidence when using VIM, I recommend you to read the set of articles about VIM for beginners (one, two, three).

Navigation

Navigation probably is the most useful and difficult thing to learn when using VIM. If your navigation commands are limited by hjkl these tips are definitely for you.

If you want to navigate to any character on the current line you can use two commands:
f + character to move cursor directly on the searched character
t + character to move cursor just before the searched character
Moreover, you can “power up” these commands (write them in uppercase) to reverse search direction. So, they look like F + character and T + character accordingly. If you have a couple of occurrences you can use ; command which repeats last line search.

Use line_number + G command to move your cursor to the specific line. To enable line numbers you have to specify set number in your .vimrc file. Here you can also enable relative line numbers with set relativenumber command. The trick is that instead of showing the absolute line numbers from the beginning of the file, it shows numbers relatively to the line you are currently on. Therefore, the fifth line under your current line is marked as 5 and if you want to move to that line you can type 5j. Pretty easy.

VIM relative numbers demo

Command `. allows you to move the cursor to the last editing place in a file. It works because ` (backtick) command moves the cursor to the mark, and . is a special mark which is automatically set to the position where the last change was made. Besides, you can use gi command which does the same but switches the mode to the insert.

The next set of commands is not truly navigation type, but it is really awesome. Command zt scrolls your current line to the top, zz to the center and zb to the bottom of the screen. And H moves the cursor to the top, M to the center and L to the bottom of the screen. You can also specify set scrolloff=lines_number in your .vimrc to set a minimal number of lines to keep above and below the cursor. This makes some context visible around where you are working.

Windows Navigation

This set of VIM windows commands starts from Ctrl + w and then:
v – to open new window vertically
s – to open new window horizontally
c – to close current window
h, j, k, l – to navigate between windows

You can simplify windows navigation by setting up these lines in your .vimrc:

map <C-j> <C-W>j
map <C-k> <C-W>k
map <C-h> <C-W>h
map <C-l> <C-W>l

VIM windows navigation demo

Plugins

VIM has a huge plugins ecosystem (e.g. there are 15000 plugins on vimawesome). Here I’ll show you those that make my VIM. To start this story properly you need to install plugins manager. Personally, I use Pathogen.

The NERD Tree

This plugin allows you to open files and directories on your computer in a tree structure. After installation, you can open tree window by typing command :NERDTreeToggle. Indeed, it becomes obviously that to work with this plugin more effectively you need to create a mapping in your .vimrc. Such one is suggested for Ctrl+n hotkey in Nerd Tree docs:

map <C-n> :NERDTreeToggle<CR>

If you use VIM as IDE you will definitely like it. Nerd Tree has a lot of useful things such as highlightings for different files, bookmarks, filters and lot more. Press ? to read it’s help and check out the NerdTree repository.

CtrlP

Navigation between files and buffers becomes easier with VIM CtrlP plugin which is basically fuzzy search. Find and open project files in new window split or tab just by pressing Ctrl + p command. It’s fast, extensible and awesome!

See CtrlP docs to setup root folder and ignore unnecessary files.

EasyMotion

EasyMotion provides a much simple way to use some navigation commands in VIM. Let’s consider a small example to understand the way of how it works. So, to navigate one line down you use motion j which is native for VIM. With EasyMotion you can type leader + leader + motion (in our case \\j) and all the lines below will be highlighted with letters to the end of the screen. Then you type a specific letter to jump directly to the target line.

Yep! Here we need to understand what the leader is. VIM has already mapped most keys and combinations, so leader key is a kind of namespace for commands where you or plugins can add custom behavior. The default leader key in VIM is \ (backslash), but you can easily remap it with :let mapleader = "," in your .vimrc file.

The last about EasyMotion is that this plugin supports lots of VIM motions. Don’t be lazy to read EasyMotion docs.

Incsearch

This is lightweight plugin which allows you highlight all pattern matches while search, unlike default VIM behavior.

See Incsearch docs for more info.

Commentary

By default, VIM does not have an ability to comment out code in a different languages with a single hotkey. With Commentary use:
gcc – to comment/uncomment out a single line
gc + motion – to comment/uncomment out a target motion (e.g. gca{ comment around curly braces).

See Commentary docs for more info.

Surround

The only one thing this plugin does is managing surrounds. And by surrounds I mean parentheses, brackets, quotes, XML tags, and other. So, it allows you easily add, delete and change such code surrounds. There are only three commands is used in this plugin:
ys + action – to add surround
cs + action – to change surround
ds + action – to delete surround

Examples:
ysiw" – wraps inner word (iw) into double quotes
cs"] – changes surround from double quotes to square brackets
ds] – deletes double quotes
yss<div> – wraps all the line with tag div (yes, it perfectly works with html)

See Surround docs for more info.

Repeat

I recommend you to take a look at this plugin. I suppose you know how . command works in VIM, but the problem is that it works only for native VIM commands. This plugin just remaps . command in a way that plugins can tap into it. From the list of plugins above it supports Commentary and Surround. But if to read Repeat docs, you will understand that it’s easy to add other plugins support.

Your plugins

If you found yourself using some awesome VIM plugins you are welcome to share experiences in comments below.

Wrap Up

Unfortunately, in this post I didn’t tell you everything I wanted. But if you like this post, next time we will speak about styling your VIM, registers and advanced .vimrc file. Click the “Like” button and leave comments with your opinion. It motivates me to write new posts. Thank you for reading my blog and have an awesome day!

Advertisements

One thought on “Level up VIM skills

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