Using the vi Editor

9 387 0
Using the vi Editor

Đang tải... (xem toàn văn)

Thông tin tài liệu

Using the vi Editor Objectives Upon completion of this module, you should be able to: • Differentiate between the three odes of vi and identify the commands that belong to each mode • Start vi • Position and move the cursor in vi • Create text in vi • Delete text in vi • Copy or move text in vi • Set vi options • Exit the vi editor • Perform search and replace functions within vi Discussion – Editing system files is a powerful tool for customizing the Linux environment. What tools would you use to edit system files? Introduction to vi The visual display (vi) editor is an interactive editor that is used to create and/or modify text files. The vi editor uses a screen display, but you cannot use the mouse to position the cursor. All editing with the vi editor is done within a buffer. Changes can be written to the disk or discarded. It is important for users who are learning to become system administrators to know how to use vi. It is the only full screen editor that can be used to edit crucial system files. This skill is also needed in case the windowing system is not available.  Depending on the instructor’s style, this module may be best taught by covering the basic modes and commands of vi contained in the lecture parts of this module in a summary fashion. The purpose of the lab for this module is to allow the students to learn vi by using it. The lab covers in a practical fashion much of what is covered in the module, along with some things that are not. vi Modes There are three modes of operation in vi: • Command mode • Entry mode • Last-line mode When you open a file with vi, you are in command mode. In this mode, you can enter positioning and editing commands to perform functions. While in command mode you can do advanced editing commands by typing a colon (:), which places you at the bottom line of the screen. This is called last-line mode. However, all commands are initiated from command mode. You must be in entry mode to enter text. To enter text, you must type a vi insert command such as i, o, or a. This takes vi out of command mode and puts it into entry mode. In this mode, text will not be interpreted as editing commands. When you finish entering text in your file, press the Escape key to return to command mode. Invoking vi Command Format vi [option(s)] [filename] view [filename] Example To create a new file, invoke vi with a new file name by typing $ vi filename Table 9-1 contains commands you can use to create, edit, or view a file. Table 9-1 : vi Commands Command Meaning vi filename Open or create a file vi Open a new file to be named later vi -r filename Recover a crashed file view filename Open a file as read-only  Consider having students run vi with set showmode set from the beginning. Input Commands To append or insert text, use the following options: Table 9-2 Append and Insert Commands Command Meaning a Append text after the cursor A Append text at the end of the line i Insert text before the cursor I Insert text at the beginning of the line o Open a new line below the cursor O Open a new line above the cursor Positioning Commands The following pages list the vi editor’s editing and positioning commands, which are used to make changes. The vi editor is case sensitive, so use the specified case when using the editing and positioning commands. The Table 9-3 shows the key sequences for character movement:. Table 9-3 : Key Sequences Command Meaning h, ←, or Back Space Move left one character j or ↓ Move down one line k or ↑ Move up one line l,→, or Space bar Move right (forward) one character w Move forward one word (including punctuation) W Move forward one word (past punctuation) b Move back one word (including punctuation) B Move back one word (past punctuation) e Move to end of current word $ Move to end of line 0 (zero) or ^ Move to beginning of line Return Move down to beginning of next line H Move to top of screen M Move to middle of screen L Move to bottom of screen Control-f Page forward one screen Control-d Scroll down one-half screen Control-b Page back one screen Control-u Scroll up one-half screen Editing Commands Deleting Text To delete text, use the following options: Table 9-4 Text Deletion Commands Command Meaning x (lowercase) Delete character at the cursor X (uppercase) Delete character to the left of the cursor dw Delete word (or part of word to right of cursor) 3dw Delete three words dd Delete line containing the cursor 3dd Delete three lines D Delete line to right of cursor (from cursor position to the end of the line) dG Delete to end of file d1G Delete from beginning of file to cursor :5,10d Delete lines 5 through 10 Undoing, Repeating, and Changing Text Commands To change text or to cancel or repeat edit functions, use the following commands. Many of these commands change you to Insert mode until you press Escape. Table 9-5 Editing Commands Command Meaning cw Change word (or part of word) at the cursor location to the end of the word 3cw Change three words R Overwrite or replace characters on line C Change from cursor to end of line s Substitute string for character(s) r Replace character at cursor with one other character i (Return) Break line J Join current line and line below xp Transpose character at cursor and character to the right ~ Change case of letter (upper or lower) at cursor u Undo previous command U Undo all changes to current line :u Undo previous last-line command Copying and Pasting Text To copy and paste text, use the following options: Table 9-6 Copy and Paste Commands Command Meaning yy Yank a copy of line Y Yank a copy of line p Put yanked or deleted line below current line P Put yanked or deleted line above current line :1,3 co 5 Copy lines 1 through 3 and put after line 5 :4,6 m 8 Move lines 4 through 6 to line 8 (line 6 becomes line 8; line 5 becomes line 7, and line 4 becomes line 6) Note – Both delete and yank write to a buffer. When yanking, deleting, and pasting, the put commands insert the text differently depending on whether you are pasting a word(s) or a line(s). Saving and Quitting Files To save and quit a file, use the following options: Table 9-7 Save and Quit Commands Command Meaning :w Save changes (write buffer) :w new_filename Write buffer to new_filename :wq Save changes and quit vi ZZ Save changes and quit vi :q! Quit without saving changes :wq! Save changes and quit vi (The ! will override read only permissions if you are the owner of the file.) Advanced Editing Options The vi editor includes options for customizing your edit sessions, such as: • Displaying line numbers • Displaying invisible characters such as tab and end-of-line characters The set command is used from last-line mode to control these options. These options can also be placed in a file the user creates in their home directory called .exrc. The set options are placed in this file, without the preceding colon, one command to a line. Once the .exrc file exists, it is read by the system each time a vi session is opened. Table 9-8 Edit Session Customization Commands Command Meaning :set nu Show line numbers :set nonu Hide line numbers :set ic Instruct searches to ignore case :set noic Instruct searches to be case sensitive :set list Display invisible characters such as tab and end-of-line :set nolist Turn off the display of invisible characters :set showmode Display current mode of operation :set noshowmode Turn off mode display :set Display all vi variables set :set all Display all possible vi variables and their current settings To find a line or to search and replace and do advanced editing, use the following options: Table 9-9 Advanced Editing/Search Commands Command Meaning G Go to last line of file 1G Go to first line of file :21 Go to line 21 21G Go to line 21 To clear the screen or insert files, use the following options: Table 9-10 Clearing/Insertion Commands Command Meaning Control-L Clear (refresh) scrambled screen :r filename Insert (read) file at line after cursor :34 r filename Insert file after line 34 To search and replace text, use the following options: Table 9-11 Search and Replace Commands Command Meaning /string Search forward for string ?string Search backward for string n Find next occurrence of string N Find previous occurrence of string :%s/old/new/g Search and replace globally Creating and Saving a File To create a file using the vi editor: 1. Type vi filenameto create the file. 2. Type i to insert text. 3. Press the Escape key to change to command mode. 4. Type :wq to write the file and exit vi. Note – The Escape key always puts you in command mode. Use the Escape key if you are not sure what mode you are in. If you press the Escape key while you are in command mode, the workstation beeps as a reminder that you are already in command mode. Exercise: Using the vi Editor Exercise objective – In this exercise you will practice creating and editing using vi. Tasks Complete the following step: 1. In your home directory there should be a file called tutor.vi. Make sure that you are currently in your home directory, then open this file with the command: $ vi tutor.vi this will open up a vi tutorial. Complete the lessons outlined in this tutorial. Exercise Summary Discussion – Take a few minutes to discuss what experiences, issues, or discoveries you had during the lab exercises.  Manage the discussion here based on the time allowed for this module, which was given in the “About This Course” module. If you find you do not have time to spend on discussion, then just highlight the key concepts students should have learned from the lab exercise. • Experiences  Ask students what their overall experiences with this exercise have been. You might want to go over any trouble spots or especially confusing areas at this time. • Interpretations  Ask students to interpret what they observed during any aspects of this exercise. • Conclusions  Have students articulate any conclusions they reached as a result of this exercise experience. • Applications  Explore with students how they might apply what they learned in this exercise to situations at their workplace. Check Your Progress Before continuing on to the next module, check that you are able to accomplish or answer the following: • Differentiate between the three modes of vi and identify the commands that belong to each mode • Start vi • Position and move the cursor in vi • Create text in vi • Delete text in vi • Copy or move text in vi • Set vi options • Exit the vi editor • Perform search and replace functions within vi Think Beyond Under what conditions might it be necessary for you to use the vi editor in your work environment? . • Start vi • Position and move the cursor in vi • Create text in vi • Delete text in vi • Copy or move text in vi • Set vi options • Exit the vi editor. files? Introduction to vi The visual display (vi) editor is an interactive editor that is used to create and/or modify text files. The vi editor uses a screen

Ngày đăng: 02/10/2013, 09:20

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan