![]() |

|
|||||||
| Tutorials Here you can post and read tutorials. |
![]() |
|
|
Thread Tools | Display Modes |
|
#1
|
|||
|
|||
|
Linux Tutorial: Visual Text Editor (vi)
Visual Text Editor (vi) The Visual Text Editor, vi, can be used on all CAEN UNIX machines. To edit with vi remotely (e.g. when using SSH), a terminal program (e.g. PuTTY) that supports cursor control must be used. The vi editor is a mode-orientated editor. In a mode-oriented editor, the results of specific keystrokes depend on the current mode of the editor. In vi, the two main modes are command mode, where all keystrokes are interpreted as a command, and append mode, in which keystrokes produce simple text. As with most other UNIX commands, vi commands are case sensitive. Entering and Exiting vi To invoke vi, type vi filename . The vi editor begins in command mode. To begin entering text, press the a key. The a command changes vi to append mode. In append mode, text can be entered into a file. Exit append (or any) mode by pressing the Esc key. Note: On a Macintosh the ~ key in the upper-left corner of the keyboard functions as an Esc key in vi. In order to exit vi and save the appended text, the editor must be in command mode. Press Esc to ensure that vi is in command mode, then type :ZZ or : x. This command will exit vi and save any changes made to the file. Note: Be sure to use case sensitive letters for this command. To exit vi without saving changes type :q!. Save your work without exiting vi by typing :w. Editing and Moving Around Within a File The following commands begin append mode in vi: a Appends text to the right of the cursor. i Inserts text to the left of the cursor. o Opens a new line below the cursor. O Opens a new line above the cursor. Each of these commands is useful in specific situations. For example, on a line which already has some text, the a command will not allow text to be inserted at the beginning of the line. Therefore, the i command must be used to insert text at the beginning of the line. Cursor Moving Commands The following commands are used to move the cursor in a file. Remember that they must be entered from command mode, which can be accessed by pressing Esc. h Moves left one character. l Moves right one character. j Moves down one line. k Moves up one line. :set number Shows the line numbers for the file. :set nonumber Hides the line numbers; this is the default. #G Moves to specified line number (# is any number). If no number is given, G will move the cursor to the end of the file. #w Moves # number of words forward. If no number is given, w will move the cursor one word forward. #b Moves # number of words backwards. If no number is given, b will go one word backwards. ^ or 0 (zero) Moves the cursor to the beginning of the line. $ Moves the cursor to the end of the line. CTRL-d Scrolls down about one half a screen. CTRL-u Scrolls up about one half a screen. CTRL-e Scrolls down one line. CTRL-y Scrolls up one line. CTRL-f Goes forward one screen. CTRL-b Goes back one screen. Text Deletion Commands x Erases the character under the cursor. xp Transposes two characters. X Erases the character before the cursor. dw Deletes word from the current cursor position to the beginning of the next word. A number may be given before the command to specify how many words to delete. dd Deletes the line on which the cursor is set. A number may be given before the command to specify how many lines are to be deleted. Search and Replace Commands /word Moves cursor to the next occurrence of word. Note: The search is case sensitive. n This command is issued after a /word command. n will move the cursor to the next occurrence after the first occurrence. n may be used repeatedly to find all occurrences of a word. Note: using an uppercase N will search in reverse order /[Ww]ord This is the non-case sensitive version of /word. :%s/word/new/g One method for global search and replace. All occurrences of word are replaced with new. Another method is… :FIRSTLINE,LASTLINEs/word/new/g For example, the command: :1,5s/word/new/g This changes all occurrences of word to new between lines one and five. The following will change all occurrences in the entire document because $ is a wildcard for the last line of the file: :1,$s/word/new/g Undo Commands u Undo the last command. U Undo all changes made to the current line. The cursor may not leave the line between the time that the changes are made and the time that the U command is issued. Yanking and Putting The vi equivalents for cutting and pasting text are the yank and the put commands. Yank stores the specified portion of text into a specified buffer. Put inserts the text most recently deleted or yanked. The vi text editor supports the use of multiple buffers. These buffers can be differentiated by a buffer number. If a buffer number is given when using a yank or put command, text will be put into or yanked from that particular buffer. Y Yanks lines to buffer. P Puts back text after cursor. p Puts back text before cursor. “xp Puts from buffer x. “xy Yanks to buffer x. “xd Deletes into buffer x. Summary of Exiting and Saving Commands :ZZ Exits vi and save all changes : x Exits vi and save all changes :wq Exits vi and save all changes :w Saves changes without exiting vi :q Quits vi; this command only works when no changes have been made to the file. :q! Forcefully quits vi without saving changes The Colon ( : ) Commands Commands preceded by a : (colon) are the same as those used by the ex and ed line editors. Ex and vi are really the same program. The different name determines whether the editor starts as a line editor or as a visual editor. For more information, check the UNIX manuals or type man ed, man ex, or man vi. As a powerful editor, vi incorporates many advanced editing and formatting commands (e.g. nroff commands). credits: Night
__________________
Intelligence plus character - that is the goal of true education. Light that shit, smoke that shit, pass that shit. Citeste-mi blogul!
|
![]() |
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|