Archive

Archive for the ‘vi’ Category

Split Windows and Shell Commands in VI

March 23, 2010 Leave a comment
Been working on a project to learn Ruby and decided it was also a great opportunity to learn VIM. One of the gripes i was having was needing to close vim to open another file or run commands on the shell. I figured there should be another way to do this so i googled around and found a way to do it.
The sp command will split you window horizontally and spw will split it vertically. Let’s say you want to edit another file and split the window horizontally you use the command:
:sp filename
This will split the window with your new file on the top pane, and your old file on your bottom pane. You can also decide how big your new pane should be by supplying the line number before issuing the command:
:20 sp filename
The above command splits the window and gives your top pane 20 lines, leaving the rest for the bottom pane.
To switch between panes just us Ctrl-w [up|down|j|k], you can also change the pane size with Ctrl-w[number+|number-] (where number is the number of lines to grow or shrink). If  you want to split the window without opening a new file just use Ctrl-ws
A small note: you have other options besides split to edit multiple files since VI supports buffers easily, use :
:e filename
to open a new file in a new buffer and :bn or :bp to cycle between them.
Now i just needed a way to call the ruby interpreter on the shell without being forced to quit vi. Found its pretty easy to run code on the shell. just use :
:!command
So to run ruby on some file (on the folder i ran vi) i just typed
:!ruby file
Also you can run a command on the file in the current buffer by using the % character:
:!ruby %

Even though i used ruby as an example you can run any shell command you need.

For further explanations visit links bellow.

References:

Advertisements