In order to increase a number under cursor,
just press Ctrl-A in command mode.
Esc Ctrl-A
and for decreasing the number under cursor,
press Ctrl-X in command mode.
Esc Ctrl-X
Showing posts with label vim. Show all posts
Showing posts with label vim. Show all posts
Friday, August 28, 2009
Wednesday, July 15, 2009
Jumping to Beginning/End of (Code) Block
Many times, we want to go to beginning or end of the code block like function, condition. or loop statement. This is required either
to see proper indentation, or
to see function declaration, etc, etc.
And we do this generally by using arrow keys, which is very time consuming and repetitive. VIM provides this facility to go to beginning or end of code block.
[m go to start of the block
]m go to end of the block
to see proper indentation, or
to see function declaration, etc, etc.
And we do this generally by using arrow keys, which is very time consuming and repetitive. VIM provides this facility to go to beginning or end of code block.
[m go to start of the block
]m go to end of the block
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
we just need to do following key presses in command mode:
Now those lines are commented
We can uncomment those lines too, just by giving following
command:
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)
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 /* */
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_commandwith the command you want to use.
22
23
Suppose we want insert date at line number 23,
goto line number 23, by pressingG23
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.
: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.
(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 :)
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
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.
command line editing, like
- moving cursors, using l ;
- Changing word, using cw
- etc
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
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.
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 } .
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
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. :)
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. :)
Wednesday, April 16, 2008
Deleted Contents in Numbered[1-9] Buffers
If you have deleted some content (line(s) or word(s)) accidentally,
you can retrieve using Paste command (p or P).
For example,
In order to retrieve second latest deleted content:
Press Esc
Press "2p
you can retrieve using Paste command (p or P).
For example,
In order to retrieve second latest deleted content:
Press Esc
Press "2p
Tuesday, April 8, 2008
Changing a Part of Word
All of us know cw (change word) command, to change a word.
It changes word from cursor to end of word. Suppose you want
to change a part of word.
for example: Mismanagement to Management
(i). Suppose your cursor is at M of Mismanagement, like
Mismanagement
(ii). Since you want to change only 4 characters "Mism" to "M",
Press c4l (Remembrall: Change 4 Left)
Then press M as replacement word. You will get
Management
Note
It changes word from cursor to end of word. Suppose you want
to change a part of word.
for example: Mismanagement to Management
(i). Suppose your cursor is at M of Mismanagement, like
Mismanagement
(ii). Since you want to change only 4 characters "Mism" to "M",
Press c4l (Remembrall: Change 4 Left)
Then press M as replacement word. You will get
Management
Note
Friday, April 4, 2008
Search and Replace on VIM
1. Search and Replace All (Easiest)
Esc
:%s/old_pattern/new_pattern/g
2. Search and Replace selected
(a) Esc
(b) /old_pattern
(c) Go to desired by pressing n again and again
(d) Press cw (Change Word) and type
(e) Esc
(f) Press n to go to next old_pattern
(g) Press . if you want to do repeat last command used in step (c)
(h) Go to step (f), until all desired changes are made
Esc
:%s/old_pattern/new_pattern/g
2. Search and Replace selected
(a) Esc
(b) /old_pattern
(c) Go to desired
(d) Press cw (Change Word) and type
(e) Esc
(f) Press n to go to next old_pattern
(g) Press . if you want to do repeat last command used in step (c)
(h) Go to step (f), until all desired changes are made
Monday, March 31, 2008
Convert DOS EOL (End of Line) to Linux EOL
This is useful when you do not have dos2unix command.
You can determine whether a file is having DOS End of Line.
$ od -c dos_file.txt | grep "\\\r" | wc -l
If you see any output other than 0, then this file
is having \r\n as new line (i.e DOS EOL).
1. Suppose you want to convert EOL of dos_file.txt . Open this
file using vim as:
$ vim dos_file.txt
2. If ^M (Ctrl-M) character is at end of every line, then this file
is in DOS format. We have to remove this ^M from the end
of line. Therefore, go to Edit mode by pressing Esc (Escape
button).
3. Type
:%s/^M$//g
And viola!, you will find that all ^M has gone from end of line.
4. Now, save the file and exit.
PS. In order to type ^M on screen, you should press Ctrl-v Ctrl-m
Otherwise this method will not work.
Alternatively, you can use sed to remove ^M from end of line, given
as following:
$ sed 's/^M$//g' dos_file.txt > temp
$ mv temp dos_file.txt
You can determine whether a file is having DOS End of Line.
$ od -c dos_file.txt | grep "\\\r" | wc -l
If you see any output other than 0, then this file
is having \r\n as new line (i.e DOS EOL).
1. Suppose you want to convert EOL of dos_file.txt . Open this
file using vim as:
$ vim dos_file.txt
2. If ^M (Ctrl-M) character is at end of every line, then this file
is in DOS format. We have to remove this ^M from the end
of line. Therefore, go to Edit mode by pressing Esc (Escape
button).
3. Type
:%s/^M$//g
And viola!, you will find that all ^M has gone from end of line.
4. Now, save the file and exit.
PS. In order to type ^M on screen, you should press Ctrl-v Ctrl-m
Otherwise this method will not work.
Alternatively, you can use sed to remove ^M from end of line, given
as following:
$ sed 's/^M$//g' dos_file.txt > temp
$ mv temp dos_file.txt
Swapping Two Consecutive Lines in Vim
1. Go to edit mode by pressing Esc (Escape Button).
2. Go to the line which you want to swap with the line following.
Example:
1 Singh Jat
2 Mitesh
3. Press dd, then press p. You will find the two line are swapped.
Example:
1 Mitesh
2 Singh Jat
2. Go to the line which you want to swap with the line following.
Example:
1 Singh Jat
2 Mitesh
3. Press dd, then press p. You will find the two line are swapped.
Example:
1 Mitesh
2 Singh Jat
Swaping Two Consecutive Characters in Vim
1. Go to edit mode by pressing Esc (Escape Button).
2. Go to the character which you want to swap with the character following.
Example: Mitseh
3. Press x, then press p. You will find the characters are swapped.
Example: Mitesh
Remembrall: Esc-x-p Transpose (X-Pose) characters
2. Go to the character which you want to swap with the character following.
Example: Mitseh
3. Press x, then press p. You will find the characters are swapped.
Example: Mitesh
Remembrall: Esc-x-p Transpose (X-Pose) characters
Subscribe to:
Posts (Atom)
