vimVim (Vi Improved) is one of the most popular text editor in the world. It’s cross platform and text-based, and, of course free and open source.

Although there are many GUI-based programming editor out there, like Geany, Code::Blocks, etc., I always stick with vim to do programming contest tasks. That’s because its commands are efficient and different from most editors. Once you master the commands and combinations, you can type FASTER than your friends! That’s an important factor in contest.

Installing

Want to try vim? Make sure it’s already installed.

sudo apt-get install vim

Vim’s commands are rather difficult for beginners. But there is a handy tutorial that make you master the most basic commands. Just run vimtutor in a terminal, and you’re brought to an interesting tutorial. Memorize the commands.

Using Vim as Programming Editor

Vim is very customizable and extendible. Some key features to programmers are syntax highlighting, line numbering, and so on. Vim saves the configuration in a file called .vimrc (notice the period in the beginning of its name). So first create a file called .vimrc, save it in your home directory (so the resulting file path is ~/.vimrc), and edit it:

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

Now you are ready with basic configurations: tab stop, auto indentation, line number, ruler, text wrapping, syntax highlighting, etc.

One-Click Compile and Run

If you were working with other IDEs, you might be able to press one key and your current file will be compiled (F9 in Geany). That saves much time, instead running the compiler manually in the terminal. You can do that in vim too. Now you will create key mappings, so if you press F9, the code will be compiled. Works with C/C++ only. Add these lines into your ~/.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>

This way you have set make program to run g++ as the default compiler. Then to compile, press F9, and to run the executable, press F5. That’s very handy!

  • Share/Bookmark

Enjoyed this article? Subscribe to our RSS feed for free!

Related posts:

Be the first to comment!

Leave a Reply