to
use 4 spaces: >
:set shiftwidth=4
The "<" command removes one shift amount of whitespace at the left
edge of the block. This command is limited by the amount of text that is
there; so if there is less than a shift amount of whitespace available, it
removes what it can.
JOINING LINES
The "J" command joins all selected lines together into one line. Thus it
removes the line breaks. Actually, the line break, leading white space and
trailing white space is replaced by one space. Two spaces are used after a
line ending (that can be changed with the 'joinspaces' option).
Let's use the example that we got so familiar with now. The result of
using the "J" command:
This is a long line short Any other long line ~
The "J" command doesn't require a blockwise selection. It works with "v" and
"V" selection in exactly the same way.
If you don't want the white space to be changed, use the "gJ" command.
==============================================================================
*10.6* Reading and writing part of a file
When you are writing an e-mail message, you may want to include another file.
This can be done with the ":read {filename}" command. The text of the file is
put below the cursor line.
Starting with this text:
Hi John, ~
Here is the diff that fixes the bug: ~
Bye, Pierre. ~
Move the cursor to the second line and type: >
:read patch
The file named "patch" will be inserted, with this result:
Hi John, ~
Here is the diff that fixes the bug: ~
2c2 ~
< for (i = 0; i <= length; ++i) ~
--- ~
> for (i = 0; i < length; ++i) ~
Bye, Pierre. ~
The ":read" command accepts a range. The file will be put below the last line
number of this range. Thus ":$r patch" appends the file "patch" at the end of
the file.
What if you want to read the file above the first line? This can be done
with the line number zero. This line doesn't really exist, you will get an
error message when using it with most commands. But this command is allowed:
>
:0read patch
The file "patch" will be put above the first line of the file.
WRITING A RANGE OF LINES
To write a range of lines to a file, the ":write" command can be used.
Without a range it writes the whole file. With a range only the specified
lines are written: >
:.,$write tempo
This writes the lines from the cursor until the end of the file into the file
"tempo". If this file already exists you will get an error message. Vim
protects you from accidentally overwriting an existing file. If you know what
you are doing and want to overwrite the file, append !: >
:.,$write! tempo
CAREFUL: The ! must follow the ":write" command immediately, without white
space. Otherwise it becomes a filter command, which is explained later in
this chapter.
APPENDING TO A FILE
In the first section of this chapter was explained how to collect a number of
lines into a register. The same can be done to collect lines in a file.
Write the first line with this command: >
:.write collection
Now move the cursor to the second line you want to collect, and type this: >
:.write >>collection
The ">>" tells Vim the "collection" file is not to be written as a new file,
but the line must be appended at the end. You can repeat this as many times
as you like.
==============================================================================
*10.7* Formatting text
When you are typing plain text, it's nice if the length of each line is
automatically trimmed to fit in the window. To make this happen while
inserting text, set the 'textwidth' option: >
:set textwidth=78
You might remember that in the example vimrc file this command was used for
every text file. Thus if you are using that vimrc file, you were already
using it. To check the current value of 'textwidth': >
:set textwidth
Now lines will be broken to take only up to 78 characters. However, when you
insert text halfway through a line, or when you delete a few words, the lines
will get too long or too short.