Wednesday, May 13, 2009

(Un)Commenting Multiple Lines in a Script/Program

Until I come to know about markers in VIM, I find
it much difficult to comment/uncomment multiple
line in script/program. Especially when a programming
language only supports one line comment.
For example: # (Pound) is used as a comment in shell scripting
Suppose, we want to comment lines 8-12 in following
shell script


1 #!/bin/bash
2
3 # shut some DoS stuff down
4 echo 1 > /proc/sys/net/ipv4/tcp_syncookies
5 echo 1 > /proc/sys/net/ipv4/icmp_ignore_bogus_error_responses
6 echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts
7
8 # increase the local port range
9 echo 1024 65535 > /proc/sys/net/ipv4/ip_local_port_range
10
11 # increase the SYN backlog queue
12 echo 2048 > /proc/sys/net/ipv4/tcp_max_syn_backlog
13
14 echo 0 > /proc/sys/net/ipv4/tcp_sack
15 echo 0 > /proc/sys/net/ipv4/tcp_timestamps



we just need to do following key presses in command mode:

8Gma12mb

:'a,'bs/^/#/


Now those lines are commented

7
8 ## increase the local port range
9 #echo 1024 65535 > /proc/sys/net/ipv4/ip_local_port_range
10 #
11 ## increase the SYN backlog queue
12 #echo 2048 > /proc/sys/net/ipv4/tcp_max_syn_backlog
13


We can uncomment those lines too, just by giving following
command:

:'a,'bs/^#//



Now, we see how this works
1) 8Gma12Gmb
The above line is used to mark beginning (8G- Goto 8, ma- Mark as 'a') line
and end (12G- Goto 12, mb- Mark as 'b') line.
2) :'a,'bs/^/#/
Run subtitute command s on lines started with mark 'a' ('a) and ended with
mark 'b' ('b')

Note: To comment multiple lines in C/C++/Java without /* */

:'a,'bs/^/\/\//

Saturday, May 9, 2009

Read Command Output in Vim

In order to insert output of a command while in Vim
editor. Just give following command.

:r !
any_command

Replace any_command with the command you want to use.


22
23

Suppose we want insert date at line number 23,
goto line number 23, by pressing
G23
Then type following command
:r !date

22
23 Sat May 9 20:15:56 IST 2009

Thursday, March 26, 2009

Undoing All Changes Made in a File

The command:
:e!
undoes all the changes made to the file.
I hope this command will save you sometime,
if you made many disastrous changes.

Tuesday, February 17, 2009

Going to Line Number

There are two ways for going to a line (say line number 149).
(i)
Esc :149
(ii) Press
149G

PS:
I would like to suggest that please enable the line number by pressing
Esc :set nu

in the Vim editor. Then you can use above trick efficiently and easily.

Wednesday, November 26, 2008

Auto Completion of Word in Vim

If you want to auto complete a word(or variable in a program), then just press Ctrl-P in the editing mode.


For example:


com<ctrl-p> => complete.


If there is more than one possible words for completion, then vim shows a drop down list, from where you can select the desired word.

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.