450 likes | 464 Views
Learn the fundamentals of using the vi editor to navigate, edit, and save files in UNIX/Linux environments. Includes commands for deleting, undoing, and creating new files.
E N D
Overview • Introducing the visual editor. • Working in an existing file with vi. • Understanding the visual editor. • Navigating through a file. • Adding text to a file. • Avoiding confusion while entering commands to shell and vi.
Overview • Deleting text from a file. • Undoing editing commands. • Creating new files with the visual editor. • Making changes to the text. • Moving and copying text. • Writing a file and quitting the editor.
Introducing the Visual Editor • Bill Joy, a student of the University of California, wrote the vi editor. • The UNIX/Linux visual editor is a powerful, fast, command-driven screen editor. • The vi visual editor is available on all systems and is an essential tool. • The vi editor allows the movement of cursor to a specific location in a file.
Introducing the Visual Editor • The vi editor is present on every UNIX environment, uses few computing resources, and includes many tools. • Standard UNIX releases include vi, and Linux provides vim, which includes some vi improvements. • Command mode and append/insert are the two modes of the vi editor.
Introducing the Visual Editor • The command mode allows a user to access a file with basic vi commands. • The append/insert mode allows a user to insert data characters in a file.
Working in an Existing File with vi • Files are central to the UNIX computing environment. • The visual editor is used to create new files and edit existing files. • The vi editor does not permit text to be added directly when editing a file. • The “cat” utility can be used for creating a file in UNIX.
Working in an Existing File with vi • We can edit the files created by the cat command. • We can access/open the files by giving the "vi filename" command. • The command will open the files in the command mode.
Understanding the Visual Editor Moving around in a file: • Opening a file in vi allows a user to delete, move, copy and paste and save and quit the file. • A user cannot insert characters while in the command mode. • 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): • The vi editor interprets the h, j, k, and l keys as a command to the move the cursor in specific directions. • 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): • The vi editor also provides a forward search command for moving the cursor to a specific word in the text. • The /text is the forward search command for moving the cursor to a specified text. • 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: • To insert characters to the file, we have to change from the command mode to the insert/append mode. • The vi editor provides the dd command for deleting an entire line of text from a file. • The “dd” command even deletes blank lines.
Understanding the Visual Editor Working with text (continued): • After dd finishes deleting, it remains in the command mode. • The “x” (x-out) command deletes only a single character under the cursor. • 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): • The “u” command undoes the last text change made to the file. • The ESC key instructs the vi editor to switch from the append/insert mode to the command mode. • A user can check the mode he is working in with the ESC key. • 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: • The “wq” command instructs the editor to write the file and quit the editor. • The “q” command can be used for quitting the editor if no changes are made to the file being edited. • 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: • A new file can be created in the vi editor by specifying the filename with the vi command. • A file opens in the command mode. Text can be entered by switching to the insert/append mode. • 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: • 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. • The “set number” command tells the editor to display the line number on the screen. • The commands “G” and “:$” instruct the editor to move to the last line of a file.
Navigating Through a File • 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. • The “set ignorecase” command can be used to switch off the case-sensitivity feature of vi’s search mechanism. • The “set” command can be used to view the environmental features that are currently set.
Navigating Through a File • The “w” command can be used to advance the cursor to the beginning of the next word. • The dollar sign ($) instructs the editor to move the cursor to the end of the current line. • 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 • Two single quotation marks instruct the editor to return to the previous location in a file. • 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 • CTRL-F displays the next screen of a text file. • CTRL-D scrolls down one half screen of a text file. • CTRL-D scrolls up one half screen of a text file. • CTRL-B displays the previous screen of a text file.
Adding Text to a File • The “i” (insert) command starts adding text to the left of the cursor. • 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. • 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 • The “I” command instructs the editor to move the cursor to the beginning of a line and to change to the append mode. • The “A” command moves the cursor to the end of a line and switches the editor to the append mode. • The I and A command instructs the editor to switch to command mode.
Avoiding Confusion While Entering Commands to Shell and vi • The “who” command, when executed in a shell, lists the users currently logged on. • In the vi’s command mode, it moves the cursor to a specific position and moves over to the append mode. • In vi’s append mode, the characters are entered directly into the file being edited.
Deleting Text From a File • Multiple lines, words, and characters can be deleted by prefacing the delete command with a number. • The uppercase “D” command instructs the editor to delete the remainder of the line starting from the character under the cursor. • 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 • The vi editor also allows lines to be deleted using the line number addresses. • The d command also allows a comma to be used for specifying a range of lines to be deleted. • Commands that employ line addresses require the ENTER key to be pressed after the command for execution.
Undoing Editing Commands • The “u” command undoes the last editing change in the file. • The vi editor in Linux allows multiple changes to be undone. • 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 • The vi filename command is used for creating a new file as well as editing an existing file. • The vi makes a buffer copy of a file if it exists, else it starts editing a memory buffer using the new filename. • A file created in vi can be made executable by executing the chmod command.
Making Changes to the Text • The “r” command instructs the editor to replace the character located under the cursor with the very next character typed in by the user. • It can also be used for breaking up a long line into parts. • The uppercase “J” command can be used for joining two lines of text.
Making Changes to the Text • 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. • The “s” command substitutes the character under the cursor with the text typed in by the user until the ESC key is pressed. • 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 • The “cc” command deletes the text from the current line and moves you from the command mode to the append mode. • 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. • The substitute command, a colon command, is used to exchange one regular expression for another in a file.
Making Changes to the Text • The substitute command requires two words to be separated by slashes. • 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. • The substitute command can also be restricted to lines having specific content.
Moving and Copying Text • The “yy” command instructs the editor to copy the contents of the line where the cursor resides. • The “p” or “P” command is used for pasting a new line in a new location below the cursor line. • 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: • 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. • A single quote character with the label name can be used for moving on to a specific label. • 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): • 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. • Marking text is a powerful way to identify lines to delete, move, or copy. • 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 • The “w” command can be used for saving a copy of the current file to a new file. • 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 • 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. • The >> symbol, when specified with the w command, appends the text to the file specified.
Summary • The vi is the editor used to create and edit files. • Vi provides several utilities to edit text. • Append/insert and command are the two modes of the vi editor. • Files created in the cat command can be opened in vi.
Summary • A user cannot insert data without switching to the append/insert mode. • The I, a, i, A, o, and O commands instruct the editor to add text relative to the cursor position. • The vi editor provides the change (c), yank (y), and delete (d) operators for text operations.
Summary • The vi provides various utilities to move, edit, delete, and replace text. • The :wq is used for saving and quitting the editor. • The vi editor facilitates global changes, where the user specifies the target for replacement and the replacement text. • Linux provides an advanced version of the vi, namely the vim editor.