LINUX CLASSES - TEXT EDITORS

Vi - Linux Editor

Can A Text Editor Be Evil?

The vi editor comes with every version of Linux or Unix. It's a terribly unfriendly beast of an editor, but you should know about it because someday you're likely to find yourself on a system where you have no other choice but to use it. A friend of mine calls vi the Heart of E vi l, but that might be just a bit harsh--you decide. Using vi is similar to using other editors in that you can see your file on the screen (this is not the case with a line editor, for example), move from point to point in the file, and make changes. But that's where the similarities end. Cryptic commands, a frustrating user interface, and the absence of prompts can all drive you up a wall. Still, if you focus on a few basics, you'll get the job done.

Command and Input Mode

The hardest thing to understand about vi is the concept of modes. When using vi, you're always in either Command or Input mode. Unfortunately there's no clue as to which mode is currently active. In Command mode, you can move the cursor, search for characters, and delete existing text. But to enter or edit new text, you have to switch to Input mode.

When you start vi, you're in Command mode. To enter Input mode, type the letter a (lowercase only) to signal that you want to add text after the cursor position. Press esc to switch back to Command mode at any time.

Here's how to create a file from scratch using vi. To start, create a new file named cow.joke by typing

vi cow.joke

You'll see a screen that looks like this:

|

~

~

~

~

~

~

~

~

~

"cow.joke" [New file]

Adding New Text to Your File

Your cursor (the vertical bar at the top of the figure below is in the upper-left corner of the screen, and the message at the bottom tells you that a new file called cow.joke was just created. The tilde characters in the first column are just placeholders for empty lines.

Now press the letter a to enter Input mode and type the lines shown in here. Press enter at the end of each line to go on to the next.

Jane: Knock, knock...

Bill: Who's there?

Jane: The Interrupting Cow.

Bill: The Interrupting Cow wh...

Jane: MOOOOOO!

|

~

~

~

~

"cow.joke" [New file]

Saving Your Work

So far, so good--let's save this little masterpiece. You're still in Input mode, so press esc to enter Command mode; then type ZZ (to put your file to sleep). You won't see any Z's on the screen, but after you've entered the second Z, your file will disappear, your Linux command prompt will return, and you'll see this message, indicating that your file was successfully saved:

"cow.joke" 6 lines, 113 characters.

Congratulations--you've just survived your first encounter with vi. You know that the a command switches to Input mode, esc gets you back to Command mode, and ZZ saves the file, but you'll have to expand this limited repertoire to get any real work done.

Common vi Commands

Have a look at this list of common vi commands (there are many more, but these will at least allow you to get some basic work done). Then we'll do one more exercise before moving on.

Note: As with all of Linux, vi commands are case sensitive.

Positioning the Cursor

Move cursor one space right.
Move cursor one space left.
Move cursor up one line.
Move cursor down one line.
ctrl-F Move forward one screen.
ctrl-B Move backward one screen.
$ Move cursor to end of line.
^ Move cursor to beginning of line.
:1 Move to first line of file
:$ Move to last line of file
/ Search for a character string.
? Reverse search for a character string.
x Delete the character at the cursor position.
dd Delete the current line.
p Paste data that was cut with x or dd commands.
u Undo.

Entering Input Mode

a Add text after the cursor.
i Insert text before the cursor.
R Replace text starting at the cursor.
o Insert a new line after the current one.

Entering Command Mode

esc Switch from Input mode to Command mode.

Exiting or Saving Your File

:w Write file to disk, without exiting editor.
ZZ Save the file and exit.
:q! Quit without saving.

Trying Out Some vi Commands

Here's another example to try out some of the vi commands. Enter the following command to fire up vi again, and you should see the file as we left it in the last example:

vi cow.joke

Changing Text

Let's change Bill's name to Biff on the second line. To do so, use the arrow keys to position your cursor on the third character of line 2 (the letter "l" in Bill); then press x twice (to delete the two l's). Now press i (to enter Input mode) and then type ff to complete the change from Bill to Biff.

Tip: Be careful about pressing the arrow keys while you're in Input mode. In some versions of vi you can position the cursor only in Command mode. Yuck.

You could also have used the R command to do this job of replacing text, so use it to change the other Bill now. Press esc to enter Command mode; then type /Bill to search for the word Bill. The cursor should move to line 4, right to where Bill is located.

Now position your cursor on the third character (the letter "l"), press R to replace the characters, and type ff. Both Bills should now be Biffs.

Adding and Deleting Lines

Here's how to add or delete a line. Press esc to enter Command mode; then press the o key to add a new line. You're in Input mode again, so you can type whatever you like on this new line. But that would ruin the joke, so delete this new line by pressing esc and then entering the dd command. The line you just added should go away.

Quitting without Saving Your Changes

Hmmm. . . . "Biff" just doesn't have that wholesome ring to it, so let's forget about all the changes we've made in this editing session and exit vi without saving the file. Make sure you're in Command mode, enter the :q! command, and then press enter. Your Linux prompt should return, and the cow.joke file will be just as it was before.

Parting Words about vi

Using vi can be frustrating, but it really isn't rocket science once you get used to the concept of the two modes and get the hang of when it's okay to move your cursor or enter text. If you're ever unsure about which mode you're in, simply press esc once or twice, and you can be sure you're in Command mode.

There are some powerful (but arcane) commands that diehard vi users use to get things done quickly in this relic-of-the-sixties text editor. The man vi command will tell you a lot more about vi if you decide you want to become proficient.

The Usenet newsgroup comp.editors is a good place to discuss vi or ask questions. You can find the vi FAQ (Frequently Asked Questions) file on the Web at http://www.faqs.org/faqs/editor-faq/vi/.

For more information on the vi command, see the vi manual.

Previous Lesson: Linking Files
Next Lesson: The Emacs Editor

[ RETURN TO INDEX ]


   

Comments - most recent first
(Please feel free to answer questions posted by others!)

Seekly     (04 Apr 2013, 12:13)


ICQ 586-179-503
tel:+380958561468


22419n()1()
2144n()1()
7 . () 3,96 (PWL-7)
23-4/14-1
3-50-7-1-
364n-
()
10-0067 b AL red 7
28-32/271 ---14448
23945n()1()
24230n1(2)()
1625 86 .
TNC- RG-58 (TNC-C58P) (TNC-7401A) (GT-205A)
1-45-2 ---10527
21623
TFT FG050701DWSWBGL2 33., 0.5 DI
2337n()1()
50-283 . 1990-2010 .
1-50-3
13-102/27-1-(,,,,)()
--09-24/271(2,3,11,12,13,14,15)
2 3 () 2 (PBD-6N) (PBD-2 203)
6-50-0-2-
4-141, BNC "" RG-59, SAT-602,
ALPINE 7524 /.


53097
earth dragon     (07 Oct 2012, 07:45)
hey Waliko Nkhoma !

I am hoping you have already found the answer to your question but i noticed none were given here so I thought I'd give it a go.

yum install vi
will give you 'no package found'

there are different versions of vi !
<vi> and a newer enhanced version <vim>

yum install vim-minimal provides vi
yum install vim-enhanced provides vim

many pages explain the diffs, but if you are used to vi I would go with vim-minimal.

Happy Day

the package you need to intall is
<a href="http://survie.net/v4/entry.php?200128-www.forexbroker.info-There-is-no-occasion-to-prepossess-the-reader-with-an-opinion-of">www.forexbroker.info</a>     (05 Sep 2012, 17:39)
The grand vizier Giafar, who had always been the spokesman, established by their familiar companionship with Jesus during His When Saniel and Glady reached the street, the rain that had fallen since
adjzoq5     (01 Sep 2012, 22:54)

lt0831sm
Waliko Nkhoma     (06 Aug 2012, 07:48)
i have just installed redhat 6 on my notebook and it seems like they is no vi and emacs editors and i need to download and install them, how can i do that?
And how can i download and install wine for windows applicaions compatibility?
varun     (31 Mar 2012, 16:16)
hello sir/mam
i wanna to run an graphics program in centos5 but everytime it could show the error graphics.h could not open plz give me the solution for this problem and plz tell me how to run an graphics programme using vi editor if there is any other option expect using cc or gcc
wagner     (06 Mar 2012, 10:23)
First thank you for the tutorial.
I was working with the text editor nano, but now I play the vi text editor, and I stab complicated by lack of information.
I'll be arriving queries thank you very much.
Matt Gilbert     (14 Feb 2012, 11:47)
It is entirely possible that vi is evil. that is why I sym linked /usr/bin/vi to /usr/bin/jstar

not a moment too soon, as I suddenly was able to resist the urge to stick a fork in my eye that using vi always inspires.
zivai     (09 Feb 2012, 07:52)
dear sir
how about if i want to quit after i have saved my file
Adiler Bertagna     (06 Feb 2012, 07:24)
Thanks !!!
Aijaz     (11 Dec 2011, 13:09)
Author of this web is liable to praise. Understanding Linux before accessing the web was not easy for me.
Neetha     (10 Dec 2011, 00:28)
Hi.. Can anybody explains how vi * works.
Nick N     (08 Dec 2011, 06:38)
Dr Bob, this linux was so lowfat i didnt even feel the effort on digesting it. Thanks for these clear instructions.
UK
anil     (25 Nov 2011, 04:57)
superb...very easy to understand...god bless you....happy holidays
Chris     (22 Nov 2011, 21:43)
was a master of vi some moons and moons ago. forgot all of them. now, my rusty vis is fresh again. thanks
Adisa     (22 Nov 2011, 11:18)
Thanks a million, its really helpful
BHUSHAN G. DANGRE     (20 Nov 2011, 20:08)
BHUSHAN G. DANGRE the 1st ever President of ACSES and the 1st Cultural Secretary from Computer Science Department from G.C.O.E.C. is now in the world of Dr. Bob and LowFat Linux.
Rakesh uniyal     (18 Nov 2011, 13:16)
Really it's very helpful and easy.
Rakesh singh uniyal     (18 Nov 2011, 13:13)
Really it's very helpful and easy.
NewApple     (08 Nov 2011, 12:17)
Really very Informative and very helpful for beginners.
raju     (07 Nov 2011, 22:59)
thank you....
Bhushan G. Dangre     (07 Nov 2011, 21:11)
Linux is new to me my company wants me to work on it . Thanks a lot I am enjoying it with your guidance .
pipi     (14 Oct 2011, 05:40)
thanx,dat was really helpful
subrahmanyam     (30 Sep 2011, 03:53)
Dear frineds iam very new to linux iam unable to save the data which i have typed in the directory ...

pelase help me ....
Siri     (13 Sep 2011, 06:55)
But :q gave error
E37: No write since last change (add ! to override)
Jessy     (26 Aug 2011, 08:14)
:wq write and quit
:w write
:q quit without saving
:q! prompt quit without saving
alf55     (18 Aug 2011, 14:54)
You should mention that vim (Vi Improved) is much better than just vi and that "gvim" (Graphical Vi IMproved) version is really nice to use. Plus with gvim, you can choose to use the "vi" commands and be able to use your mouse as intended. That way you can work in the non-gui version when needed.

Actually the UNIX spec requires an editor that is compatible with vi. In Linux this is usually VIM.

If a link called "vi" is used to launch vim it will behave as Vi rather than as vim (same is true for 'ex'), which is why most distros use an alias for vim that is called vi.
Sohrab Khan     (10 Jul 2011, 06:05)
Dear Sir,
Plz sent me the full script of vi.
Mike     (02 Jun 2011, 22:06)
THANK YOU! for this excellent writeup just as other excellent writeups.
Lateefah     (21 Apr 2011, 04:49)
Thanks that was really helpful.
JayantD     (08 Apr 2011, 05:23)
Hi, this is good article about vi editor.
thanks for it
Bhushan     (05 Apr 2011, 03:40)
Hi,

I am using vi editor on Linux-64 (Red Hat). After saving a file (!w) it simply hangs instead coming back on insert mode. I had to kill the job and reopen it to edit the file. Any ideas, what's happening?

Thanks
Manju     (04 Apr 2011, 14:18)
Great !!!! Thank you
Vadim     (29 Mar 2011, 09:40)
Thank you !!!!!! After the above tutorial vi is not so bad, for some basic editing anyways...
kiko     (27 Mar 2011, 22:15)
Hey Doc this is great thanx a lot!!!
harneet     (08 Mar 2011, 04:41)
hello sir.....
thanx for providing us a great introductory knowledge of vi editor in linux.......

aarti     (08 Mar 2011, 04:39)
thanx for providing us such a good knowledge of vi editor in linux......
Bismillah Khan     (19 Feb 2011, 09:33)
Hello Sir,
I am new on our page. can you please teach me shell scripts.
Vusi     (11 Feb 2011, 09:43)
Hi Doc, been reading your site I am a begginer want to learn Linux Please help with Tutorials. Great site you have here, thank you in advance.
Kranthi     (07 Feb 2011, 15:29)
Hey dude ..It is very great work .It helped me a lot ..I really appreciate your work..
birla     (07 Feb 2011, 00:11)
how compile and run program in vi editor
Hiii     (02 Feb 2011, 15:52)
Thanks it was very easy to understand.Please send me more tutorial of unix/linux i am begineer.
sandip     (23 Jan 2011, 01:46)
Its vary easy to understand really good
pratik kamble     (18 Jan 2011, 21:53)
I find realy this web site or office site realy good and best for any type of searching related to our tutorial or any thing what we want
bhavani     (18 Jan 2011, 03:49)
Nice work...
It is very clear to understand...
Thank's for ur hardwork..........
shiva     (13 Jan 2011, 02:52)
Dear sir,

please sending me a basic Linux and vi editor commands.am the beginner.kindly please help me regarding the above .hope u.
Shobhit Agrawal     (07 Jan 2011, 19:51)
This like a Ready reference for Quick informations.
I like it.

I appreciate your effort!!
kk29     (23 Dec 2010, 02:27)
thanks this tutorial help me for start the unix and linux
Rajendra     (20 Dec 2010, 10:37)
I searched many sites for Editors in Linux/Unix but could not find the proper solution in any, When I got this site and tried it got the solutions. This is really amazing..

Thanks.
Ahmad Hussain     (16 Dec 2010, 22:38)
Thx lot help us by this tutorial , I am new in linux and want to do c++, before this tutorial I had no clue that what to do , but now my vision is clear and I knew exactly what to do. Thanks lot
Ahmad Hussain     (15 Dec 2010, 15:27)
I am new in linux and having problem to using the vi editor and c++ , but from this example , I got whole idea how this gonna work. I really very thankful of you from bottom my heart.Thx lot you are star.
sandya     (04 Dec 2010, 12:38)
hey ya thts really helpful and could understand clearly.thank u.
Jozsef     (03 Dec 2010, 09:56)
Vim is the best editor in the whole wide world :)
You can't even imagine all things Vim or Vi can do.
rayninfo.co.uk/vimtips.html

Best,
Jozsef
Amir     (19 Nov 2010, 16:35)
That's very understandable and clear work
elisseket     (03 Nov 2010, 23:45)
It is my first message here, so I would like to say hallo to all of you! It is really comfort to go your community!
Dr. Sankar     (03 Nov 2010, 04:50)
This is one of the best sites to learn the Basic Linux commands. Thank you so much for your Contribution...
Subbu     (22 Oct 2010, 03:34)
Getting error while trying to save tcl file in linux with esc-q-!
E138: Can't write viminfo file /home/sustenance/.viminfo!
Alex Rum     (07 Oct 2010, 00:34)
Thanks a lot for this great article. As much as I hate Linux/Unix, your article makes usage of this revolting environment surviveable.
VISHAL PATIL MGM     (27 Sep 2010, 03:33)
Thank you sir
providing us such type of basic and easy to understandable commands
Jay     (09 Sep 2010, 18:46)
This is a good cheat sheet. Is there a place where I can find advance VI commands... like more search options etc. Here is a command that I use often for copy/pasting block of text.
mk --> Mark the beginning of the text to be copied
"ay'k --> Copies text upto this line into the "clipboard"

"ap --> Inserts the copied text from the line below the cursor
sree     (30 Aug 2010, 12:52)
I agree with mrboo. VI is a very powerful text editor. Once we become familiar with it, it will not seem difficult but we will enjoy using it. There are a lot of options that can be used, like deleting a line, a paragraph, repeating a line, a paragraph etc etc.. But this introductory tutorial is fine.
AMAN     (30 Aug 2010, 00:33)
thanks for this tutorial to help the program
mrboo     (25 Aug 2010, 20:42)
This is a great basic tutorial. I am impressed.

I use vi on a daily basis. I can't tell you how many times I have enjoyed the power of vi when it comes to text processing. Take character substitution or insertion for example.

It is one of the main reasons I am so hooked on VI as a Linux/Unix sysadmin.

I frequently use this type of command

:12,43 s/Charles /John /g

to replace all instances of Charles with John between lines 12 and 43. Try that kind of precision with Word or other programs.

VI, although created in the old days, has power that few modern word processors can even approach. You just have to take the time to use it and let it grow on you. Soon, it will change from being VIle to VItal to your sysadmin job.
Omar     (18 Aug 2010, 12:10)
Great help! Really easy to understand :)
Mongol Power     (12 Aug 2010, 04:01)
Thank you for you wonderful easy to use tutorial
ben     (09 Aug 2010, 08:50)
Hi nice and easy tutorial..thx
sikender yadav     (29 Jul 2010, 14:05)
great info.but i send a new command (r) plz use it for copy line.
salome masinde     (22 Jul 2010, 02:53)
thanks so much for that tutorial it has really helped me to study for my CAT 2day
Satish     (19 Jul 2010, 22:11)
thank you..giving to me importent information..keep it up..
Ankit     (17 Jul 2010, 03:44)
Hi.......thanx for providing such a simple and easily executable document, hope to see such practises in future also.
Bob Rankin     (16 Jul 2010, 15:09)
@Somya - Which "basic" commands do you think are missing?
Somya     (15 Jul 2010, 20:41)
it's not great information about VI.because Basic vi command are not there.
Amar     (15 Jul 2010, 00:42)
Thank you for such a tutorial which help me to solve some of the Important problem in linux.
ram     (10 Jul 2010, 02:51)
Hi it is very nice document......thank u so much.
rasmeet     (05 Jul 2010, 00:03)
thanks .this means lot to me
amazed     (02 Jul 2010, 07:29)
I had the unfortunate experience of needing to use vi for the first time recently. I almost lost my mind. As I'm sure you almost do reading the comments, some of which are ludicrous. How so some people become so demanding and ungrateful? And why do poeple take the time to type out a question when the answer is almost slapping you in the face? Thank you.
Ravi Chandran     (29 Jun 2010, 07:57)
This tutorial really means a lot to the beginners like me. This tutorial helped me to learn Vi editor in less than 2hrs.

Thanks for sucha wonderful tutorial.
Aslam     (28 Jun 2010, 11:26)
Easy to understand these important commands...Thanks
Tawhid     (22 Jun 2010, 04:42)
I am new to linux, and this is like my bible for vi stuff. Thanks alot.
Karan gahdge     (22 Jun 2010, 03:01)
Your tutorial we really usefull for me

Thanks a lot
roli mishra     (20 Jun 2010, 05:23)
this is the most knowlege giving ,and easily understandable tutorial which one can easily configure
Jack Wade     (18 Jun 2010, 14:48)
It'd be neat if you revised this and included some stuff on Vim.
Pradeep     (08 Jun 2010, 13:22)
Really it helped me a lot... Thanks for this info...
Ed     (06 Jun 2010, 18:52)
Great guide. Wacko tool.
anilkumar     (04 Jun 2010, 03:04)
thank you very much for giving great info of starters............
sir magneto     (19 May 2010, 11:06)
Thi sis great. here's an additional one I use: lowercase r, allows single character replacement
Srinivasan     (11 May 2010, 03:36)
Very good .Thanks for information
Sam     (26 Apr 2010, 22:20)
How do I assign the file permission at the point the file is created? Without exit vi and then chmod..
Jason     (26 Mar 2010, 08:14)
I remember h,j,k,l keys are featured by the vimtutor. More convenient substitute for arrows..

BTW, I love this website!
Reader     (18 Mar 2010, 11:19)
Thanks for sharing, great as a quick handy notes to refer to...
Bob Rankin     (22 Feb 2010, 05:25)
I'm starting to think there's a browser bug that makes the ARTICLE invisible and only shows the comment form...
Bhupinder Kaur     (21 Feb 2010, 03:57)
plz give me informai=tion about Vi editor commands
mahesh     (16 Feb 2010, 22:18)
thanx for sharing...it helped me doing programs in linux thru vi editor
Rajesh B     (11 Feb 2010, 23:59)
great the information provided is easy to understand............
thanks
SantiagoP     (22 Jan 2010, 03:23)
Thanks for this entertaining tutorial of the greatest vintage program

I welcome your comments. However... I am puzzled by many people who say "Please send me the Linux tutorial." This website *is* your Linux Tutorial! Read everything here, learn all you can, ask questions if you like. But don't ask me to send what you already have. :-)

NO SPAM! If you post garbage, it will be deleted, and you will be banned.
*Name:
Email:
Notify me about new comments on this page
Hide my email
*Text:
 
 


Ask Bob Rankin - Free Tech Support


Copyright by - Privacy Policy
All rights reserved - Redistribution is allowed only with permission.
More To Explore