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
Comments (most recent first)
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
|
|
|
![]() |
|
| <Send This Link to a Friend> <Bookmark This Page> | ||