Sie sind auf Seite 1von 8

06/04/2017 Vi For Smarties - Lesson One

Vi For Smarties
Lesson One

Basic Concepts
1. Start by typing vi followed by the file(s) you want to edit.
Example: vi one.txt two.txt etc.txt
2. There are two modes: command mode and insert mode. You will begin in command mode. (Unless you have a config file which changes this. Rare.)
3. Hitting the Esc key always puts you in command mode.
It never takes you out of command mode.
4. Commands only work in command mode. Get used to hitting Esc a lot.
5. Esc is your friend. If you begin to issue a command and then change your mind, always hit Esc a few times to cancel.
6. Remember: ALL COMMANDS ARE CASE-SENSITIVE!!!

Basic Editing Commands


1. Hitting i or a from command mode puts you in insert mode. i starts inserting text before the cursor. a starts appending text after the cursor.
2. Use h j k l to move left, down, up, and right, respectively.
3. Use w e b to move forward one word, forward to the next end of a word, and back one word, respectively.
4. Use x to delete one character, dw to delete one word, or dd to delete one whole line.
5. Use a number before a command to repeat the command that many times.
Example: 3dw deletes three words starting from the current cursor position.
6. u (lowercase) means undo the last change.
7. ZZ (uppercase) saves any changes and quits.
8. :q! abandons any changes and quits.

Until you have mastered everything on this page, you are not ready for Lesson Two.

Copyright 2001 jerry_y_wang@yahoo.com. All rights reserved.

http://jerrywang.net/vi/ 1/1
06/04/2017 Vi For Smarties - Lesson Two

Vi For Smarties
Lesson Two

Cut and Paste


1. yy yanks the current line into the buffer. This is like copying the current line to the clipboard.
Example: 3yy yanks three lines into the buffer starting with the current one.
2. p (lowercase) pastes the buffer contents to the line under the current one.
3. P (uppercase) Pastes the buffer contents to the line above the current one.
4. Lines deleted with dd also end up in the buffer and can be pasted.
5. Words deleted with dw also end up in the buffer and can be pasted.

Advanced Cut and Paste


1. There are 26 lettered buffers, labeled from a to z (lowercase), in addition to the usual "clipboard" one.
2. Lettered buffers are accessed with the " double quote.
Example: "a3yy yanks three lines into the a buffer starting with the current one.
Example: "bp pastes the contents of buffer b to the line under the current one.

Going to a Line or Column


1. Type a number followed by G (uppercase) to go to that line number.
Example: 24G goes to line 24.
Example: G goes to the last line of the buffer.
2. Type a number followed by | (pipe) to go to that column in the current line.
Example: 40| goes to column 40.

Repeat the Last Action


1. Use . (period) to repeat the last action.
Example: If you hit i to insert, then type "hello world" followed by the Esc key to enter command mode, then move to another line and hit . to
repeat, the words "hello world" will appear before the current cursor position.

Search for a String


1. Use / (foward slash) followed by a string to search for that string.
Example: /foobar would find the first occurrence of foobar after the cursor position. It would also find, for example, something like
foobariciousness.
2. After the first occurrence of the string is found, use n (lowercase) to find the next occurrence. Continue hitting n as needed.
3. Use N (uppercase) to find the previous occurrence of the string.
4. Use ? (question mark) followed by a string to search backwards through the file for that string.

Until you have mastered everything on this page, you are not ready for Lesson Three.

Copyright 2001 jerry_y_wang@yahoo.com. All rights reserved.

http://jerrywang.net/vi/vitutor2.html 1/1
06/04/2017 Vi For Smarties - Lesson Three

Vi For Smarties
Lesson Three

Colon Commands (Ed/Ex Commands)


1. Use : (colon) in command mode before entering complex commands.
Example: :%s/foo/bar/g replaces all occurrences of foo with bar in the entire file.
Example: :set nu displays line numbers.
Example: :set nonu cancels line number display.
Example: :1,8d deletes lines 1 through 8 inclusive.

Useful Colon Commands


1. Use :set wm=8 to make text wrap around to a new line if it goes beyond column 72 of the current line. (80-8=72) The 8 can be some other value if
you wish, of course.
2. Use :set ts=4 to set the number of spaces used to display the tab character to 4. The 4 can be some other value if you wish.
3. Use :set ic to make string searches with / ignore cases.
Example: After :set ic using /foobar will find foobar as well as FooBaR.

Marking Text Regions


1. Use ma to mark the current line as a. The a can be any letter from a to z. The line can then be referenced by using 'a (single quote followed by a or
appropriate letter).
Example: 'a goes to the line marked as a.
2. Marked lines can be used in colon commands.
Example: :'a,'b d deletes all lines from the line marked as a to the line marked as b, inclusive.

Other Useful Colon Commands


1. Use :w newfile.txt to save to a new file called newfile.txt.
2. Use :set all to find out the value of all options set.
3. Use :8,16 co 32 to copy lines 8 through 16 to the spot after line 32.
4. Use :8,16 m 32 to move lines 8 through 16 to the spot after line 32.
5. If you have multiple files open for editing, use :n to go the next file in the sequence. If you are at the end of the sequence, use :rew to rewind to the
beginning and start again with the first file in the sequence.
6. Use :args to list all the open files in order.

Until you have mastered everything on this page, you are not ready for Lesson Four.

Copyright 2001 jerry_y_wang@yahoo.com. All rights reserved.

http://jerrywang.net/vi/vitutor3.html 1/1
06/04/2017 Vi For Smarties - Lesson Four

Vi For Smarties
Lesson Four

Useful Tricks
1. G by itself goes to the last line of the file.
2. ~ (tilde) toggles the case of the character under the cursor.
3. When deleting blocks of text, move to the first line of the block, type ma to mark it, then move to the last line of the block and type d'a to delete the
block.
4. Use the above trick with y'a to yank blocks of text.

Useful Symbols
1. Outside a colon command, $ stands for the end of the current line.
Inside a colon command, $ stands for the last line in the file.
Example: $ goes to the end of the current line.
Example: d$ deletes from the cursor to the end of the current line.
Example: :$ goes to the last line in the file.
Example: :24,$ d deletes everything from line 24 through the last line in the file, inclusive.
2. Outside a colon command, 0 (zero) stands for the beginning of the current line.
Inside a colon command, 0 (zero) stands for line 0, or the line that is going to be created above the first line in the file.
Example: 0 goes to the beginning of the current line.
Example: :10,20 m 0 moves lines 10 through 20, inclusive, above the first line of the file.

Shell Operations
1. Use :!command to execute command without leaving Vi.
Example: :!date will execute the shell command date and display its output.
2. Use :sh to spawn a new command shell inside Vi.
Use exit to leave that shell when done.

Until you have mastered everything on this page, you are not ready for Quiz One.

Copyright 2001 jerry_y_wang@yahoo.com. All rights reserved.

http://jerrywang.net/vi/vitutor4.html 1/1
06/04/2017 Vi For Smarties - Lesson Five

Vi For Smarties
Lesson Five

Dangerous Pitfalls
1. When you abort a command, ALWAYS hit Esc before continuing. It's easy to lose a lot of work if you forget this!
Example: If you hit d to begin deleting a chunk of text, then change your mind and decide to go look at the last line of your file by hitting G, you'll lose
the rest of your file starting from your current cursor position.
2. Use :w to save your file regularly.
3. Start vi with vi -r filename to recover the swap file of filename if you lost your editing session prematurely. The swap file may or may not be
complete, but it's better than nothing.

Some Loose Ends


1. Use J (uppercase) to Join two consecutive lines. Place the cursor on the first of the two lines.
2. Use U (uppercase) to Undo all changes made to a line if and only if the cursor has not been moved off that line yet.
3. Use o (lowercase) or O (uppercase) to open a new line. Lowercase o opens a new line beneath the current one, and uppercase O opens a new line
above the current one.
4. For a line marked with ma (replace the a with any desired character), use 'a (single normal quote) to return to the beginning of that line. Use `a
(backquote) to return to the exact position marked within that line.
5. When pasting something that does not contain a return or newline (such as a fragment of a line or a single word), p (lowercase) pastes the contents
of a buffer after the current cursor position on the same line and P (uppercase) pastes the contents of a buffer before the current cursor position on
the same line.
6. In addition to the 26 lettered buffers (a-z) which can be accessed with the " double quote, there are also 9 numbered buffers (1-9) which can be
accessed in the same way. The 1 buffer is the one that holds whatever was last cut or yanked from the text. If something else is cut or yanked, the 1
buffer's contents are moved to the 2 buffer and the new cut or yanked text then goes in the 1 buffer. If another piece of text is cut or yanked, the 2
buffer's contents go to the 3 buffer, the 1 buffer's contents go to the 2 buffer, and so on. You cannot directly choose a numbered buffer to use when
cutting or yanking text. You can only choose a numbered buffer to use when pasting text.
What this means: If you cut or yank something, then forget and accidentally cut or yank something else, the first item is not lost. It is moved to the 2
buffer. If you forget again, the first item is moved to the 3 buffer, the second item is moved to the 2 buffer, and the new item is in the 1 buffer. You
would have to forget 9 times in order to completely lose a piece of cut or yanked text. Do not forget anything 9 times.

Display Control
1. If you hit Q you will enter the line editor, ex. Type vi followed by the enter key to return to vi mode.
2. Use z. (remember the dot) to redraw the screen. Rarely necessary.

Until you have mastered everything on this page, you are not ready for Lesson Six.

Copyright 2001 jerry_y_wang@yahoo.com. All rights reserved.

http://jerrywang.net/vi/vitutor5.html 1/1
06/04/2017 Vi For Smarties - Lesson Six

Vi For Smarties
Lesson Six

Useful Movement Shortcuts


1. Use W E B (uppercase) to move forward one space-delimited Word, forward to the next End of a space-delimited word, and Back one space-delimited
word, respectively. This allows punctuation to be counted as part of a word.
Example: 3dW deletes 3 space-delimited Words.
2. Use fc to move forward to the next occurrence of the letter c on the current line. Replace the c with any character desired.
3. Use Fc to move backward to the next occurrence of the letter c on the current line. Replace the c with any character desired.
4. Use ; (semicolon) to repeat the last f or F search and , (comma) to repeat it in the opposite direction.
5. Use ) to move forward one sentence. Use ( to move backward one sentence.
6. Use } to move forward one paragraph. Use { to move backward one paragraph.
7. Use H M L to move to the Highest line on the screen, the Middle line of the screen, and the Lowest line on the screen, respectively.

Useful Editing Shortcuts


1. Use I to Insert text at the beginning of the current line. Use A to Append text to the end of the current line.
2. Use D to Delete the rest of the line starting from the cursor position.
3. Use C (uppercase) to Change the rest of the line starting from the cursor position. This deletes the rest of the line starting from the cursor position
and places you in insert mode.
4. Use c (lowercase) to change text objects. This deletes the indicated object and places you in insert mode.
Example: cw deletes the current word and places you in insert mode.
Example: cc deletes the current line and places you in insert mode.
5. Use r to replace the character under the cursor with the next character typed.
Example: rc will replace the character under the cursor with the character c and leave you in command mode.
6. Use R to begin overwriting text starting from the cursor position. Hit Esc when done to return to command mode.

Until you have mastered everything on this page, you are not ready for Lesson Seven.

Copyright 2001 jerry_y_wang@yahoo.com. All rights reserved.

http://jerrywang.net/vi/vitutor6.html 1/1
06/04/2017 Vi For Smarties - Lesson Seven

Vi For Smarties
Lesson Seven

Command Mode Macros ( :map)


1. Use :map followed by a space, a key, another space, and a macro definition to map a complex command or series of commands to that key. Not all
keys can be mapped with this command. It is best to choose a key which you rarely or never use in your editing. If the command includes any special
characters, such as Esc, Enter, Tab, or any Ctrl characters, use a <Ctrl-v> (hold down the Ctrl key and press v) before each special character.
Example: :map @ I/* <Ctrl-v><Esc>A */<Ctrl-v><Esc>0 followed by hitting Enter will make it so that hitting @ in command mode will insert C
style "/* */" comments (without the quotes) around the line which the cursor is on. Vi will go to the beginning of the line, insert a "/* " (without the
quotes), hit Esc for you, go the end of the line, append a " */" (without the quotes), hit Esc for you again, then place the cursor back on the
beginning of the line and leave you in command mode.
2. BE VERY CAREFUL! Sometimes macros can become recursive. If you are not an advanced macro user, do NOT attempt to put the key being mapped
anywhere inside the actual macro definition itself.
Example: In the C commenting macro above, you would not want to put the @ symbol anywhere in the rest of the macro.

Insert Mode Abbreviations ( :ab)


1. Use :ab followed by a space, a short abbreviation (no spaces in the abbreviation!), another space, and a definition for the abbreviation to make a
macro that will self-expand when typed in insert mode.
Example: :ab ilv I Love Vi followed by hitting Enter will cause ilv to be a macro which self-expands to the phrase I Love Vi when typed in
insert mode. From insert mode, try typing Everyone knows that ilv and I'm proud of it! You will notice that after you type the ilv and hit the
spacebar, it expands to the proper phrase.
2. BE VERY CAREFUL! The same warning that applies to command mode macros also applies to insert mode abbreviations. Sometimes the abbreviation
can become recursive.

Customizing Vi ( .exrc)
1. Any of the colon (ed/ex) commands may be put into a setup file in your home directory called .exrc so that you may customize Vi to act however you
want without having to type in each setting by hand every time you start the editor. Your .exrc should contain one complete command per line. When
entering commands into the file, omit the beginning colon. Thus, a line that would normally begin with :map would instead begin with map when it is in
your .exrc file.
2. Users of the various Vi clones, such as Vim, Nvi, and Elvis, should consult their documentation to see if there is a more appropriate name for the
setup file. Vim, for example, uses .vimrc instead.

Until you have mastered everything on this page, you are not ready for Quiz Two.

Copyright 2001 jerry_y_wang@yahoo.com. All rights reserved.

http://jerrywang.net/vi/vitutor7.html 1/1
06/04/2017 Vi For Smarties - Lesson Eight

Vi For Smarties
Lesson Eight

Reading From and Writing To Other Files


1. Use :r infile to read in the contents of infile starting at the line under the current one. Put a line number in front of the r to read in the contents
of infile starting at the line under the one specified.
Example: :25r foo.txt will read in the contents of the file named foo.txt starting at the line under line 25. Thus, the first line of foo.txt will
become line 26 in the current file.
2. Use :r !command to read in the output of command starting at the line under the current one. Put a line number in front of the r to read in the output
of command starting at the line under the one specified.
Example: :25r !date will read in the current date and time onto line 26.
3. Use :w outfile to write the contents of the current file to a file named outfile. Put a line number range in front of the w to write only those lines to
outfile.
Example: :1,10 w outfile writes lines 1 through 10 inclusive from the current file into outfile.
4. Use :1,10 w >> outfile to append lines 1 through 10 inclusive from the current file to outfile nondestructively.

Advanced Search and Replace


1. Use :s/foo/bar/ to replace the first occurrence of the word foo on the current line with the word bar.
2. Use :s/foo/bar/g to replace all occurrences of the word foo on the current line with the word bar.
3. Use :%s/foo/bar/g to replace all occurrences of the word foo in the current file with the word bar. Leaving off the g at the end only replaces the first
occurrence of foo on each line of the current file.
4. Use :%s/foo//g to delete all occurrences of the word foo in the current file. Leaving off the percent sign (%), of course, only does this for the current
line.
5. Use :%s/foo/bar/gc to have Vi query you before each attempt to replace the word foo with the word bar.

Advanced Vi Invocation
1. Use vi +/foo when invoking Vi from the command prompt to have it automatically move the cursor to the first occurrence of the string foo in the
first file being edited.
2. Use vi +24 myfile.txt when invoking Vi from the command prompt to have it start with the cursor on line 24 of myfile.txt. Replace the line
number and filename with whatever is appropriate.
3. Use view myfile.txt to start Vi in read-only mode on the file myfile.txt. On systems which do not support symbolic links, such as MS-DOS, try vi
-R myfile.txt instead.
4. NOTE: It is sometimes necessary to invoke clones of Vi by their appropriate program name, such as vim under MS-DOS.

Congratulations, you're done with Vi For Smarties! For a more advanced tutorial, see Walter Alan Zintz's The Vi/Ex Editor.

Copyright 2001 jerry_y_wang@yahoo.com. All rights reserved.

http://jerrywang.net/vi/vitutor8.html 1/1

Das könnte Ihnen auch gefallen