1 / 16

COS 231 Introduction to UNIX

COS 231 Introduction to UNIX. UNIX - Lecture 8 The vi Editor Chapter 8. The vi Text Editor. UNIX has a number of editors that can process the contents of text files, whether those files contain data, source code, or sentences. There are

vinson
Download Presentation

COS 231 Introduction to UNIX

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. COS 231 Introduction to UNIX UNIX - Lecture 8 The vi Editor Chapter 8 ____________________________________________________________________________________ COS 231 Introduction to UNIX, V. Karagiozov, AUBG [1]

  2. The vi Text Editor • UNIX has a number of editors that can process the contents of text files, whether those files contain data, source code, or sentences. There are • line editors, such as ed and ex, which display a line of the file on the screen; and there are • screen editors, such as vi and emacs, which display a part of the file on your terminal screen. • vi (like any text editor) is not a "what you see is what you get" word processor (UNIX supports the nroff/troff formatting package) ____________________________________________________________________________________ COS 231 Introduction to UNIX, V. Karagiozov, AUBG [2]

  3. Components of editing • First, you want to insert text (a forgotten word or a missing sentence), and you want to delete text (a stray character or an entire paragraph). • You also need to change letters and words (to correct misspellings or to reflect a change of mind about a term). • You might want to move text from one place to another part of your file. And, on occasion, • you want to copy text to duplicate it in another part of your file. ____________________________________________________________________________________ COS 231 Introduction to UNIX, V. Karagiozov, AUBG [3]

  4. Vi IMproved, a programmers text editor • Vim is a text editor that is upwards compatible to Vi. It can be used to edit any ASCII text. It is especially useful for editing programs. • There are a lot of enhancements above Vi: • multi level undo, • multi windows and buffers, • syntax highlighting, • command line editing, • filename completion, • on-line help, • visual selection, • etc. ____________________________________________________________________________________ COS 231 Introduction to UNIX, V. Karagiozov, AUBG [4]

  5. vi - modes • Command mode – default (esc), insert mode (i) One or two characters are used for the basic commands. For example: i insert cw change word • In general, vi commands: • Are case-sensitive (uppercase and lowercase keystrokes mean different things; I is different from i). • Are not shown (or "echoed") on the screen when you type them. • Do not require a [RETURN] after the command. ____________________________________________________________________________________ COS 231 Introduction to UNIX, V. Karagiozov, AUBG [5]

  6. Opening and Closing Files • Opening files $ vi [filename] $ vi practice • Saving and Quitting a File Esc ZZ Esc :wq (Esc ZZ) Esc :q • Quitting without saving Esc :q! ____________________________________________________________________________________ COS 231 Introduction to UNIX, V. Karagiozov, AUBG [6]

  7. Simple Editing • Moving the Cursor • h -- left, one space. • j -- down, one line. • k -- up, one line. • l -- right, one space. • You can also use the cursor arrow keys or the [RETURN] and [BACKSPACE] keys, but they are out of the way, and the arrow keys are not supported by all terminals. • Two useful commands that involve movement within a line are: 0 --- Move to beginning of line. $ --- Move to end of line. ____________________________________________________________________________________ COS 231 Introduction to UNIX, V. Karagiozov, AUBG [7]

  8. Movement by Text Blocks • You can also move the cursor by blocks of text: words, sentences, paragraphs, etc. • The w command moves the cursor forward one word at a time, counting symbols and punctuation as equivalent to words. The line below shows cursor movement by w: cursor,delete lines,insert characters, • You can also move by word, not counting symbols and punctuation, using the W command. (You can think of this as a "large" or "capital" Word.) Cursor movement using W looks like this: cursor, delete lines, insert characters, ____________________________________________________________________________________ COS 231 Introduction to UNIX, V. Karagiozov, AUBG [8]

  9. vim Tutor start with command: $ vimtutor • Lesson 1 – MOVING THE CURSOR, ENTERING AND EXITING VIM, TEXT EDITING – DELETION, TEXT EDITING – INSERTION • Lesson 2 - DELETION COMMANDS, MORE DELETION COMMANDS, ON COMMANDS AND OBJECTS, AN EXCEPTION TO 'COMMAND-OBJECT‘, THE UNDO COMMAND • Lesson 3 - THE PUT COMMAND, THE REPLACE COMMAND, THE CHANGE COMMAND • Lesson 4 - LOCATION AND FILE STATUS, THE SEARCH COMMAND, MATCHING PARENTHESES SEARCH, A WAY TO CHANGE ERRORS • Lesson 5 - HOW TO EXECUTE AN EXTERNAL COMMAND, MORE ON WRITING FILES, A SELECTIVE WRITE COMMAND, RETRIEVING AND MERGING FILES • Lesson 6 - THE OPEN COMMAND, THE APPEND COMMAND, ANOTHER VERSION OF REPLACE, SET OPTION • Lesson 7 - ON-LINE HELP COMMANDS ____________________________________________________________________________________ COS 231 Introduction to UNIX, V. Karagiozov, AUBG [9]

  10. LESSON 1 SUMMARY 1. The cursor is moved using either the arrow keys or the hjkl keys. h (left) j (down) k (up) l (right) 2. To enter Vim (from the $ prompt) type: vim FILENAME <RETURN> 3. To exit Vim type: <ESC> :q! <RETURN> to trash all changes. OR type: <ESC> :wq <RETURN> to save the changes. 4. To delete a character under the cursor in Normal mode type: x 5. To insert text at the cursor while in Normal mode type: i type in text <ESC> NOTE: Pressing <ESC> will place you in Normal mode or will cancel an unwanted and partially completed command. ____________________________________________________________________________________ COS 231 Introduction to UNIX, V. Karagiozov, AUBG [10]

  11. LESSON 2 SUMMARY 1. To delete from the cursor to the end of a word type: dw 2. To delete from the cursor to the end of a line type: d$ 3. To delete a whole line type: dd 4. The format for a command in Normal mode is: [number] command object OR command [number] object where: number - is how many times to repeat the command command - is what to do, such as d for delete object - is what the command should act upon, such as w (word), $ (to the end of line), etc. 5. To undo previous actions, type: u (lowercase u) To undo all the changes on a line type: U (capital U) To undo the undo's type: CTRL-R ____________________________________________________________________________________ COS 231 Introduction to UNIX, V. Karagiozov, AUBG [11]

  12. LESSON 3 SUMMARY 1. To replace text that has already been deleted, type p . This puts the deleted text AFTER the cursor (if a line was deleted it will go on the line below the cursor). 2. To replace the character under the cursor, type r and then the character which will replace the original. 3. The change command allows you to change the specified object from the cursor to the end of the object. eg. Type cw to change from the cursor to the end of the word, c$ to change to the end of a line. 4. The format for change is: [number] c object OR c [number] object ____________________________________________________________________________________ COS 231 Introduction to UNIX, V. Karagiozov, AUBG [12]

  13. LESSON 4 SUMMARY • Ctrl-g displays your location in the file and the file status. • Shift-G moves to the end of the file. A line number followed by Shift-G moves to that line number. • Typing / followed by a phrase searches FORWARD for the phrase. • Typing ? followed by a phrase searches BACKWARD for the phrase. • After a search type n to find the next occurrence in the same direction or Shift-N to search in the opposite direction. • Typing % while the cursor is on a (, ), [, ], {, or } locates its matching pair. • To substitute new for the first old on a line type :s/old/new • To substitute new for all 'old's on a line type :s/old/new/g • To substitute phrases between two line #'s type :1,5s/old/new/g • To substitute all occurrences in the file type :%s/old/new/g • To ask for confirmation each time add 'c' :%s/old/new/gc ____________________________________________________________________________________ COS 231 Introduction to UNIX, V. Karagiozov, AUBG [13]

  14. LESSON 5 SUMMARY • :!command executes an external command. Some useful examples are: :!dir - shows a directory listing. :!delete FILENAME - removes file FILENAME. • :w FILENAME writes the current Vim file to disk with name FILENAME. • :#,# FILENAME saves the lines # through # in file FILENAME. • :r FILENAME retrieves disk file FILENAME and inserts it into the current file following the cursor position. ____________________________________________________________________________________ COS 231 Introduction to UNIX, V. Karagiozov, AUBG [14]

  15. LESSON 6 SUMMARY 1. Typing o opens a line BELOW the cursor and places the cursor on the open line in Insert mode. Typing a capital O opens the line ABOVE the line the cursor is on. 2. Type an a to insert text AFTER the character the cursor is on. Typing a capital A automatically appends text to the end of the line. 3. Typing a capital R enters Replace mode until <ESC> is pressed to exit. 4. Typing ":set xxx" sets the option "xxx“ example – “set nu” set numbering lines option ____________________________________________________________________________________ COS 231 Introduction to UNIX, V. Karagiozov, AUBG [15]

  16. LESSON 7 : ON-LINE HELP COMMANDS ** Use the on-line help system ** Vim has a comprehensive on-line help system. To get started, try one of these three: • press the <HELP> key (if you have one) • press the <F1> key (if you have one) • type :help <RETURN> Type :q <RETURN> to close the help window. You can find help on just about any subject, by giving an argument to the ":help" command. Try these: • :help w <RETURN> • :help c_<T <RETURN> • :help insert-index <RETURN> ____________________________________________________________________________________ COS 231 Introduction to UNIX, V. Karagiozov, AUBG [16]

More Related