New riders vi improved VIM ISBN 0735710015 pdf

572 41 0
New riders vi improved VIM ISBN 0735710015 pdf

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

The Tutorial Basic Editing Editing a Little Faster Searching Text Blocks and Multiple Files Windows Basic Visual Mode Commands for Programmers Basic Abbreviations, Keyboard Mapping, and Initialization Files Basic Command-Mode Commands 10 Basic GUI Usage 11 Dealing with Text Files 12 Automatic Completion 13 Autocommands 14 File Recovery and Command-Line Arguments 15 Miscellaneous Commands 16 Cookbook 17 Topics Not Covered Basic Editing T HE VIM EDITOR IS ONE OF THE MOST powerful text editors around It is also extremely efficient, enabling the user to edit files with a minimum of keystrokes.This power and functionality comes at a cost, however:When getting started, users face a steep learning curve This chapter teaches you the basic set of 10 Vim commands you need to get started editing In this chapter, you learn the following: n The four basic movement commands n How to insert and delete text n How to get help (very important) n Exiting the editor After you get these commands down pat, you can learn the more advanced editing commands Before You Start If you have not installed Vim, you need to read Appendix A, “Installing Vim,” and install the editor Chapter Basic Editing If you are running on UNIX, execute the following command: $ touch ~/.vimrc By creating a ~/.vimrc, you tell Vim that you want to use it in Vim mode If this file is not present, Vim runs in Vi-compatibility mode and you lose access to many of the advanced Vim features However, you can enable the advanced features from within Vim at any time with this command: :set nocompatible If you are running on Microsoft Windows, the installation process creates the Microsoft Windows version of this file, _vimrc, for you Running Vim for the First Time To start Vim, enter this command: $ gvim file.txt Note that the $ is the default UNIX command prompt.Your prompt might differ If you are running Microsoft Windows, open an MS-DOS prompt window and enter this command: C:> gvim file.txt (Again, your prompt may differ.) In either case, Vim starts editing a file called file.txt Because this is a new file, you get a blank window Figure 1.1 shows what your screen will look like The tilde (~) lines indicate lines not in the file In other words, when Vim runs out of file to display, it displays tilde lines At the bottom of a screen, a message line indicates the file is named file.txt and shows that you are creating a new file.The message information is temporary and other information overwrites it when you type the first character ~ ~ ~ ~ ~ ~ ~ ~ “file.txt” [New File] Figure 1.1 Initial Vim window Editing for the First Time The vim Command The gvim command causes the editor to create a new window for editing If you use the command vim, the editing occurs inside your command window In other words, if you are running inside an xterm, the editor uses your xterm window If you are using an MS-DOS command prompt window under Microsoft Windows, the editing occurs inside the window Figure 1.2 shows a typical MS-DOS command prompt window A very intelligent turtle Found programming UNIX a hurdle The system, you see, Ran as slow as did he, And that's not saying much for the turtle ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ "turtle.txt" 5L, 158C Figure 1.2 1,1 All Editing with the vim command in an MS-DOS window Modes The Vim editor is a modal editor.That means that the editor behaves differently, depending on which mode you are in If the bottom of the screen displays the filename or is blank, you are in normal mode If you are in insert mode, the indicator displays INSERT ; and if you are in visual mode, the indicator shows VISUAL Editing for the First Time The next few sections show you how to edit your first file During this process, you learn the basic commands that you have to know to use Vim At the end of this lesson, you will know how to edit—not fast, not efficiently, but enough to get the job done Inserting Text To enter text, you need to be in insert mode.Type i, and notice that the lower left of the screen changes to INSERT (meaning that you are in insert mode) Chapter Basic Editing Now type some text It will be inserted into the file Do not worry if you make mistakes; you can correct them later Enter the following programmer’s limerick: A very intelligent turtle Found programming UNIX a hurdle The system, you see, Ran as slow as did he, And that’s not saying much for the turtle After you have finished inserting, press the key.The INSERT indicator goes away and you return to command mode Your screen should now look something like Figure 1.3 A very intelligent turtle Found programming UNIX a hurdle The system, you see, Ran as slow as did he, And that's not saying much for the turtle ~ ~ ~ ~ Figure 1.3 Screen after the text has been inserted Getting Out of Trouble One of the problems for Vim novices is mode confusion, which is caused by forgetting which mode you are in or by accidentally typing a command that switches modes.To get back to normal mode, no matter what mode you are in, press the key Moving Around After you return to command mode, you can move around by using these keys: h (left), j (down), k (up), and l (right) At first, it may appear that these commands were chosen at random After all, who ever heard of using l for right? But actually, there is a very good reason for these choices: Moving the cursor is the most common thing you in an editor, and these keys are on the home row of your right hand In other words, these commands are placed where you can type them the fastest Note You can also move the cursor by using the arrow keys If you do, however, you greatly slow down your editing—because to press the arrow keys, you must move your hand from the text keys to the arrow keys Considering that you might be doing it hundreds of times an hour, this can take a significant amount of time If you want to edit efficiently, use h, j, k, and l Also, there are keyboards which not have arrow keys, or which locate them in unusual places; therefore, knowing the use of these keys helps in those situations Editing for the First Time One way to remember these commands is that h is on the left, l is on the right, j is a hook down, and k points up Another good way to remember the commands is to copy this information on a Post-It Note and put it on the edge of your monitor until you get used to these commands k h l j Deleting Characters To delete a character, move the cursor over it and type x (This is a throwback to the old days of the typewriter, when you deleted things by typing xxxx over them.) Move the cursor to the beginning of the first line, for example, and type xxxxxxx (eight x’s) to delete the first eight characters on the line Figure 1.4 shows the result To enter a correction, type iA young .This begins an insert (the i), inserts the words A young, and then exits insert mode (the final ) Figure 1.5 shows the results intelligent turtle Found programming UNIX a hurdle The system, you see, Ran as slow as did he, And that's not saying much for the turtle ~ ~ ~ ~ Figure 1.4 Screen after delete (xxxxxxxx) A young intelligent turtle Found programming UNIX a hurdle The system, you see, Ran as slow as did he, And that's not saying much for the turtle ~ ~ ~ ~ Figure 1.5 Result of the insert Note is a text editor By default, it does not wrap text You must end each line by pressing the key If you don’t and just keep typing when you reach the right margin, all you will is insert a very Vim long line into the editor You will not automatically go to the next line To so, you need to press the key (This is the default mode of operation You can configure the Vim editor to word wrap, however, as discussed in Chapter 11, “Dealing with Text Files.”) Chapter Basic Editing Undo and Redo Suppose you delete too much.Well, you could type it in again, but an easier way exists.The u command undoes the last edit Take a look at this in action Move the cursor to the A in the first line Now type xxxxxxx to delete A young.The result is as follows: intelligent turtle Type u to undo the last delete.That delete removed the g, so the undo restores the character g intelligent turtle The next u command restores the next-to-last character deleted: ng intelligent turtle The next u command gives you the u, and so on: ung intelligent turtle oung intelligent turtle young intelligent turtle young intelligent turtle A young intelligent turtle If you undo too many times, you can press CTRL-R (redo) to reverse the preceding command In other words, it undoes the undo To see this in action, press CTRL-R twice.The character A and the space after it disappear young intelligent turtle There’s a special version of the undo command, the U (undo line) command.The undo line command undoes all the changes made on the last line that was edited Typing this command twice cancels the preceding U Note If you are an old Vi user, note that the multilevel undo of Vim differs significantly from the single level available to a Vi user Note Throughout this book we assume that you have turned off Vi compatibility (Vi compatiblity disables many advanced features of Vim in order to be compatible with Vi.) This feature is automatically turned off for Unix users when they create the $HOME/.vimrc file For Microsoft Windows, it is turned off during installation (If compatibility is turned on the v command provides one level of undo.) Other Editing Commands A very intelligent turtle xxxx A intelligent turtle Delete very xxxxxx Delete turtle A intelligent Restore line with U A very intelligent turtle A intelligent Second U undoes the preceding U Getting Out To exit, use the ZZ command.This command writes the file and exits Unlike many other editors, Vim does not automatically make a backup file If you type ZZ, your changes are committed and there’s no turning back (You can configure the Vim editor to produce backup files, as discussed in Chapter 14,“File Recovery and Command-Line Arguments.”) Discarding Changes Sometimes you will make a set of changes and suddenly realize you were better off before you started Don’t worry; Vim has a “quit-and-throw-things-away” command It is :q! For those of you interested in the details, the three parts of this command are the colon (:), which enters command mode; the q command, which tells the editor to quit; and the override command modifier (!).The override command modifier is needed because Vim is reluctant to throw away changes Because this is a command mode command, you need to type to finish it (All command mode commands have at the end.This is not shown in the text.) If you were to just type :q, Vim would display an error message and refuse to exit: No write since last change (use ! to override) By specifying the override, you are in effect telling Vim, “I know that what I’m doing looks stupid, but I’m a big boy and really want to this.” Other Editing Commands Now that you have gone through a few simple commands, it is time to move on to some slightly more complex operations Inserting Characters at the End of a Line The i command inserts a character before the character under the cursor.That works fine; but what happens if you want to add stuff to the end of the line? For that you need to insert text after the cursor.This is done with the a (append) command 10 Chapter Basic Editing For example, to change the line and that’s not saying much for the turtle to and that’s not saying much for the turtle!!! move the cursor over to the dot at the end of the line.Then type x to delete the period.The cursor is now positioned at the end of the line on the e in turtle: and that’s not saying much for the turtle Now type a!!! to append three exclamation points after the e in turtle: and that’s not saying much for the turtle!!! Deleting a Line To delete a line, use the dd command, which deletes the line on which the cursor is positioned.To delete the middle line of this example, for instance, position the cursor anywhere on the line The system, you see, as shown in Figure 1.6 Now type dd Figure 1.7 shows the results Opening Up New Lines To add a new line, use the o command to open up a new line below the cursor.The editor is then placed in insert mode A very intelligent turtle Found programming UNIX a hurdle The system, you see, Ran as slow as did he, And that's not saying much for the turtle!!! ~ ~ "turtle.txt" 5L, 155c written Figure 1.6 Screen before dd command A very intelligent turtle Found programming UNIX a hurdle Ran as slow as did he, And that's not saying much for the turtle!!! ~ ~ ~ Figure 1.7 Screen after dd command 558 override (!) :dsearch, 314 :function, 358 :global, 312 :ijump, 314 :ilist, 314 :next, 42 :quit, :recover, 152 :unlet, 353 :vi, 41 :wqall, 242 :write, 41, 168, 202 :wviminfo, 233 perl_string_as_statement, Perl syntax vari- able, 409 perl_sync_dist, Perl syntax variable, 409 perl_want_scope_in_variables, Perl syntax variable, 409 Php3, syntax options, 410 php3_baselib, Php3 syntax variables, 410 php3_minlines, Php3 syntax variables, 410 php3_sql_query, Php3 syntax variables, 410 Phtm, syntax options, 410 P p flag, substitute, 103 phtml_minlines, Phtm syntax variable, 410 phtml_sql_query, Phtm syntax variable, 410 page down, insert mode (), 228 pointer callback fix, gui option, 328 page up, insert mode (), 228 popup, special menu, 334 pairs, match, 76-77 position, window (:winpos), 324-325 paragraph PostScript, syntax options, 410 backward (}), 121 forward ({), 121 select (ap), visual mode, 254 select (ip), visual mode, 254 postscr_andornot_binary, PostScript syntax variable, 410 postscr_display, PostScript syntax variable, 410 parenthesis block inner, select (i( ), 254 select (a( ), 254 postscr_encoding, PostScript syntax paste, toolbar, 111 postscr_fonts, PostScript syntax path only modifier (:p), 354 path option, 127, 281 checking, 282 path, relative, modifier (:.), 354 pclose (close preview window), 276 perl, 173 variable, 410 variable, 410 postscr_ghostscript, PostScript syntax variable, 410 postscr_level, PostScript syntax variable, 410 ppop (:pop in preview window), 276 Perl, syntax options, 409 prepend string, option (:set option ^=), 381 perl_extended_variable, Perl syntax preserve, 151 variable, 409 perl_no_sync_on_global, Perl syntax variable, 409 perl_no_sync_on_sub, Perl syntax variable, 409 preserve changes (:preserve), 319 preview window, 276 :pop (:ppop), 276 :tjump (:ptjump), 276 :tnext (:ptnext), 276 :tprevious (:ptprevious), 277 quote next character (CTRL-V) :tselect (:ptselect), 276 close (:pclose), 276 go to tag (:ptag), 276 tag (CTRL-Wg}), 277 tag (CTRL-W}), 277 tags, 276-277 previous buffer :bNext, :bprevious, 51 and split (:sbNext, :sbprevious), 51-52 previous error (:cNext, :cprevious), , 88, 170 previous file (CTRL-^), 44, 244 previous file after writing (:wNext, :wprevious), 43 editing (:Next, :previous), 43 ptcap_minlines, Termcap / Printcap syntax variable, 410 ptjump (:tjump in preview window), 276 ptlast (:tlast in preview window), 277 ptnext (:tnext in preview window), 276 ptNext (see :ptprevious), 277 ptprevious (:tprevious in preview window), 277 ptrewind (:trewind in preview window), 277 ptselect (:tselect in preview window), 276 previous jump, go to (CTRL-O), 189 punctuation, regular expression ([:punct:]), 216 previous tag (:tNext, :tprevious), 83 put (p), 36-37, 39, 160-161, 220, 224 previous window (CTRL-Wp), 162163, 240 put (text), 36 print, 100 print and substitution, 103 print lines (:print), 308 in list mode (:list), 308 with numbers (:number, :#), 293, 308 and move cursor (gp), 220 before and move cursor(gP), 220 before cursor (P), 37, 162-163, 220 before with reindent ([P, ]P), 265 lines (:put), 318 toolbar, 111 with reindent (]p), 265 print neighborhood (:z), 309 pyfile, 173 print working directory (:pwd), 314 python, 173 print, substitute, 103 Q print, toolbar, 110 printable character, regular expression \p, 217 [:print:], 216 qall (quit all), 242 quit (:q!), 9, 202 printable non-digit character, regular expression (\P), 217 quit (:quit), 144, 242-243 Printcap, syntax options, 410 quit after write modified (:xit), 202 printing character definition, ‘isprint’ option, 186 quit all (:qall), 242 priority, menu, 334 program, building (:make), 87, 284 prohibit modifications (vim -m), 144 ptag (go to tag in preview window), 276 quit after write (:wq), 202, 322 quit all after writing (:wqall), 242 quit and discard changes (ZQ), 202 quit with error code (:cquit), 89 quitting (ZZ), quote next character (CTRL-V), 85, 229, 268 559 560 range R range $, 100 %, 101, 160 ‘, 101 , 101 line,‘, 102, 105 line, visual mode, 102 regular expression ([]), 215 user defined commands, 361 ranges, 100 read, 104, 317 :browse, 340 file (:read), 135 from command, 317 wildcards, 396 read-only opening file (-R), 142, 144 opening file (:view), 41 recover file (:recover), 152 file (-r), 149, 152 redirection, output (:redir), 321 redo (CTRL-R), redo (:redo), 318 redo, toolbar, 110 redraw screen (CTRL-L), 156 reference, regular expression, 215 region, syntax, 415 registers, 50 appending text, 222 execute (:@), 317 insert (CTRL-R), 263-264 keyword, 361 list registers, 222 repeat execute (:@@), 317 special, 223 unnamed, 221 user defined commands, 361 visual mode, 251 yank delete, put, 221 regular expression, 32-33, 103, 168, 209 $, 104 *, 104 (\r), 217 (\t), 217 ASCII characters ([:ASCII:]), 215 [:lower], 212 [:upper], 212 [], 104 \1, 103 alphabetic characters (\a), 216 and tags, 82 any letter (\a), 211 backspace ([:backspace:]), 215 backspace character (\b), 216 begin word (\), 215 end word (\>), 209 escape character (\), 34 escape character (\e), 216 filename character (\f), 216 filename character, non-digit (\F), 216 first group (\1), 216 first match (\1), 168 first match (\1), 213 grouping ( \( \) ), 213, 216 grouping \( \), 104 head of word character (\h), 216 hexidecimal digit ([:xdigit:]), 216 repeat or more times, regular expression (\+) hexidecimal digit (\x), 217 identifier character (\i), 216 keyword character, non-digit (\k), 217 keyword characters (\k), 217 last substitute string (\~), 217 letter, lowercase (\l), 217 letter, upper case ([:upper:]), 216 letter, upper case (\u), 217 letters and digits ([:alnum:]), 215 lowercase letter (\l), 217 match any character (.), 104 match except ([^x]), 104, 168 non-alphabetic characters (\A), 216 non-digit character (\D), 216 non-head of word character (\H), 216 non-hexidecimal character (\X), 217 non-identifier character (\I), 216 non-lowercase letter (\l), 217 non-upper case character (\U), 217 non-whitespace character (\S), 217 non-word character (\W), 217 octal digit (\o), 217 or operator (\|), 214 or operator (\|), 218 printable character (\p), 217 printable characters ([:print:]), 216 printable non-digit character (\P), 217 puncuation ([:punct:]), 216 range ([]), 215 reference, 215 repeat (*), 167-168 repeat (\{m,n}), 212 repeat or times (\=), 217 repeat or more times (*), 104, 217 repeat or more times (\+), 217 repeat a given number of times ({}), 217 repeat atom (*), 210 repeat atom (\+), 210 single character (.), 215 single character match (.), 33-34, 168 spaces ([:space:]), 216 special atoms, 211 start of line (^), 32-33, 215 start of word (\), 77 repeat shell command (:!!), 319 right shift (>>), 69-70 repeat single character search backwards (,), 187 right shift {} block, 77 repeat single character search forward (;), 187 right shift, visual mode (>), 60 repeat visual selection, 252-253 repeat regular expression (*), 167-168 regular expressions (\{m,n}), 212 replace character block visual mode (r{char}), 64 r{char}, 23, 199 virtual (gr{char}), 199-200 with (r), 23 rgview command, 145 right mouse button, 109 right shift (:>), 317 right shift, block visual mode (>), 64 right to left, 174 right word, insert mode (), 228 right, move, insert mode (), 228 right, scrollbar, gui option, 328 root modifier (:r), 354 rot13 encode / decode (g?{motion}), 123 replace string, virtual (gR{string}), 200 rot13 encode / decode, visual mode (g?), 257 replace, GUI (:promptrepl), 340 rot13 encode line (g?g?, g??), 123 replace, select mode, 259 rotate down, window (CTRL-Wr), 241 replace, toolbar, 111 rotate up, window (CTRL-WR), 241 reset, boolean option (:set option), 380 rview command, 145 restricted mode (-Z), 144 rvim command, 145 retab, 267-268 rviminfo, 233 replace mode (R), 199 return, 358 S reverse and search again (N), 31-32 reverse single character search (F{char}), 17 save all, toolbar, 110 reverse single character search until (T{char}), 17 saving options, 95 save, toolbar, 110 rewind window, :trewind (:ptrewind), 277 screen down (), 192 Rexx, syntax options, 410 screen up (CTRL-B), 191 rexx_minlines, Rexx syntax variable, 410 screen mode, MS-DOS (:mode), 347 search screen changing bottom line (zb, z-), 194 changing center line (zz, z.), 194-195 changing top line (z), 193 changing top line (zt), 193-194 down 1/2 page (CTRL-D), 192 down line (CTRL-E), 192 down page (CTRL-F), 192 redraw (CTRL-L), 156 restore, option, 395 scroll up 1/2 (CTRL-D), 20, 190-191 scroll up 1/2 (CTRL-U), 20, 190-191 up 1/2 page (CTRL-U), 192 up line (CTRL-Y), 191-192 up page (CTRL-B), 192 script executing (-s), 145 toolbar, 111 scripts, 144 scroll down (CTRL-X CTRL-Y), 131 1/2 page (CTRL-D), 192 1/2 screen (CTRL-D), 20, 190-191 line (CTRL-E), 192 page (CTRL-F), 192 scroll up (CTRL-X CTRL-E), 131 1/2 page (CTRL-U), 192 1/2 screen (CTRL-U), 20, 190-191 line (CTRL-Y), 191-192 page (CTRL-B), 192 scrollbar bottom, gui option, 328 left, gui option, 328 right, gui option, 328 scrolling, horizontal, 234 search #include files (:isearch), 313 Abort, 18 again (n), 31-32, 227 again, reverse (N), 31-32 backward for { in column ([[), 279 backward for } in column ([]), 279 backwards (?), 31-32 backwards, instant word (#, £), 206 backwards, instant word, partial (g#, g£), 206 changing direction, 31-32 for global variable definition (gD), 7374 for local variable definition (gd), 73-74 for word under the cursor, start at cursor (]CTRL-I), 73-74 for word under the cursor, start at top of file ([CTRL-I), 73-74 forward (/), 27-30, 32, 161, 203, 227 forward for { in column (]]), 279 forward for } in column (][), 279 GUI (:promptfind), 340 highlight off (:nohlsearch), 29 highlighting (‘hlsearch’), 203 history, 28 in new window (CTRL-W CTRL-I), 244 incremental, 30 instant word (*), 206 instant word, partial (g*), 206 interrupting (CTRL-C, CTRLBREAK), 206 method end, backward ([M), 279 method end, forward (]M), 279 method start, backward ([m), 279 method start, forward (]m), 279 offset, 206-208 offset, begin, 207 offset, end, 207 offset, reference, 215 offset, start, 207 option, highlight, 29 options, 29 regular expressions, 209 repeat (n), 161 single character (f{char}), 17, 187 single character reverse (F{char}), 17 single character, repeat backwards (,), 187 single character, repeat forward (;), 187 string, last (/ special register), 223 string, repeat last substitute (:~), 311 tags, 289 toolbar, 111 until, single character (t{char}), 17 until, single character reverse (T{char}), 17 wrapping, 205 563 564 second line, formatting based on (2 flag, ‘formatoptions’) option ^=, 380-381 option!, 380 option&, 380 option?, 380 second line, formatting based on (2 flag, ‘formatoptions’), 117-119 sections backwards ([[), 122 sections forwards (][), 122 sed, syntax options, 411 select angle bracket block (a

Ngày đăng: 19/03/2019, 11:01

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan