Nvim :help pages, generated
from source
using the tree-sitter-vimdoc parser.
<Enter> yourself. This is useful when you are writing programs where
you want to decide where the line ends. It is not so good when you are
creating documentation and want the text to be at most 70 character wide.
If you set the 'textwidth' option, Vim automatically inserts line breaks.
Suppose, for example, that you want a very narrow column of only 30
characters. You need to execute the following command::set textwidth=30Now you start typing (ruler added):
1 2 3 12345678901234567890123456789012345 I taught programming for a whiIf you type "l" next, this makes the line longer than the 30-character limit. When Vim sees this, it inserts a line break and you get the following:
1 2 3 12345678901234567890123456789012345 I taught programming for a whilContinuing on, you can type in the rest of the paragraph:
1 2 3 12345678901234567890123456789012345 I taught programming for a while. One time, I was stopped by the Fort Worth police, because my homework was too hard. True story.You do not have to type newlines; Vim puts them in automatically.
1 2 3 12345678901234567890123456789012345 I taught for a while. One time, I was stopped by the Fort Worth police, because my homework was too hard. True story.This does not look good. To get the paragraph into shape you use the "gq" operator. Let's first use this with a Visual selection. Starting from the first line, type:
v4jgq"v" to start Visual mode, "4j" to move to the end of the paragraph and then the "gq" operator. The result is:
1 2 3 12345678901234567890123456789012345 I taught for a while. One time, I was stopped by the Fort Worth police, because my homework was too hard. True story.Note: there is a way to do automatic formatting for specific types of text layouts, see auto-format.
gqap"ap" stands for "a-paragraph". This formats the text of one paragraph (separated by empty lines). Also the part before the cursor. If you have your paragraphs separated by empty lines, you can format the whole file by typing this:
gggqG"gg" to move to the first line, "gqG" to format until the last line. Warning: If your paragraphs are not properly separated, they will be joined together. A common mistake is to have a line with a space or tab. That's a blank line, but not an empty line.
:{range}center [width]
{range} is the usual command-line range. [width] is an optional line width to
use for centering. If [width] is not specified, it defaults to the value of
'textwidth'. (If 'textwidth' is 0, the default is 80.)
For example::1,5center 40results in the following:
I taught for a while. One
time, I was stopped by the
Fort Worth police, because my
homework was too hard. True
story.
:1,5right 37gives this result:
I taught for a while. One
time, I was stopped by the
Fort Worth police, because my
homework was too hard. True
story.
:{range}left [margin]
Unlike ":center" and ":right", however, the argument to ":left" is not the
length of the line. Instead it is the left margin. If it is omitted, the
text will be put against the left side of the screen (using a zero margin
would do the same). If it is 5, the text will be indented 5 spaces. For
example, use these commands::1left 5 :2,5leftThis results in the following:
I taught for a while. One time, I was stopped by the Fort Worth police, because my homework was too hard. True story.
:packadd justify
Or put this line in your vimrc::packadd! justify
This Vim script file defines a new visual command "_j". To justify a block of
text, highlight the text in Visual mode and then execute "_j".
Look in the file for more explanations. To go there, do "gf" on this name:
$VIMRUNTIME/pack/dist/opt/justify/plugin/justify.vim.:%!fmt
<Tab> at the start of each line. Take this
text:the first line the second lineThis is entered by typing
<Tab>, some text, <Enter>, <Tab> and more text.
The 'autoindent' option inserts indents automatically::set autoindentWhen a new line is started it gets the same indent as the previous line. In the above example, pressing the
<Tab> key after <Enter> is not needed anymore.<C-t>.
The amount of indent added is specified with the 'shiftwidth' option. The
default value is 8. To make ">>" insert four columns worth of indent, for
example, type this::set shiftwidth=4When used on the second line of the example text, this is what you get:
the first line
the second line
"4>>" will increase the indent of four lines.<Tab> you still get 8 columns worth of indent. To change
this, set the 'softtabstop' option::set softtabstop=4Vim now creates invisible tab stops for your cursor every 4 columns; hitting
<Tab> jumps to the next stop and inserts the exact mix of spaces or tabs
needed.:set guioptions+=bOne horizontal scrollbar will appear at the bottom of the Vim window.
|<-- current window -->|
some long text, part of which is visible in the window
ze |<-- window -->|
zH |<-- window -->|
4zh |<-- window -->|
zh |<-- window -->|
zl |<-- window -->|
4zl |<-- window -->|
zL |<-- window -->|
zs |<-- window -->|
g0 to first visible character in this line
g^ to first non-blank visible character in this line
gm to middle of screen line
gM to middle of the text in this line
g$ to last visible character in this line
|<-- window -->|
some long text, part of which is visible in one line
g0 g^ gm gM g$
+---------------------------------+ |letter generation program for a b| |ank. They wanted to send out a s| |pecial, personalized letter to th| |eir richest 1000 customers. Unfo| |rtunately for the programmer, he | +---------------------------------+
:set linebreakit looks like this:
+---------------------------------+ |letter generation program for a | |bank. They wanted to send out a | |special, personalized letter to | |their richest 1000 customers. | |Unfortunately for the programmer,| +---------------------------------+
:map <Up> gk :map <Down> gjTURNING A PARAGRAPH INTO ONE LINE edit-paragraph-join
:g/./,/^$/joinThat looks complicated. Let's break it up in pieces:
+----------------------------------+ |A letter generation program | |for a bank. They wanted to | |send out a special, | |personalized letter. | | | |To their richest 1000 | |customers. Unfortunately for | |the programmer, | +----------------------------------+
+----------------------------------+ |A letter generation program for a | |bank. They wanted to send out a s| |pecial, personalized letter. | |To their richest 1000 customers. | |Unfortunately for the programmer, | +----------------------------------+
:g/\S/,/^\s*$/joinThis still requires a blank or empty line at the end of the file for the last paragraph to be joined.
nice table test 1 test 2 test 3 input A 0.534 input B 0.913You need to enter numbers in the third column. You could move to the second line, use "A", enter a lot of spaces and type the text. For this kind of editing there is a special option:
set virtualedit=allNow you can move the cursor to positions where there isn't any text. This is called "virtual space". Editing a table is a lot easier this way. Move the cursor by searching for the header of the last column:
/test 3Now press "j" and you are right where you can enter the value for "input A". Typing "0.693" results in:
nice table test 1 test 2 test 3 input A 0.534 0.693 input B 0.913Vim has automatically filled the gap in front of the new text for you. Now, to enter the next field in this column use "Bj". "B" moves back to the start of a white space separated word. Then "j" moves to the place where the next field can be entered.
CTRL-V to start blockwise Visual mode.
nice table test 3 test 1 test 2 test 3 input A 0.693 0.534 0.693 input B 0.913Notice that the whole "test 1" column was shifted right, also the line where the "test 3" column didn't have text.
:set virtualedit=
inp 0.693 0.534 0.693
|
rx |
V
inpx0.693 0.534 0.693
The layout is messed up. To avoid that, use the "gr" command:inp 0.693 0.534 0.693
|
grx |
V
inpx 0.693 0.534 0.693
What happens is that the "gr" command makes sure the new character takes the
right amount of screen space. Extra spaces or tabs are inserted to fill the
gap. Thus what actually happens is that a tab is replaced by "x" and then
blanks added to make the text after it keep its place. In this case a
tab is inserted.
When you need to replace more than one character, you use the "R" command
to go to Replace mode (see 04.9). This messes up the layout and replaces
the wrong characters:inp 0 0.534 0.693
|
R0.786 |
V
inp 0.78634 0.693
The "gR" command uses Virtual Replace mode. This preserves the layout:inp 0 0.534 0.693
|
gR0.786 |
V
inp 0.786 0.534 0.693
:set tabstop=4This updates the visual layout, but the file itself remains unchanged. Another possibility is to permanently reformat the file. For this Vim provides the :retab command. First, set 'tabstop' to match original layout (as above), then run:
:retab 8The ":retab" command will change 'tabstop' to 8, while changing the text such that it looks the same. It changes spans of white space into tabs and spaces for this. You can now write the file. Warning: When using ":retab" on a program, it may change white space inside a string constant. Therefore it's a good habit to use "\t" instead of a real tab.