vi Tutorial #6: Structuring Documents This lesson lasts 10-15 minutes. You should have a strong understanding of Tutorials #1-5 before working through these advanced techniques. Lines which begin with >>> mark exercises you should try. When you want to exit this tutorial, type ZZ . ALIGNMENT --------- You have already learned commands such as o for inserting. A natural situation is to want lines that you insert aligned with preceding ones. To arrange for this to happen, use :set autoindent . To cease that, use :set noautoindent . :set autoindent enable automatic alignment during insertions :set noautoindent disable automatic alignment during insertions >>> Try :set autoindent now. Then add lines to the following >>> practice text. Move the cursor to the word "PRACTICE", type o , >>> and press or a couple of times, inserting blank >>> lines. Notice that vi keeps the cursor aligned with the line >>> where you started inserting. If you type some text, it will be >>> aligned with that. Press to stop inserting. PRACTICE One day a bright programmer found VI was a big turnaround: It makes you feel warm 'Cause it works like a charm, And really cool features abound! >>> Move to one of the two indented lines in that practice text. >>> Type o , and press or a couple of times, >>> inserting blank lines. Notice that vi keeps the cursor aligned with >>> the line where you started inserting. If you type some text, it >>> will be aligned with that. Press to stop inserting. >>> Use :set noautoindent to disable automatic alignment. >>> Then retry some of the insertions you just did above. Notice that >>> vi now doesn't align new lines with old ones, it just starts at >>> the left margin. If you have done :set autoindent , vi will align text also if you insert in the middle of a line and press or , breaking the line. ADVANCED SHIFTING ----------------- :set shiftwidth=# set shift-amount for <, >, etc. (e.g. :set shiftwidth=4 , :set shiftwidth=3 ) You have already learned the > and < commands. >movement Shift right to where the movement command specifies >> Try :set shiftwidth=4 now. Then move the cursor to the >>> paragraph of sample text below. Type >> and << on lines other >>> than the last one, shifting the material back and forth so the text >>> is all aligned along its left. >>> Next try >} to shift the paragraph to the right, then <} to >>> shift it left, then type . until all four lines start at the >>> left edge. THIS IS THE FIRST LINE OF EXAMPLE TEXT IS EXAMPLE TEXT END SHIFTING WHEN INSERTING ----------------------- ^T shift (insert) ^D unshift (insert) 0^D unshift to edge (insert) When you are inserting, you can use ^T to TAB and ^D to DOWN-SHIFT or backwarD-tab by the shift-width value that you set and use with > and < . >>> Type :set autoindent and :set shiftwidth=4 . >>> Here is sample text again. Your goal will be to reproduce it >>> below, doing the shifting while inserting: THIS IS THE FIRST LINE OF EXAMPLE TEXT IS EXAMPLE TEXT END >>> Below is the beginning of another copy of the sample text. >>> Starting with this beginning, reproduce the material above by >>> doing the following: >>> Move the cursor to this beginning line below. >>> Type o to start inserting a new line below it. >>> Type ^T to shift to the right while inserting. >>> Insert IS , and press the ENTER or RETURN key to go to the next >>> line. The cursor should be aligned with IS . >>> Type ^D to shift to the left while inserting. Type ^D a >>> second time here. >>> Type EXAMPLE . >>> On the next line, use ^T to shift to the right three times. >>> Enter TEXT END . >>> End inserting by pressing ESC. THIS IS THE FIRST LINE OF EXAMPLE TEXT When inserting, if you want to shift all the way to the left edge, type 0^D . When you do this, the 0 is visible at first. >>> Type :set autoindent . >>> Move the cursor to the sample line of text below, and type o to >>> start inserting a new line below it. The cursor should be >>> under the "T" at the beginning of the given line. Type 0^D >>> to shift to the left edge while inserting. Enter TEXT END and >>> then end inserting by pressing ESC. THIS IS THE FIRST LINE OF EXAMPLE TEXT LINE-NUMBERS ------------ :set number have line-numbers :set nonumber hide line-numbers If you use the command :set number , vi will show line-numbers along the left side. If you want vi to not show line-numbers, use the command :set nonumber . >>> Try :set number now. Use the #G command to go to lines, >>> e.g. 136G or 142G . Then type :set nonumber . SETTINGS -------- :set show settings You've seen that you can set various things such as "autoindent", "shiftwidth", and "number". You can use the command :set to see what you have set. >>> Try :set now. STARTING vi AT A SPECIFIED PLACE -------------------------------- vi +# filename vi filename starting with the cursor at line # You can start vi at a specific line-number by typing vi +# filename , e.g. vi +738 data or vi +3 memo . >>> Use the command ^G or :set number to get the number of a >>> line here in this tutorial, say line number 169. Then, exit >>> this tutorial via the command ZZ and then restart to your >>> chosen line in this tutorial by typing vi +169 6structuring >>> or use the number you chose instead of 169. SUMMARY ------- :set autoindent enable automatic alignment during insertions :set noautoindent disable automatic alignment during insertions :set shiftwidth=# set shift-amount for <, >, etc. (e.g. :set shiftwidth=4 , :set shiftwidth=3) ^T shift (insert) ^D unshift (insert) 0^D unshift to edge (insert) :set number have line-numbers :set nonumber hide line-numbers :set show settings vi +# filename vi filename starting with the cursor at line # (e.g. vi +169 6structuring ) These commands should significantly facilitate your working on structured documents, particularly programs. Have a nice day. Copyright (c) 2000 Hugh McGuire. All Rights Reserved.