Things I've Learned in Vim So Far

As I move over to vim as my full-time text editor, I am constantly reminded how much there is to know about vim, and how very, very little I currently know.

BUT each week I get a little bit better.

Here’s what I’ve learned this week.

Smooth screen scroll

For years, I’ve been j-k‘ing (cursor up/down) and ctrl-d-ctrl-u‘ing (half-page up/down) my way through files.

This week I learned that ctrl-e and ctrl-y will “smooth scroll” the screen up/down one line at a time, without moving the cursor.

Simple? Sure, but also rather useful.

Use normal mode to edit many lines

Needing to perform an action across multiple lines of code is fairly common occurrence.

My preferred way to do this has quickly become to select multiple lines in visual mode (shift-v, j-k‘ing to highlight lines) then running a command in :normal mode.

To run the command A, across each selected line, type:

:normal A,

This command will:

Who knew?

Set syntax of a buffer

Sometimes you want to open up a tab and work with some throwaway code. This tip lets you highlight syntax without saving the file to disk: http://stackoverflow.com/questions/3853028/how-to-force-vim-to-syntax-highlight-a-file-as-html

:set syntax=html

Obviously, the html above could be replaced with json, javascript, ruby, etc.

Replace tabs with spaces

I really, really dislike hard tabs in source code. So anytime I find a file containing them, my first priority is to replace them with spaces.

Vim’s search/replace within a buffer is still a bit shakey for me, so I really enjoyed this tip: http://stackoverflow.com/questions/426963/replace-tab-with-spaces-in-vim

The command:


expands hard tab characters as defined by your tabstop and expandtab settings.

I also use this snippet in my ~/.vimrc to easily spot hard tabs:

" Display hard tabs
set list listchars=tab:»

Remove trailing whitespace on save

Trailing whitespace at the end of a line is sloppy. It can lead to frustrating git diffs that serve no purpose, and is just bad practice.

Adding the following to your ~/.vimrc will remove trailing whitespace with every save!

autocmd BufWritePre * :%s/\s\+$//e

Quit everything!

Working on a rails project I commonly have several tabs open with multiple split screens on each tab. Until now I’ve been :q‘ing to close each buffer.

Another simple trick I learned this week closes all buffers with a single command.

When it’s time to go home for the night, running:


will close all open buffers at once.

Sometimes it’s the little tricks that help the most.

Awesome plugin: ctrl-p

Fuzzy file finders have become my go-to way for opening files across a project path using sublime text, so I was happy to find an even better plugin for vim.

The Ctrl-P plugin allows you to type ctrl-p then a few characters that fuzzy find a file somewhere in your working directory.

For instance typing controlluser will populate a list of matching files as you type with: app/controllers/users_controller.rb highlighted. (Change the highlighted file in the list with ctrl-j ctrl-k to select the file above/below respectively.)

What makes this setup better than fuzzy finding in other text editors is that once highlighted, files can be opened in several ways. I can open the file in:

If a file is already open in any tab/split, Ctrl-P will take you to that buffer.

Just lovely.

Source: https://github.com/kien/ctrlp.vim

So, that’s what I’m committing to memory this week. If you would like to share tips, hit me up on twitter @vormwald!

Image credit: Ted Naleid

Share Comment on Twitter