post

Vim as a Programming Editor in Ubuntu

vimVim (Vi Improved) is a very famous text editor. Vim, which is a terminal-based application, can run on many operating systems. It is free and open-source.

Even though there are many GUI-based text editor, like Geany, Code::Blocks, Dev-C++, etc., Vim is still very popular among programmer communities. That is mainly because of Vim’s efficient and unique set of commands. If you have mastered the basic commands, you surely can type and edit faster than your friends!

In this post I would like to share how I install it in Ubuntu.

Installation

To try using Vim, install a package named vim.

$ sudo apt-get install vim

For the first-time users, the commands in Vim may be considered difficult. Unlike other text editors, Vim operates in several modes, some of them are insert mode, normal mode, and visual mode. Don’t worry, there are many tutorials that can teach you the fundamental commands of Vim. There is also a handy built-in tutorial, called vimtutor. It is a text-based interactive Vim tutorial. Just run vimtutor in the terminal. You will grasp the basic commands quickly.

As Programming Editor

Because of its fast typing and editing experience, many programmers use Vim as their main editor. Vim has several standard interface settings to programmers, such as syntax highlighting, line numbering, etc. You have to enable the desired settings manually in a configuration file name .vimrc (notice the period in the beginning). Now, create the file and save it in your home directory, and type these into the file:

set nocp ts=4 sw=4 noet ai cin bs=2 cb=unnamed
set number ruler wrap autoread showcmd showmode fdm=marker nobackup
syntax on
filetype on

The standard settings have been applied. You can code in Vim comfortably now. Other settings are also available; you may refer to Vim documentation.

One-Click Compilation

If you use other GUI-based programming editor like Geany, you can compile and run your code in one click, i.e. using F9 and F5. This is more efficient than typing the compiler command manually in the terminal. You can obtain the same effect in Vim, by defining a mapping from keys to commands. Append this to ~/.vimrc:

set makeprg=g++\ -o\ \"%:p:r\"\ \"%:p\"
map <F9> :w<CR>:!clear<CR>:make<CR>
imap <F9> <ESC>:w<CR>:!clear<CR>:make<CR>
map <F5> :!clear<CR>:!%:p:r<CR>

Note that this is for C++ language only.

Further References

After making Vim your favorite editor, the next step is to deeply master the Vim commands to gain other Vim benefits. You may read these articles.

Ready to convert to using Vim?

About Ashar Fuadi

Ashar Fuadi is a competitive programmer from University of Indonesia. He loves to code, especially for TopCoder SRM, Codeforces, and ICPC.
Follow Ashar on Google+ and Twitter.

Comments

  1. imap <Tab> <c-n>
    is something Ive found useful. Uses tab instead of Ctrl-N for auto-completion.

  2. Fushar bro..may be this line ( set makeprg=g++ -o “%:p:r” “%:p”) will be (datline+ ./”%:p”). Will you plz check bro.

    • Hi Shaheen,

      The line should have been (set makeprg=g++\ -o\ \”%:p:r\”\ \”%:p\”). I have fixed the post.

      Why do you add ./%:p? The current .vimrc works for me.

Speak Your Mind

*