Lesson 4 Mastering the Visual Editor Overview n













































- Slides: 45

Lesson 4 -Mastering the Visual Editor

Overview n Introducing the visual editor. n Working in an existing file with vi. n Understanding the visual editor. n Navigating through a file. n Adding text to a file. n Avoiding confusion while entering commands to shell and vi.

Overview n Deleting text from a file. n Undoing editing commands. n Creating new files with the visual editor. n Making changes to the text. n Moving and copying text. n Writing a file and quitting the editor.

Introducing the Visual Editor n Bill Joy, a student of the University of California, wrote the vi editor. n The UNIX/Linux visual editor is a powerful, fast, commanddriven screen editor. n The vi visual editor is available on all systems and is an essential tool. n The vi editor allows the movement of cursor to a specific location in a file.

Introducing the Visual Editor n The vi editor is present on every UNIX environment, uses few computing resources, and includes many tools. n Standard UNIX releases include vi, and Linux provides vim, which includes some vi improvements. n Command mode and append/insert are the two modes of the vi editor.

Introducing the Visual Editor n The command mode allows a user to access a file with basic vi commands. n The append/insert mode allows a user to insert data characters in a file.

Working in an Existing File with vi n Files are central to the UNIX computing environment. n The visual editor is used to create new files and edit existing files. n The vi editor does not permit text to be added directly when editing a file. n The “cat” utility can be used for creating a file in UNIX.

Working in an Existing File with vi n We can edit the files created by the cat command. n We can access/open the files by giving the "vi filename" command. n The command will open the files in the command mode.

Understanding the Visual Editor Moving around in a file: n Opening a file in vi allows a user to delete, move, copy and paste and save and quit the file. n A user cannot insert characters while in the command mode. n The fundamental way to move the cursor in a file is to do it with the direction keys.

Understanding the Visual Editor Moving around in a file (continued): n The vi editor interprets the h, j, k, and l keys as a command to the move the cursor in specific directions. n The workstation either flashes or beeps if an attempt is made to move the cursor beyond the existing text.

Understanding the Visual Editor Moving around in the file (continued): n The vi editor also provides a forward search command for moving the cursor to a specific word in the text. n The /text is the forward search command for moving the cursor to a specified text. n The “n” command can be used for locating the next occurrence of a target string specified in the forward search command.

Understanding the Visual Editor Working with text: n To insert characters to the file, we have to change from the command mode to the insert/append mode. n The vi editor provides the dd command for deleting an entire line of text from a file. n The “dd” command even deletes blank lines.

Understanding the Visual Editor Working with text (continued): n After dd finishes deleting, it remains in the command mode. n The “x” (x-out) command deletes only a single character under the cursor. n The “a” command switches the vi editor from the command mode to the append mode and allows text to be entered to a file.

Understanding the Visual Editor Working with text (continued): n The “u” command undoes the last text change made to the file. n The ESC key instructs the vi editor to switch from the append/insert mode to the command mode. n A user can check the mode he is working in with the ESC key. n The “o” command tells the editor to open a new blank line between the cursor line and the next line in the file.

Understanding the Visual Editor Quitting the editor: n The “wq” command instructs the editor to write the file and quit the editor. n The “q” command can be used for quitting the editor if no changes are made to the file being edited. n The “q!” command instructs the editor to quit without saving the changes made to the file.

Understanding the Visual Editor Creating and reading a file in the editor: n A new file can be created in the vi editor by specifying the filename with the vi command. n A file opens in the command mode. Text can be entered by switching to the insert/append mode. n The “r” or the “read” command locates a file and reads its contents into the current file, starting immediately after the cursor line.

Navigating Through a File Using the direction keys: n The arrow keys and the h, j, k, and l direction keys, either alone or in conjunction with the number keys, make it possible to move the cursor to any character in a file. n The “set number” command tells the editor to display the line number on the screen. n The commands “G” and “: $” instruct the editor to move to the last line of a file.

Navigating Through a File n Lowercase “n” or “/” (slash) can be used to instruct the vi editor to search forward, whereas uppercase N or ? (question mark) is used to search in the backward direction. n The “set ignorecase” command can be used to switch off the case-sensitivity feature of vi’s search mechanism. n The “set” command can be used to view the environmental features that are currently set.

Navigating Through a File n The “w” command can be used to advance the cursor to the beginning of the next word. n The dollar sign ($) instructs the editor to move the cursor to the end of the current line. n The caret (^) and the zero (0) move the cursor to the beginning of a line.

Navigating Through a File Moving the Cursor in vi

Navigating Through a File Moving to a Specific Textual Character in vi

Navigating Through a File Moving the Cursor by Line Address and Location Display

Navigating Through a File n Two single quotation marks instruct the editor to return to the previous location in a file. n The display adjustment commands are used to adjust the workstation’s screen display to move forward or backward to a different block or section of code, regardless of its context.

Navigating Through a File n CTRL-F displays the next screen of a text file. n CTRL-D scrolls down one half screen of a text file. n CTRL-D scrolls up one half screen of a text file. n CTRL-B displays the previous screen of a text file.

Adding Text to a File n The “i” (insert) command starts adding text to the left of the cursor. n The “a” and “i” commands instruct the editor to move from the command mode to the append mode and start adding text at the cursor position. n The “O” command is an instruction to open a line above the line where the cursor resides and shift to the append/insert mode.

Adding Text to a File n The “I” command instructs the editor to move the cursor to the beginning of a line and to change to the append mode. n The “A” command moves the cursor to the end of a line and switches the editor to the append mode. n The I and A command instructs the editor to switch to command mode.

Avoiding Confusion While Entering Commands to Shell and vi n The “who” command, when executed in a shell, lists the users currently logged on. n In the vi’s command mode, it moves the cursor to a specific position and moves over to the append mode. n In vi’s append mode, the characters are entered directly into the file being edited.

Deleting Text From a File n Multiple lines, words, and characters can be deleted by prefacing the delete command with a number. n The uppercase “D” command instructs the editor to delete the remainder of the line starting from the character under the cursor. n The “df*” command deletes the text from the cursor to the first character * on the current line to the right of the cursor.

Deleting Text From a File n The vi editor also allows lines to be deleted using the line number addresses. n The d command also allows a comma to be used for specifying a range of lines to be deleted. n Commands that employ line addresses require the ENTER key to be pressed after the command for execution.

Undoing Editing Commands n The “u” command undoes the last editing change in the file. n The vi editor in Linux allows multiple changes to be undone. n The “U” command undoes any number of changes made to the current line where the cursor is located.

Creating New Files with the Visual Editor n The vi filename command is used for creating a new file as well as editing an existing file. n The vi makes a buffer copy of a file if it exists, else it starts editing a memory buffer using the new filename. n A file created in vi can be made executable by executing the chmod command.

Making Changes to the Text n The “r” command instructs the editor to replace the character located under the cursor with the very next character typed in by the user. n It can also be used for breaking up a long line into parts. n The uppercase “J” command can be used for joining two lines of text.

Making Changes to the Text n The “R” command switches the editor to the “type-over” mode, wherein each letter typed replaces the character under the cursor as it moves down the line. n The “s” command substitutes the character under the cursor with the text typed in by the user until the ESC key is pressed. n The “cw” (change word) command allows a user to substitute a word or multitude of words for a single word.

Making Changes to the Text n The “cc” command deletes the text from the current line and moves you from the command mode to the append mode. n The “C” command puts the editor into the append mode and allows a user to change part of the line from the cursor position to the end of the line. n The substitute command, a colon command, is used to exchange one regular expression for another in a file.

Making Changes to the Text n The substitute command requires two words to be separated by slashes. n The “g” at the end of the substitute command is a flag and can be used for replacing all occurrences of the target pattern with the addressed lines. n The substitute command can also be restricted to lines having specific content.

Moving and Copying Text n The “yy” command instructs the editor to copy the contents of the line where the cursor resides. n The “p” or “P” command is used for pasting a new line in a new location below the cursor line. n The dd and p commands can be combined to perform a cut and paste operation.

Moving and Copying Text Commands to Move and Copy Text

Moving and Copying Text Marking a place in the file: n The “m*” command instructs the editor to mark a line of text and assign it a label specified in the *; it can have a value from a to z. n A single quote character with the label name can be used for moving on to a specific label. n Vi allows deleting, changing, and yanking of text from the current position to the marked spot.

Moving and Copying Text The delete, change, and yank commands

Moving and Copying Text Marking a place in the file (continued): n The mark command can also be used for simplifying the tasks of deleting, moving, and copying large blocks of text by marking both ends of the block. n Marking text is a powerful way to identify lines to delete, move, or copy. n The move or copy commands can be used for moving or copying a block of text specified using line numbers.

Writing a File and Quitting the Editor n The “w” command can be used for saving a copy of the current file to a new file. n The w or write command can also be used for writing a part or block of text from the current file to a new file.

Writing a File and Quitting the Editor n The “w!” command instructs the editor to overwrite the contents of the file if it exists, else creates a new file for saving the contents of the current file. n The >> symbol, when specified with the w command, appends the text to the file specified.

Summary n The vi is the editor used to create and edit files. n Vi provides several utilities to edit text. n Append/insert and command are the two modes of the vi editor. n Files created in the cat command can be opened in vi.

Summary n A user cannot insert data without switching to the append/insert mode. n The I, a, i, A, o, and O commands instruct the editor to add text relative to the cursor position. n The vi editor provides the change (c), yank (y), and delete (d) operators for text operations.

Summary n The vi provides various utilities to move, edit, delete, and replace text. n The : wq is used for saving and quitting the editor. n The vi editor facilitates global changes, where the user specifies the target for replacement and the replacement text. n Linux provides an advanced version of the vi, namely the vim editor.