📄 vi.beginner
字号:
Section 21: {l} {k} {j} {h}There are several commands to move around on the screen on a character bycharacter basis:l - moves the cursor one character to the RIGHTk - moves the cursor UP one linej - moves the cursor DOWN one lineh - moves the cursor one character to the LEFTSection 22: {i} {a} {I} {A} {o} {O} ^[ (escape key)For this and following sections you will need to use the ESCAPE key on yourterminal. It is usually marked ESC. Since the escape key is the same astyping {^[} we will use ^[ for the escape key.Probably the most often used command in an editor is the insert command. Beloware two lines of text, the first correct, the second incorrect. Position yourcursor at the beginning of Line 1 and type {z^M}.Line 1: This is an example of the insert command.Line 2: This is an of the insert command.To make line 2 look like line 1, we are going to insert the characters'example ' before the word 'of'. So, now move the cursor so that it ispositioned on the 'o' of 'of'. (You can do this by typing {^M} to moveto the beginning of line 2, followed by {6w} or {wwwwww} to position the cursoron the word 'of'.)Now carefully type the following string and observe the effects: {iexample ^[} (remember: ^[ is the escape key)}The {i} begins the insert mode, and 'example ' is inserted into the line: be sure to notice the blank in 'example '. The ^[ ends insertion mode, and the line is updated to include the new string. Line 1 should look exactly like Line 2.Move the cursor to the beginning of Line 3 below and type {z^M}:Line 3: These lines are examples for the 'a' command.Line 4: These line are examples for the 'We will change line four to look like line three by using the append command.We need to append an 's' to the word 'line'. Position the cursor on the 'e'of 'line'. You can do this in several ways, one way is the following:First, type {/line /^M}. This puts us on the word 'line' in Line 4(the blank in the search string is important!). Next, type {e}. The 'e' putsus at the end of the word. Now, type {as^[ (^[ is the escape character)}. The 'a' puts us in insert mode, AFTER the current character. We appended the 's', and the escape ^[ ended the insert mode.The difference between {i} (insert) and {a} (append) is that {i} beginsinserting text BEFORE the cursor, and {a} begins inserting AFTER the cursor.Now type {Aa' command.^[}. The cursor is moved to the end of the line and thestring following {A} is inserted into the text. Line 4 should now look likeline 3.Just as {A} moves you to the end of the line to begin inserting, {I} wouldbegin inserting at the FRONT of the line.To begin the insertion of a line after the cursor line, type {o}. To insert aline before the cursor line, type {O}. In other words {o123^[} is equivalentto {A^M123^[}, and {O123^[} is equivalent to {I123^M^[}. The text after the{o} or {O} is ended with an escape ^[.This paragraph contains information that is terminal dependent: you will justhave to experiment to discover what your terminal does. Once in the insertmode, if you make a mistake in the typing, ^H will delete the previouscharacter up to the beginning of the current insertion. ^W will delete theprevious word, and one of ^U, @, or ^X will delete the current line (up to thebeginning of the current insertion). You will need to experiment with ^U, @,and ^X to determine which works for your terminal.Section 23: {f} {x} {X} {w} {l} {r} {R} {s} {S} {J}Position the cursor at the beginning of line 5 and {z^M}:Line 5: The line as it should be.Line 6: The line as it shouldn't be.To make Line 6 like Line 5, we have to delete the 'n', the apostrophe, and the't'. There are several ways to position ourselves at the 'n'. Choosewhichever one suits your fancy:{/n't/^M}{^M7w6l} or {^M7w6 } (note the space){^M3fn} (finds the 3rd 'n' on the line)Now {xxx} will delete the three characters, as will {3x}.Note that {X} deletes the character just BEFORE the cursor, as opposedto the character AT the cursor.Position the cursor at line 7 and {z^M}:Line 7: The line as it would be.Line 8: The line as it could be.To change line 8 into line 7 we need to change the 'c' in 'could' into a 'w'.The 'r' (replace) command was designed for this. Typing {rc} is the same astyping {xic^[} (i.e. delete the 'bad' character and insert the correctnew character). Therefore, assuming that you have positioned the cursor on the'c' of 'could', the easiest way to change 'could' into 'would' is {rw}.If you would like to now change the 'would' into 'should', use the substitutecommand, 's': {ssh^[}. The difference between 'r' and 's' is that 'r'(replace) replaces the current character with another character, while 's'(substitute) substitutes the current character with a string, ended with anescape.The capital letter version of replace {R} replaces each character by acharacter one at a time until you type an escape, ^[. The 'S' commandsubstitutes the whole line.Position your cursor at the beginning of line 9 and {z^M}.Line 9: Love is a many splendored thing.Line 10: Love is a most splendored thing.To change line 10 into line 9, position the cursor at the beginning of 'most',and type {Rmany^[}.You may have noticed that, when inserting text, a new line is formed by typing{^M}. When changing, replacing, or substituting text you can make a new lineby typing {^M}. However, neither {x} nor {X} will remove ^M to make two lines into one line. To do this, position the cursor on the first of the two lines you wish to make into a single line and type {J} (uppercase J for 'Join').Section 24: {u} {U}Finally, before we review, let's look at the undo command. Positionyour cursor on line 11 below and {z^M}.Line 11: The quick brown fox jumped over the lazy hound dog.Line 12: the qwick black dog dumped over the laxy poune fox.Type the following set of commands, and observe carefully the effect of each of the commands:{/^Line 12:/^M} {ft} {rT} {fw} {ru} {w} {Rbrown fox^[} {w} {rj} {fx} {rz} {w} {Rhound dog^[}Line 12 now matches line 11. Now type {U} - capital 'U'. And line 12 nowlooks like it did before you typed in the command strings. Now type:{ft} {rT} {fw} {ru} {^M} {^M}and then type {u}: the cursor jumps back to the line containing the secondchange you made and 'undoes' it. That is, {U} 'undoes' all the changes on theline, and {u} 'undoes' only the last change. Type {u} several times andobserve what happens: {u} can undo a previous {u}!Caveat: {U} only works as long as the cursor is still on the line. Move thecursor off the line and {U} will have no effect, except to possibly beep atyou. However, {u} will undo the last change, no matter where it occurred.Section 25: reviewAt this point, you have all the commands you need in order to make use of vi.The remainder of this tutorial will discuss variations on these commands aswell as introduce new commands that make the job of editing more efficient.Here is a brief review of the basic commands we have covered. They are listedin the order of increasing complexity and/or decreasing necessity (to say thata command is less necessary is not to say that it is less useful!). Thesecommands allow you to comfortably edit any text file. There are othercommands that will make life easier but will require extra time to learn,obviously. You may want to consider setting this tutorial aside for severalweeks and returning to it later after gaining experience with vi and gettingcomfortable with it. The convenience of some of the more exotic commands maythen be apparent and worth the extra investment of time and effortrequired to master them.to get into the editor from Unix: {vi filename}to exit the editor saving all changes {ZZ} or {:wq^M} throwing away all changes {:q!^M} when no changes have been made {:q^M}save a file without exiting the editor {:w^M}write the file into another file {:w filename^M}insert text before the cursor {i ...text... ^[} at the beginning of the line {I ...text... ^[} after the cursor (append) {a ...text... ^[} at the end of the line {A ...text... ^[} after the current line {o ...text... ^[} before the current line {O ...text... ^[}delete the character ... under the cursor {x} to the left of the cursor {X}delete n characters {nx} or {nX} (for n a number)make two lines into one line (Join) {J}find a string in the file ... searching forward {/ ...string... /^M} searching backwards {? ...string... ?^M}repeat the last search command {n}repeat the last search command in the opposite direction {N}find the character c on this line ... searching forward {fc} searching backward {Fc}repeat the last 'find character' command {;}replace a character with character x {rx}substitute a single character with text {s ...text... ^[}substitute n characters with text {ns ...text... ^[}replace characters one-by-one with text {R ...text... ^[}undo all changes to the current line {U}undo the last single change {u}move forward in the file a "screenful" {^F}move back in the file a "screenful" {^B}move forward in the file one line {^M} or {+}move backward in the file one line {-}move to the beginning of the line {0}move to the end of the line {$}move forward one word {w}move forward one word, ignoring punctuation {W}move forward to the end of the next word {e}to the end of the word, ignoring punctuation{E}move backward one word {b}move back one word, ignoring punctuation {B}return to the last line modified {''}scroll a line onto the top of the screen {^Y}scroll a line onto the bottom of the screen {^E}move "up" in the file a half-screen {^U}move "down" in the file a half-screen {^D}move the cursor to the top screen line {H}move the cursor to the bottom screen line {L}move the cursor to the middle line {M}move LEFT one character position {h} or {^H}move RIGHT one character position {l} or { }move UP in the same column {k} or {^P}move DOWN in the same column {j} or {^N}mark the current position, name it x {mx}move to the line marked/named x {'x}move to the character position named x {`x}move to the beginning of the file {1G}move to the end of the file {G}move to line 23 in the file {23G}repaint the screen with the cursor line at the top of the screen {z^M} in the middle of the screen {z.} at the bottom of the screen {z-}More information on vi can be found in the file vi.advanced, which you canperuse at your leisure. From UNIX, type {vi.tut advanced^M}.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -