Presentation on theme: "CS 497C – Introduction to UNIX Lecture 8: The vi/vim Editor Chin-Chih Chang"— Presentation transcript:
CS 497C – Introduction to UNIX Lecture 8: The vi/vim Editor Chin-Chih Chang
The vi/vim Editor When you write some C or Java programs or shell (or perl ) scripts, or edit some system files, you need to use an editor. There are two versatile editors in UNIX – vi and emacs. vi is a full-screen editor. It was created by a graduate student – Bill Joy – later to become the cofounder of Sun Microsystems.
The vi/vim Editor vim (vi improved) is a well-known vi editor in Linux. Bram Moolenaar has made three notable improvements – multiple windows, highlighting text, and command history. A vi session begins by invoking the command vi with (or without) a filename: vi index.html You are presented a full empty screen, each line beginning with a ~ (tilde).
The vi/vim Editor The ~ is vi ’s way to indicating that they are nonexistent line. For text editing, vi uses 24 of the 25 lines that are normally available in a terminal. The last line is reserved for some commands that you’ll enter to act on the text. This line is also used by the system to display messages.
The Three Modes There are three modes in which vi works: –Command Mode – Where keys are used as commands to act on text. –Input Mode – Where any key depressed is entered as text. –Last Line Mode - Where commands can be entered in the last line of the screen to act on text. The behavior of vi is controlled by a configuration file – the.exrc file which it reads on startup.
Quitting vi – The Last Line Mode The editor works with a copy of the file which is placed in a buffer that is simply a temporary storage area which is associated with the file on disk. It is necessary to know how to leave the editor. Saving and quitting are handled by the Last Line Mode. Every command in this mode is preceded by a : (colon) and followed by the [Enter] key.
Quitting vi – The Last Line Mode Remember to leave the Input Mode by pressing the [Esc] key. The Last Line Mode offers two ways of saving and quitting - :x and :wq. The commands return you to the shell after saving your work. The quick way to save and quit the editor is to use the Command Mode command ZZ.
Quitting vi – The Last Line Mode To abort editing, you can use q (quit) command. The q command takes you out of the editor only if you don’t have a changed buffer. : q [Enter] $ The q! command always return you to the prompt irrespective of the status of the buffer.
Quitting vi These are exit commands in vi : –:x – saves files and quits editing mode –:wq – saves files and quits editing mode. –:q – quits editing mode when no changes are made to file –:q! – quits editing mode but after abandoning changes. –:sh – escapes to UNIX shell (use exit to return to vi ). –[Ctrl-z] – suspends current session and escapes to UNIX shell (use fg to return to vi ).
Inserting and Replacing Text Before you are able to enter text, you have to change from the default Command Mode to Input Mode. You can set the editor in showmode (with :set showmode ) to display a suitable message in the last line. The simplest type of input is insertion of text.
Inserting and Replacing Text When vi is invoked, the cursor is always positioned at the first character of the first line. To insert text at this position, press i. The character doesn’t show up on the screen, but pressing this key changes the mode from Command to Input. Since the showmode setting was made, you’ll see the words INSERT or INSERT MODE in the last line.
Inserting and Replacing Text Further key depressions will show text on the screen as it is being entered. This is the vi editor It is quite powerful. It operates in three modes. You can even escape to the UNIX shell. It maintains 26 buffers ~
Inserting and Replacing Text The cursor is now positioned in the last character of the last line. This is known as the current line. The cursor is stationed in the current cursor position. You can use [Backspace] to correct a mistake or erase the previous word using [Ctrl-w]. After you have entered a few lines, you should press [Esc] to take you back to the Command Mode to save your work.
Inserting and Replacing Text When i can be used to insert text anywhere in a line, I inserts text only at the beginning of a line. To append text to the right of the cursor position use a. To append text at the end of a line use A. To open a line below the current line use o. To open a line above the current line use O. Text is replaced with the r, R, s, and S commands.
Inserting and Replacing Text To replace one single character with another, you should use r followed by the character that replaces the one under the cursor. To replace more than a single character use R followed by the text and then press [Esc]. Many a time, you’ll need to replace a single character with multicharacter text. Take your cursor to the point and then press s.
Inserting and Replacing Text S replaces the entire line irrespective of the cursor position. After pressing S, the entire line vanishes from sight. Key in your text, and then press [Esc]. There is one more method of entering the Input Mode with the c operator. It will be discussed later.
Inserting and Replacing Text These 10 keys of the Input Mode are summarized as follows: –i – inserts text to left of cursor. –I – inserts text at the beginning of line. –A – appends text at end of line. –o – opens line below. –O – open line above. –r – replaces single character under cursor. –R – replaces text from cursor to right. –s – replaces single character under cursor with any number of characters. –S – replaces entire line.
Inserting and Replacing Text To input a control character, you need to use [Ctrl-v] to precede any control character. Even though you feel you are seeing a ^ (caret) and an H, there’s only a single character out there.
Saving Text (:w) These are commands of saving text: –w – saves file and remains in editing mode. –w note – saves to file note. –w! note – overwrite file note. –w >> note – appends current file contents to file note. –10,20w note – writes lines 10 to 20 to file note. –.w note – writes current line to file note. –$w note – writes the last line to file note.
Saving Text and Exit to the UNIX Shell To recover a file from a crash, use: vi –r note Sometimes, you need to make a shell escape (temporary exit) which brings the shell prompt to you. : sh $
Exit to the UNIX Shell Pressing exit or [Ctrl-d] brings you back to vi. You can also run a single UNIX command using :! With the command name. An [Enter] returns you toe the editing screen. :! date
The Repeat Factor When you prefix a number to a command, most commands interpret the instruction to be repeated as many times. This number prefixing a command is called the repeat factor. For instance, 3s replaces the next three characters under the cursor. Suppose you wish to insert a series of 20 asterisks, you can use: 20i*
The Command Mode The Command Mode is the mode you come when you have finished entering or changing your text. This mode is meant for performing navigation and cut-and-paste operations. When you press a key in the Command Mode, it simply performs its function.