Archive for the ‘VI/VIM’ Category

How to remove ^M character with VI

May 5, 2010

There is a much easier way.

In vi, execute this –


To get the ^M hold the control key, press V then M (Both while holding the control key) and the ^M will appear. This will find all occurrences and replace them with nothing.



Hope this helps


Edit a file when viewing with less / more !!

February 16, 2010

I use less or more pagers to view a file. How do I start an editor (such as vi or other text editor) to edit the current file without leaving the less or more pager command.

Here is the trick –
You define your EDITOR first, enter:

export EDITOR=vim
echo ‘export EDITOR=vim’ >> ~/.bash_profile

Now open any file using more or less pager:
less myfile.txt
more ~/some.conf

Now, to edit a file, hit v key from keyboard. This will start up an editor at current line and file. make changes then save and exit, you will be back with less/more pager again 🙂

Change, delete, copy, paste in vi editor

May 20, 2008
Actions Change Delete Copy
Line cc dd yy
Letter cl (r) dl (x) yy
word cw dw yw
Sentence ahead c) d) y)
Sentence behind c( d( y(
Paragraph below c} d} y}
Paragraph above c{ d{ y{

N.B. To change a single line we use ‘cc’ but to change multiple line we will use ‘ncc’ where n is the no of line u want to change.Same in case of delete and copy.

Delete from current line to the end of file
Delete all the lines to a file-
Move a word ahead
press w

dL Delete up to last line on screen
dG Delete to end of file
J Join two lines

rx Replace character with x
Rtext Replace text beginning at cursor
Ctrl-G Display line number of cursor
s Substitute character
nG Move cursor to line number
U Restore current line
x Delete current cursor position
X Delete back one character
nX Delete previous n characters
. Repeat last change
~ Reverse case
ye Copy to end of
dn,m Delete line starting from n to m
d8G Delete all lines from cursor to line 8