Pinterest Stumbleupon Whatsapp
Ads by Google

Text editors, although perhaps not the sexiest topic in the world, are really important. One of the most powerful text editors, and therefore one of the most popular, is Vim The Top 7 Reasons To Give The Vim Text Editor A Chance The Top 7 Reasons To Give The Vim Text Editor A Chance For years, I've tried one text editor after another. You name it, I tried it. I used each and every one of these editors for over two months as my primary day-to-day editor. Somehow, I... Read More  (short for, “Vi Improved”).

Although Vim has a sharp learning curve (which can be overcome with hours spent playing with the interactive Vim tutorial Interactive Vim Tutorial: Familiarize Yourself With Vim The Easy Way Interactive Vim Tutorial: Familiarize Yourself With Vim The Easy Way Read More ), and can be intimidating to new users, developers love it. That’s because it allows them to be more productive, and because it can be endlessly customized. This is done by editing the Vim configuration file, known as the “dotfile”.

The Power of the Dotfile

The dotfile is amazingly powerful, and can control almost every part of the Vim experience. Editing yours is really easy. First, you’ve got to check that you’ve actually got one. You can do this by running:

cat ~/.vimrc

If there’s nothing there, you simply need to create it:

touch ~/.vimrc

Now you’re ready to start customizing Vim. To do that, you need to open it in your favorite text editor. This can be Vim, or Nano nano vs. vim: Terminal Text Editors Compared nano vs. vim: Terminal Text Editors Compared Although Linux has become easy enough for practically anyone to use without ever having to use the Terminal, there are some of us who regularly use it or are curious about how one can control... Read More , or even Gedit gedit: One Of The Most Feature-Filled Plain Text Editors [Linux & Windows] gedit: One Of The Most Feature-Filled Plain Text Editors [Linux & Windows] When you think of plain text editors, the first thing that may pop into your head is Windows' Notepad application. It does exactly what its job description states - plain features for a plain text... Read More . Just so long as it’s a plaintext editor.

The Vim dotfile has its own embedded programming language, called VimScript. It’s what we’re going to be using in the rest of this article. But don’t worry. It’s really simple to understand. To get you started, here’s some useful lines to add to your config file.

Ads by Google

Handle Indentation For You

Last month, I published an article on how to write clean, readable code Arduino and Raspberry Pi Beginner? Here's How To Write Clean Code Arduino and Raspberry Pi Beginner? Here's How To Write Clean Code Read More . One of the points I made was to always avoid using ‘soft tabs’, as various text editors interpret them in different ways, and it can introduce problems in programming languages which are strict about indentation, such as Ruby and Python.

Thankfully, with a few lines of VimScript, you can turn each tab into a predefined number of spaces. Just add the following to your Vim dotfile.

vim-tabstop

set expandtab
set tabstop=4
set softtabstop=4
set shiftwidth=4

Python users should use four spaces, as is recommended by the PEP-8 standard. For other languages, you’re best either reading the relevant style guide, or using your best judgement.

You can also set it to auto-indent where required. Again, just add the following lines of code to your dotfile. The second line of code ensures that no line can ever be longer than 80 characters.

vim-autoindent

set autoindent
set textwidth=80

Turn Vim into a Distraction-Free Word Processor

While Vim is a great text editor for developers, it’s also great for those who want a simplified, customizable yet distraction-free environment for writing Write In Peace With These Distraction-Free Editors Write In Peace With These Distraction-Free Editors I have felt it. Visual clutter – thanks to menus and other markup features – have often cemented my writer’s block. So, I have tried out quite a few distraction-free text editors in a grand... Read More .

With a few lines of code, you can configure vim to switch into a “word processor” mode when required. This changes how text is formatted in the editor, and introduces things like spellchecking.

First, create a function called WordProcessorMode, and include the following lines of code.

vim-wordprocessormode

func! WordProcessorMode()
 setlocal textwidth=80
 setlocal smartindent
 setlocal spell spelllang=en_us
 setlocal noexpandtab
endfu

Then, you’re going to need to define how you’ll activate it. The following line of code allows you to create a command. When in command mode, if you call “WP”, it will activate word processor mode.

vim-callwordprocessormode

com! WP call WordProcessorMode()

To test that it works, open a new text file in VIM, and press escape. Then type “WP”, and hit enter. Then, type some text, with some words intentionally spelled incorrectly. If VIM highlights them as incorrect, then you know you’ve installed it correctly.

Install Vundle

Vundle is a package manager for Vim, not too dissimilar to the Ubuntu package manager Your Guide to Ubuntu Repositories and Package Management Your Guide to Ubuntu Repositories and Package Management Read More . It allows you to extend the functionality of it, by installing third-party packages. Many of these are aimed at programmers; others are more generalized productivity tools.

Installing Vundle is easy. First, you’re going to need to install the Git version control system What Is Git & Why You Should Use Version Control If You’re a Developer What Is Git & Why You Should Use Version Control If You’re a Developer As web developers, a lot of the time we tend to work on local development sites then just upload everything when we’re done. This is fine when it’s just you and the changes are small,... Read More . If you don’t have it already, install it. The command for that on Ubuntu is:

sudo apt-get install git

Then, you’re going to need to clone the Vundle repository onto your local machine. That’s easier than it sounds. Just run the following command, and remember that it’s case sensitive.

git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim

Then, add the following lines to your Vim dotfile.

vim-vundle-code

set nocompatible
filetype off
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
Plugin 'VundleVim/Vundle.vim'
Plugin 'L9'
call vundle#end()
filetype plugin indent on

In-between “Vundle#Begin()” and “Vundle#End()” is where you’ll add the packages you want to install. The only required one is “VundleVim/Vundle.vim”, but I also decided to install “L9” for the purpose of illustrating how to install third-party packages.

Once these lines have been added to your Vim config file, you need to do one more thing. Exit your Vim dotfile, and open a new text document. In command mode, run “:PluginInstall”.

vim-plugininstall

If it worked, it’ll look a bit like this.

vim-plugin-worked

Change How It Looks

Many text editors (like on of my personal favorites – iA Writer) allow you to switch between a darker, ‘night’ mode, and a daytime mode. Vim is no exception.

To change the color scheme, simply add “colorscheme” to your dotfile, with the theme you wish to use. It should look like this.

colorscheme darkblue

Here, I’m using “darkblue”, as suggested by Wyatt Andersen on Twitter. Of course, there are dozens to choose from.

To see if it worked, reopen Vim, and confirm visually.

vim-colormode

Slap On Some SPF13

If you don’t feel confident enough to modify Vim dotfile, there’s a simpler way. SPF13 is a distribution of Vim that comes pre-built with the plugins and dotfile modifications you need to be a productive developer. Installing it on Linux, FreeBSD, and OS X is remarkably simple. Just open up a terminal and run:

curl http://j.mp/spf13-vim3 -L -o - | sh

Then, let do what it has to do. Once it’s finished, you’ll have a turbo-charged Vim install. If you’re on Windows, read the instructions on the SPF13 website.

Using SPF13 doesn’t stop you from configuring Vim to how you like it, however. Just edit the .vimrc dotfile as required.

Mistakes Happen

It’s worth noting that if you make a mistake in your Vim dotfile, Vim will let you know with some helpful debug messages.

vim-errordetection

If you have trouble making sense of them, a great place to ask for help is on StackOverflow 10 Websites That Can Help The Beginner Programmer With Sample Code Snippets 10 Websites That Can Help The Beginner Programmer With Sample Code Snippets Read More , the Vim subreddit, and even MakeUseOf Answers. Just pay attention to this simple advice 5 Tips For Asking A Question On MakeUseOf Answers 5 Tips For Asking A Question On MakeUseOf Answers MakeUseOf Answers is a bit like Yahoo Answers, but with one key difference. The people who answer your question are the same people who write for us. If they can't help you, nobody can. Read More to increase the chance your question will be answered.

Over To You

Vim is a remarkable text editor. One of the great things about it is how it lets you configure it to your hearts content. If you want more inspiration, I highly recommend you check out the excellent Learn VimScript The Hard Way by Steve Losh.

For more inspiration, check out other people’s dot files as well. Many people have published their own on Github, under open-source licenses. The dotfiles written by Hakim Cassimally, Andrew Bolster, Amir Salihefendic, and one-time MakeUseOf writer Erez Zukerman are worth a look.

Are You a VIM user? Have you configured your own dotfile in an interesting way? I want to hear about it. Leave me a comment below, and we’ll chat.

  1. Aaron Enfield
    November 4, 2015 at 7:44 pm

    Get the most out of vim by editing .vimrc with nano or gedit. This makes me chuckle.

  2. likefun butnot
    October 28, 2015 at 10:11 pm

    I don't think I've made any appreciable changes to my home directory's .rc files since probably 2001. These are things I haven't needed to experiment with. I'll have to take a better look at other people's .vimrc files.

    • Matthew Hughes
      October 29, 2015 at 11:04 am

      A well-configured VIM dotfile is amazing. Play around with yours, and let me know what you think!

  3. Brian Wisti
    October 28, 2015 at 6:20 pm

    Oh, and "To see if it worked, reopen Vim, and confirm visually" - an alternate approach is `:source ~/.vimrc`. That's what I usually do to refresh settings.

    • Matthew Hughes
      October 29, 2015 at 11:02 am

      That's a great tip. Cheers Brian!

  4. Brian Wisti
    October 28, 2015 at 6:13 pm

    Nice! I've dabbled a bit with Vundle and spf13, but usually end up going back to a fairly barebones .vimrc. Currently expanding my knowledge of the basics with a Damian Conway Vim video from O'Reilly, which I enjoyed so far.

Leave a Reply

Your email address will not be published. Required fields are marked *