Location: Asia Pacific, Cyberspace, in the Dark Dystopia
Posts: 19,118
Thanks Given: 2,351
Thanked 3,359 Times in 1,878 Posts
We are going to put each one of these seven lessons into it's own thread and then see if we can get a discussion going on each (improvements, suggestions, errors, etc.) - Maybe we'll make a new (better?) set?
We are going to put each one of these seven lessons into it's own thread and then see if we can get a discussion going on each (improvements, suggestions, errors, etc.) - Maybe we'll make a new (better?) set?
vi is a programmers editor and programmers usually want their texts to be neatly indented. One doesn't write:
but rather
To indent every line by hand is possible - but there computers for that, no? Alas, it is not obvious how to do this with vi. Here is, how it works.
The first thing you have to decide upon is how many characters you want to indent. I am most comfortable with 5 characters, as you can see in the little piece of pseudocode above, but that is just personal taste - whatever works for you is fair game. How many characters make one "indent-step" is controlled by the variable "sw" (for shift-width), which you can set at the colon-prompt (lasting only for the current session) or in your "~/.exrc" (for lasting effect). Enter:
or any other number of characters you want to indent. Now have the following keys/possibilities: at the colon-prompt you can use ">" and "<" along with ranges to move (a number of) lines to the left or right. For instance, sippose your current line is 2 and you want to indent the while-block one level:
You can also use "<<" and ">>" as commands, which will move the current line one level leftwards/rightwards. Prepend these commands with numbers to move blocks of lines. In the above example "3>>" issued with line 2 as current line would have done the same thing. This way you can reindent large pieces of code you have copied from somewhere else quickly.
You probably want new lines to start automatically at the same indent level as the last line. For this there is another vi switch which complements "sw": it is "ai" (auto-indent). Again, set it from the colon-prompt ("set ai" to switch on / "set noai" to switch off) or put that in your "~/.exrc"-file for lasting effect. When you hit "<ENTER>" to end a line you will notice that the cursor will be positioned in the next line not in column 1 but at the same column where the last line started. Indent a line, switch to insert mode by pressing "A" (append to end of line) and the press <ENTER> to see the effect.
CAUTION: You probably sometimes change to insert mode, mark something with your mouse in another window and then paste it into your vi buffer. You can do that, but when autoindent mode is set lines not start in the leftmost line will "accumulate" the indentation.
Paste this:
into a vi session with autoindent on and it looks like this:
Pasting for vi is just keystrokes and the end of a line is like an <ENTER>. Switch off the mode at the colon-prompt first (":set noai"), paste and only then switch it back on (":set ai").
All this is nice and fine, but you probably want also a way to change indent levels while in insert mode, because you don't want to first write a line, then change its indentation, then write the next line, etc.. For this there are two commands which change the indentation level of a whole line in insert mode:
Both shift the whole current line, regardless of cursor position, to the right (CTRL-T) or the left (CTRL-D). Both will not leave insert mode.
Another word of caution: vi uses tab characters to indent lines when the indentation is deeper than one tab only fills up with spaces. It even silently replaces space characters with tabs if you change a lines indentation. Many people (me included) don't like their program files not to be indented with tabs, because tabs width can be redefined to destroy the proper indentation. I have not found yet how to switch off this rather annoying behavior, instead i have written a small sed-program to change leading tabs to (eight) spaces. Replace "<spc>" and "<tab>" with literal tabs/spaces:
I hope this helps.
bakunin
Last edited by bakunin; 10-12-2012 at 05:30 AM..
These 2 Users Gave Thanks to bakunin For This Post:
Lesson 1:
A man is getting into the shower just as his wife is finishing up her shower, when the doorbell rings.
The wife quickly wraps herself in a towel and runs downstairs.
When she opens the door, there stands Bob, the next-door neighbour.
Before she says a word, Bob says, 'I'll... (2 Replies)
I downloaded vim.7.2 and compiled the vim source .
Added the vim binary path to PATH (Because iam not the root of the box)
when i load the file using vim it throws me an error
Error detected while processing /home2/e3003091/.vimrc:
line 2:
E185: Cannot find color scheme darkblue
line... (0 Replies)
I started wrting c lessons for absoulute begginers to advanced users, I think material can be considered quality, and my students learn programming with this stuff. If you wish feel free to start reading it here:
www.visualcmaniac.com
its only 5 lessons for now so maybe could be easier for... (4 Replies)
Hi everyone,
I'm looking to hire for private lessons a individual who is presently working as a unix system administrator or instructor in school who is teaching unix. I live in Clifton nj my nubmer is Cell **no phonenumbers on this forum** or email
**no emails on this forum** please let me... (1 Reply)