Install vim on Ubuntu first. Ubuntu only ships with vi, not vim, so install vim
$ sudo apt-get install vim
Next, set the vim preference so that it loads up every time vim is executed
$ vim ~/.vimrc
This will open up the .vimrc file on your home directory that will be executed whenever vim is loaded up.
To edit the file, press i keystroke, which will switch to the insert mode. Write out
syntax on
set nu
filetype plugin indent on
set tabstop=4
set shiftwidth=4
set softtabstop=4
set expandtab
set hlsearch
To save, press ESC keystroke, and enter
:wq
which will write (w) the file and quit (q).
That's it. syntax on turn on syntax coloring, set nu will turn on line numbering, and the last line filetype plugin indent on will turn on automatic indentation, which is quite handy when programming.
Let's write helloworld program in c using vim
Create helloworld.c file in the home directory by running the following commands in terminal
$ cd ~
$ vim helloworld.c
When the vim opens up, press i keystroke again to switch to insert mode
Enter the c code
#include <stdio.h>
int main () {
printf("Hello World!\n");
return 0;
}
Notice the auto indentation feature!
Again, press ESC key, and enter the following to save and quit vim
:wq
Let's compile using gcc and run it
$ gcc helloworld.c
$ ./a.out
Great to see it works well. Note that the default compiled binary from gcc is a.out. In order to execute the file in the current directory, you simply enter.
$ ./<binary_file_name>
** If you are a Mac OS X user ***
To install vim on Mac OS X, you will first install homebrew and run
$ brew install vim
Also, if you are using a non-Apple keyboard, you probably want to change the configuration of the backspace by appending the following line to ~/.vimrc
set backspace=indent,eol,start
No comments:
Post a Comment