Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 46 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
46
Dung lượng
487,08 KB
Nội dung
82935bapp01.qxd:LinuxToolbox 10/29/07 1:19 PM Page 289 Using vi or Vim Editors Although easy-to-use graphical text editors (such as gedit and kedit) are readily available with Linux, most power users still use vi or Emacs to edit text files Besides the fact that vi and Emacs will work from any shell (no GUI required), they offer other advantages such as your hands never having to leave the keyboard and integration with useful utilities And unlike GUI editors, text-based editors are usable over slow Internet connections such as dial-up or satellite IN THIS APPENDIX Using the vi editor Starting/quitting the vi editor Moving around in vi Changing and deleting text Using Ex commands This appendix focuses on features of the vi editor that can not only help you with basic editing, but Using visual mode also help you some advanced text manipulation We chose to cover vi rather than Emacs because vi is more universal and leaner, and also because vi keyboard shortcuts only require two arms Because many Linux systems use the Vim (Vi IMproved) editor in place of the older vi editor, the descriptions in this appendix are extended to cover Vim as well Some features in Vim that are not in vi include multiple undo levels, syntax highlighting, and online help NOTE If you have never used vi or Vim before, try out the tutor that comes with the vim-enhanced package Run the vimtutor command and follow the instructions to step through many of the key features of vi and Vim Starting and Quitting the vi Editor If you want to experiment with using vi, you should copy a text file to practice on For example, type: $ cp /etc/passwd /tmp Then open that file using the vi command as follows: $ vi /tmp/passwd 82935bapp01.qxd:LinuxToolbox 10/29/07 1:19 PM Page 290 Appendix A: Using vi or Vim Editors To benefit from all the improvements of Vim, make sure you have the vim-enhanced package installed (which gets installed by default on Ubuntu) On many systems, vi is aliased to the vim command On Ubuntu, both commands launch vim You may want to double-check that using the alias command /bin/vi /tmp/text.txt Here are a few other ways you can start vi: $ $ $ $ $ vi +25 /tmp/inittab vi + /tmp/inittab vi +/tty /tmp/inittab vi -r /tmp/inittab view /tmp/inittab Begin on line 25 Begin editing file on the last line Begin on first line with word “tty” Recover file from crashed edit session Edit file in read-only mode When you are done with your vi session, there are several different ways to save and quit To save the file before you are ready to quit, type :w To quit and save changes, type either zz or :wq To quit without saving changes, type :q! If you find that you can’t write to the file you are editing, it may be opened in read-only mode If that’s the case, you can try forcing a write by typing :w! or you can save the contents of the file to a different name For example, type the following to save the contents of the current file to a file named myfile.txt: :w /tmp/myfile.txt The vi editor also enables you to line up several files at a time to edit For example, type: $ cd /tmp $ touch a.txt b.txt c.txt $ vi a.txt b.txt c.txt In this example, vi will open the a.txt file first You can move to the next file by typing :n You may want to save changes before moving to the next file (:w) or save changes as you move to the next file (:wn) To abandon changes while moving to the next file, type :n! You will probably find it easier to open multiple files by splitting your vi screen When you’re in vi and have a file open, you can split your screen multiple times either horizontally or vertically: :split /etc/motd.tail :vsplit /etc/motd.tail Use to complete the path to the files, just like you would in a bash shell To navigate between split windows, press Ctrl+w, followed by the w key To close the current window, use the usual vi exit command (:q) 290 82935bapp01.qxd:LinuxToolbox 10/29/07 1:19 PM Page 291 Appendix A: Using vi or Vim Editors Moving Around in vi The first thing to get used to with vi is that you can’t just start typing Vi has multiple modes that enable you to perform a different set of tasks You start a vi session in Normal mode, where vi is waiting for you to type a command to get started While you are in Normal mode, you can move around the file, to position where you want to be in the file To enter or modify text, you need to go into Insert or Replace modes Assuming vi is open with a file that contains several pages of text, Table A-1 shows some keys and combinations you can type to move around the file while in normal mode Table A-1: Keystroke Commands for Moving Around Key Result Key Result PageDown or Ctrl+f Move down one page PageUp or Ctrl+b Move up one page Ctrl+d Move down half page Ctrl+u Move up half page Shift+g Go to last line of file :1 Go to first line of file (use any number to go to that line) Shift+h Move cursor to screen top Shift+l Move cursor to screen bottom Shift+m Move cursor to middle of screen Ctrl+l Redraw screen (if garbled) Enter Move cursor to beginning of the next line - Move cursor to beginning of the previous line Home or $ Move cursor to end of line End or ^ or Move cursor to line beginning ( Move cursor to beginning of previous sentence ) Move cursor to beginning of next sentence { Move cursor to beginning of previous paragraph } Move cursor to beginning of next paragraph w Move cursor to next word (space, new line, or punctuation) Shift+w Move cursor to next word (space or new line) Continued 291 82935bapp01.qxd:LinuxToolbox 10/29/07 1:19 PM Page 292 Appendix A: Using vi or Vim Editors Table A-1: Keystroke Commands for Moving Around (continued) Key Result Key Result b Move cursor to previous word (space, new line, or punctuation) Shift+b Move cursor to previous word (space or new line) e Move cursor to end of next word (space, new line, or punctuation) Shift+e Move cursor to end of next word (space or new line) Left arrow or Backspace Move cursor left one letter Right arrow or l Move cursor right one letter k or up arrow Move cursor up one line j or down arrow Move cursor down one line /string Find next occurrence of string ?string Find previous occurrence of string n Find same string again (forward) Shift+n Find same string again (backwards) Changing and Deleting Text in vi To begin changing or adding to text with vi, you can enter Insert or Replace modes, as shown in Table A-2 When you enter Insert or Replace mode, the characters you type will appear in the text document (as opposed to being interpreted as commands) Press the Esc key to exit to normal mode after you are done inserting or replacing text Table A-2: Commands for Changing Text 292 Key Result Key Result i Typed text appears before current character Shift+i Typed text appears at the beginning of current line a Typed text appears after current character Shift+a Typed text appears at the end of current line 82935bapp01.qxd:LinuxToolbox 10/29/07 1:20 PM Page 293 Appendix A: Using vi or Vim Editors Table A-2: Commands for Changing Text (continued) Key Result Key Result o Open a new line below current line to begin typing Shift+o Open a new line above current line to begin typing s Erase current character and replace with new text Shift+s Erase current line and enter new text c? Replace ? with l, w, $, or c to change the current letter, word, end of line, or line Shift+c Erase from cursor to end of line and enter new text r Replace current character with the next one you type Shift+r Overwrite as you type from current character going forward Table A-3 contains keys you type to delete or paste text Table A-3: Commands for Deleting and Pasting Text Key Result Key Result x Delete text under cursor Shift+x Delete text to left of cursor d? Replace ? with l, w, $, or d to cut the current letter, word, or end of line from cursor or entire line Shift+d Cut from cursor to end of line y? Replace ? with l, w, or $ to copy (yank) the current letter, word, or end of line from cursor Shift+y Yank current line p Pastes cut or yanked text after cursor Shift+p Pastes cut or yanked text before cursor Using Miscellaneous Commands Table A-4 shows a few miscellaneous, but important, commands you should know 293 82935bapp01.qxd:LinuxToolbox 10/29/07 1:20 PM Page 294 Appendix A: Using vi or Vim Editors Table A-4: Miscellaneous Commands Key Result u Type u to undo the previous change Multiple u commands will step back to undo multiple changes Typing a period (.) will repeat the previous command So, if you deleted a line, replaced a word, changed four letters, and so on, the same command will be done wherever the cursor is currently located (Entering input mode again resets it.) Shift+j Join the current line with the next line Esc If you didn’t catch this earlier, the Esc key returns you from an input mode back to command mode This is one of the keys you will use most often Modifying Commands with Numbers Nearly every command described so far can be modified with a number In other words, instead of deleting a word, replacing a letter, or changing a line, you can delete six words, replace 12 letters, and change nine lines Table A-5 shows some examples Table A-5: Modifying Commands with Numbers Command Result 7cw Erase the next seven words and replace them with text you type 5, Shift+d Cut the next five lines (including the current line) 3p Paste the previously deleted text three times after the current cursor 9db Cut the nine words before the current cursor 10j Move the cursor down ten lines y2) Copy (yank) text from cursor to end of next two sentences 5, Ctrl+f Move forward five pages 6, Shift+j Join the next six lines From these examples, you can see that most vi keystrokes for changing text, deleting text, or moving around in the file can be modified using numbers 294 82935bapp01.qxd:LinuxToolbox 10/29/07 1:20 PM Page 295 Appendix A: Using vi or Vim Editors Using Ex Commands The vi editor was originally built on an editor called Ex Some of the vi commands you’ve seen so far start with a semicolon and are known as Ex commands To enter Ex commands, start from normal mode and type a colon (:) This switches you to command line mode In this mode, you can use the Tab key to complete your command or file name, and the arrow keys to navigate your command history, as you would in a bash shell When you press Enter at the end of your command, you are returned to normal mode Table A-6 shows some examples of Ex commands Table A-6: Ex Command Examples Command Result :!bash Escape to a bash shell When you are done, type exit to return to vi :!date Run date (or any command you choose) Press Enter to return :!! Rerun the command previously run :20 Go to line 20 in the file :5,10w abc.txt Write lines through 10 to the file abc.txt :e abc.txt Leave the current file and begin editing the file abc.txt :.r def.txt Read the contents of def.txt into the file below the current line :s/RH/RedHat Substitute Red Hat for the first occurrence of RH on the current line :s/RH/Red Hat/g Substitute Red Hat for all occurrences of RH on the current line :%s/RH/Red Hat/g Substitute Red Hat for the all occurrences of RH in the entire file :g/Red Hat/p List every line in the file that contains the string “Red Hat” :g/gaim/s//pidgin/gp Find every instance of gaim and change it to pidgin From the ex prompt you can also see and change settings related to your vi session using the set command Table A-7 shows some examples 295 82935bapp01.qxd:LinuxToolbox 10/29/07 1:20 PM Page 296 Appendix A: Using vi or Vim Editors Table A-7: set Commands in ex Mode Command Result :set all List all settings :set List only those settings that have changed from the default :set number Have line numbers appear left of each line (Use set nonu to unset.) :set Sets autoindent, so opening a new line follows the previous indent :set ic Sets ignore case, so text searches will match regardless of case :set list Show $ for end of lines and ^I for tabs :set wm Causes vi to add line breaks between words near the end of a line Working in Visual Mode The Vim editor provides a more intuitive means of selecting text called visual mode To begin visual mode, move the cursor to the first character of the text you want to select and press the v key You will see that you are in visual mode because the following text appears at the bottom of the screen: VISUAL At this point, you can use any of your cursor movement keys (arrow keys, Page Down, End, and so on) to move the cursor to the end of the text you want to select As the page and cursor move, you will see text being highlighted When all the text you want to select is highlighted, you can press keys to act on that text For example, d deletes the text, c lets you change the selected text, :w /tmp/test.txt saves selected text to a file, and so on 296 82935bapp02.qxd:LinuxToolbox 10/29/07 1:37 PM Page 297 Shell Special Characters and Variables Ubuntu provides bash as the default shell Chapter helps you become comfortable working in the shell This appendix provides a reference of the numerous characters and variables that have special meaning to the bash shell Many of those elements are referenced in Table B-1 (Shell Special Characters) and Table B-2 (Shell Variables) IN THIS APPENDIX Using special shell characters Using shell variables Using Special Shell Characters You can use special characters from the shell to match multiple files, save some keystrokes, or perform special operations Table B-1 shows some shell special characters you may find useful Table B-1: Shell Special Characters Character Description * Match any string of characters ? Match any one character [ ] Match any character enclosed in the braces ‘ … ‘ Remove special meaning of characters between quotes Variables are not expanded “ … “ Same as simple quotes except for the escape characters ($ ` and \) that preserve their special meaning \ Escape character to remove the special meaning of the character that follows Continued 82935bapp02.qxd:LinuxToolbox 10/29/07 1:20 PM Page 298 Appendix B: Shell Special Characters and Variables Table B-1: Shell Special Characters (continued) Character Description ~ Refers to the $HOME directory ~+ Value of the shell variable PWD (working directory) ~- Refers to the previous working directory Refers to the current working directory Refers to the directory above the current directory Can be used repeatedly to reference several directories up $param Used to expand a shell variable parameter cmd1 `cmd2` cmd2 is executed first Then the call to cmd2 is substituted with the output of cmd2, and cmd1 is executed or cmd1 $(cmd2) cmd1 > Redirects standard output from command cmd1 < Redirects standard input to command cmd1 >> Appends standard output to file from command, without erasing its current contents cmd1 | cmd2 Pipes the output of one command to the input of the next cmd & Runs the command in the background cmd1 && cmd2 Runs first command, then if it returns a zero exit status, runs the second command cmd1 || cmd2 Runs first command, then if it returns a non-zero exit status, runs the second command cmd1 ; cmd2 Runs the first command and when it completes, runs the second command Using Shell Variables You identify a string of characters as a parameter (variable) by placing a $ in front of it (as in $HOME) Shell environment variables can hold information that is used by the shell itself, as well as by commands you run from the shell Not all environment variables will be populated by default Some of these variables you can change (such as the default printer in $PRINTER or your command prompt in $PS1) Others are managed by the shell (such as $OLDPWD) Table B-2 contains a list of many useful shell variables 298 ... A: Using vi or Vim Editors Table A-1: Keystroke Commands for Moving Around (continued) Key Result Key Result b Move cursor to previous word (space, new line, or punctuation) Shift+b Move cursor... 291 Appendix A: Using vi or Vim Editors Moving Around in vi The first thing to get used to with vi is that you can’t just start typing Vi has multiple modes that enable you to perform a different... A: Using vi or Vim Editors To benefit from all the improvements of Vim, make sure you have the vim- enhanced package installed (which gets installed by default on Ubuntu) On many systems, vi is