Friday, October 17, 2008

Pasting in Vim without Formatting

If smartindent, autoindent, and/or cindent are enabled in VIM,
you find that pasting in the vim does a lot right indentation, which
is not desirable always.

Therefore, you may want to disable indentation while pasting.
This can be done as:

:set paste

Paste your buffer content in the VIM. Then revert back
to indentation mode as given below:

:set nopaste

I hope that this tip will help you in saving time and reducing key
presses.
Enjoy VIMming :)

Thursday, August 21, 2008

Using VIM Command in BASH Command Line

You can use vim commands in the BASH
command line editing, like
  • moving cursors, using l ;
  • Changing word, using cw
  • etc
For enabling vim mode in BASH, you have to append following
line in your bashrc file ~/.bashrc. Just type the command given
below:

$ echo 'set -o vi' >> ~/.bashrc
$ .
~/.bashrc

Now, you can use vim commands by pressing Esc (Escape)
button.

Tuesday, July 1, 2008

Sorting a Column in Vim

I am also learning the Visual mode in Vim.
If you want to sort a column (usually selected)
in a file.

$ vim unsorted.txt
2
3
1
5
4

Go to the line, from where you want to start
the sort. Press Ctrl-v, now select the column
using arrow keys.
2
3
1
5
4

Now, press :sort in command mode. You will get
1
2
3
4
5

Wednesday, June 18, 2008

Indentation in Vim

If you are a programmer, you always find doing indentations hard with
GUI based editors. But you can do indentations of line(s), block, or whole file,
with Vim with much ease and speed.

1. Indentation of a line.
a. Go to the line you want to indent.
b. Press ESc,
c. Press >>, to indent right, or
d. Press <<, to indent left

2. Indentation of n(say 5) lines.
a. Go to the first line of lines you want to indent.
b. Press ESc,
c. Press 5>>, to indent right, or
d. Press 5<<, to indent left


3. Indentation of a code block (Lines between { and }).
a. Go to the beginning '{' or end '}' of block.
b. Press ESc,
c. Press =%, to indent.


4. Indentation of whole program file.
a. Press ESc,
b. Go to the beginning of file by pressing 1G.
c. Press =G, to indent whole file.


I hope that now you can say that Vim is very easy and fast
as compared to other GUI editors. :) Happy VIMming.

Monday, June 9, 2008

Finding Matching Parenthesis in Your Source Code

Let us consider a simple C program.

1 #include

2
3 int main(int argc, char *argv[])
4 {
5 printf("Hello World.\n");
6 return (0);
7 }


In order to find matching parenthesis for parenthesis at line number 4.
Just press % in ESc mode.

You can use same command (%) to match (, ), [, ], {, or } .

Wednesday, May 21, 2008

Run Commands From Vim

Hello, you can run shell commands from Vim editor.

Press Esc : to go to command mode.

Type ! (Exclamation sign)

Type any command after !, and press ENTER

For example:

:!date

Wed May 21 12:44:33 IST 2008

Press ENTER or type command to continue

Searching Current Word

In order to search current word (word under cursor),

forward direction:

Esc *

reverse direction:

Esc #

Now, press either n or N to go in same direction or opposite direction.

This tip will prevent you from pressing many keys.
No need to go to search and press n/N. :)