Monday, May 18, 2015

Programming using VIM editor

There would be some cases, where we need to change source on the host machines and run them. For those instances, VIM (VI - improved) is very good editor to make use of. As we are most used to modern GUI editors, we may find it difficult to use the terminal. We can make best use of terminal using some tips and tricks. I will try to explain what i generally use.

Syntax Highlighting

We do make mistakes/typos when typing, so better to use syntax highlighting to make sure that we are typing the right command/keyword/phrase etc. Syntax highlighting is simple enough in VIM. Use the following commands to set that. Open VIM (empty or with any java/c file) and type
:syntax on
VIM detects the language automatically, but if you would like to change the language, use syn variable to set the preference (java or xml etc). See some samples below on how to use
:set syn=java
:set syn=xml
If you are using a terminal, syntax highlighting may not work for you, unless you set your term variable to ansi
:set term=ansi
You may need to add this in your .vimrc file (exists in $HOME directory. If not exists, create it) to default it for all the files opened by VIM.

Color-Schemes

Syntax highlighting will be shown with default color schemes. There are few color schemes available by vim by default, you can switch the color shown on the highlighting. use colorscheme keyword.
:colorscheme <color-scheme-name>
You can download the color schemes available on web and place in your vim colors directory. Generally it will be at $HOME/.vim/colors. You can download the color-schemes from from github, and install. Copy the downloaded files into .vim/colors (in $HOME), if not exists - create it, and copy all the colors to colors folder.
If want to default the syntax highlighting and color schemes defaulted when you open VIM, simply put all the commands into .vimrc files in your home directory.
See one sample below
set term=ansi
syntax on

colorscheme eclipse

Compile and Run

To run your program, you can use the compile or execute command prefixed with :! inside VIM editor. See sample below. Open vim and write the code and compile using
:!javac Sample.java
When you run the command, a new screen will be opened with the javac command output (Compilation errors/success of compilation). Once the compilation is successful, run the program using the following command.
:!java Sample
  • Note: Just a point to note, javac and java should be in PATH variable to be recognized. 
But it will be too cumbersome to type the same command multiple times, if any compilation errors, you have two options to make it simple and easy. 

QuickRun plugin

As VIM allows to add plugins, there is a plugin QuickRun which compiles and run a program in one go. Download the plugin at github. Follow the installation instructions (just copy the mentioned files to your vim plugins directory - $HOME/.vim/plugins). Once installed, you can run your program by the command QucikRun. Open your program and execute the command
:QuickRun
This will compile and run the program and output will be shown by splitting the screen. QuickRun supports few programming languages by default and it recognizes based on the file type (See documentation for more details).

MakePrg command

If you wouldn't like to install a plugin, or if the plugin doesn't support the programming language that you are working on, then you can use makeprg command to compile your program.
Edit .vimrc file in your $HOME directory to include the following lines (I am giving an example on how to use javac command in makeprg)
"Compiling Java Code
autocmd Filetype java set makeprg=javac\ %
set errorformat=%A%f:%l:\ %m,%-Z%p^,%-C%.%#
map <F9> :make<Return>:copen<Return>
map <F10> :cprevious<Return>
map <F11> :cnext<Return>
By doing this, you are mapping some of the control characters to default compile command (i.e. make). What we are doing in the above lines are
  • Setting the detault make program to use the command javac
  • Mapping F9 (Function-9) key to compile the java code and return to the current window without any prompt.
  • If any compilation error,
    • use F11 to see the next error message (printed in the given error-format).
    • use F10 to see the previous error message (printed in the given error-format).
How to use
Open the program using vim, write the code and compile using by pressing F9. To navigate through the errors, use F10 and F11.
Using this, you can only compile your code, it doesn't execute (Of course javac doesn't execute your java program). Use :!java command from the vim editor to execute the compiled program (as mentioned in the starting of the post), or instead use the java command on the command line (after exiting from vim editor). 

Hope this post helped you with the commands. Happy Learning!!!

No comments:

Post a Comment