Is a blank line considered part of a paragraph in Vim? - vim

In the Vim help a paragraph is defined as follows: "A paragraph begins AFTER each empty line..." This implies that the empty line is not part of the new paragraph but belongs either to the previous paragraph or is not part of any paragraph. However, the command } supposedly takes one "to the beginning of the next paragraph". Since this command positions the cursor ON the blank line this implies that the blank line does belong to the next paragraph. Otherwise, one would expect that the command } would position the cursor at the start of the text, and not before it on a blank line. How is one to account for this?

No, the blank line is not part of the paragraph. You could test this with dip
which will leave the empty lines alone.
} does not move to the next paragraph, it moves to the first position after a paragraph, which in your case is a newline.
It is a exclusive (:h exclusive) motion, which means, that it moves the cursor further than the block. If you use d} the newline will not be deleted.

Related

Vim moving cursor to the beginning of the next line

In Vim, is there a key moving cursor to the beginning of the next line?
j moves the cursor to the corresponding position of the current position in the next line. I'm looking for a single key that could move the cursor to position 0 irrespective of the cursor position in the current line.
The + motion moves to the first non-blank character in the next ([count]) line. That fulfills your requirement if there's no indent. With indent, you can use +0 or j0.
You can move to the beginning of a line by pressing +
That just moves to the first non-blank character at the beginning of the line, which is fine if there is no indentation. If you want to go to the first character in the line regardless of white space, you can do j0
If you need any more help with movement keys, this is a good resource:
http://vim.wikia.com/wiki/Moving_around

What is the definition of a non-blank character in Vim?

Launch vim.
In the empty buffer, insert two lines where the first line consists of 3 spaces and the second line consists of hello world.
Here is an example file where the spaces are represented with dots.
...
hello world
Now press gg. The cursor moves to the third space of the first line.
Quoting :help gg:
<C-Home> or *gg* *<C-Home>*
gg Goto line [count], default first line, on the first
non-blank character |linewise|. If 'startofline' not
set, keep the same column.
The documentation says that the cursor should move to the first non-blank character of the first line. I have two questions.
Does :help document the definition of a non-blank character? If so, could you please point me to it?
Is the behaviour that we observe in the experiment mentioned above consistent with the documentation provided in :help gg?
I don't think there is a general definition of non-blank in the vim docs, but I also do not believe this is a "side effect" of gg.
Note that gg is consistent here with ^:
^ To the first non-blank character of the line.
|exclusive| motion.
and [:blank:] of vim's pattern matching behavior (:h blank) defines blank characters as space and tab:
*[:blank:]* [:blank:] space and tab characters
As far as whether or not this is consistent with gg, consider what it says it will do as two steps instead of one:
Go to the first line (default since no count was specified) -- it does this.
Go to the first non-blank character of said line.
Probably the easiest way to implement 2 as an algorithm is to position the cursor after all the blank characters at the beginning of the line. In your case, they are all blank characters (and it cannot move to the next line), so the cursor is positioned at the end of the line (after all the blank characters).

How to jump to the exact last change point in Vim

'. can jump to the line of last change. But I need to jump to the exactly point.
Use `.. That is, backtick followed by a dot.
As a change has a beginning and an end, `[ and `] are also very helpful:
'[ `[ To the first character of the previously changed
or yanked text.
'] `] To the last character of the previously changed or
yanked text.
After executing an operator the Cursor is put at the beginning of the text
that was operated upon. After a put command ("p" or "P") the cursor is
sometimes placed at the first inserted line and sometimes on the last inserted
character. The four commands above put the cursor at either end. Example:
After yanking 10 lines you want to go to the last one of them: "10Y']". After
inserting several lines with the "p" command you want to jump to the lowest
inserted line: "p']". This also works for text that has been inserted.
answers are kinda old, but for current reference:
:changes show a list of changes
g; jump to last edit
g, jump forward in last edit's
found at vimtricks
I suggest a mapping to jump to the last changing point, as we have:
gv ................... repeats last selection
gi ................... enters insert at the last inserting point
" gl to jump to the last change "exactly"
nnoremap gl `.
Reference: https://twitter.com/dotvimrc/status/191163723065462786

question about text formatting in Vim

I'm using Vim to write text that isn't code, just free form writing. To do this I like to have formatoptions include the w flag and sometimes the a flag.
The w flag is supposed to make it so an end-of-paragraph occurs when a line ends in a non-blank character, rather than at first blank line (which is Vim default). Here is snippet from the help docs:
w Trailing white space indicates a paragraph continues in the next line.
A line that ends in a non-white character ends a paragraph.
The a flag makes reformatting automatic, as you type. For example, in document with text below and w and a flags the lines reformat as I type only up to the 'last line in paragraph' and not below.
This it the first line of the paragraph. <cr>
The text in it reformats properly as I type <cr>
up to the last line of the paragraph, which is <cr>
right after this line, that is, just below here: <cr>
This is the last line of paragraph.<cr>
This is the first line of second paragraph. <cr>
Even though there is no blank line between <cr>
it and first paragraph, any reformatting of <cr>
the first paragraph ends at the last line <cr>
of first paragraph.<cr>
<cr>
This the the first line after second paragraph<cr>
Now to my question:
With a and w flags the reformatting works properly as I type in the first paragraph, i.e, second paragraph remains untouched. But sometimes I want to select the current paragraph using vap. It seems to me that with the w flag set this should select only the first paragraph. But in fact issuing vap in first paragraph selects the second paragraph also, all the way down to the blank line with <cr>, seemingly ignoring the w formatoptions flag.
Is this expected behavior? Am I missing something? Why does vap not select only the paragraph I'm in, which ends at first line with no trailing whitespace?
If you read the help at :h paragraph this does not mention the 'fo' setting. So it
looks like the fo setting does not really change, what defines a paragraph. This might be a bug or unclear documentation, so you might want to discuss this on the vim-dev mailinglist.
From :help text-objects or :h paragraph you can see that a paragraph for vim begins after each empty line, and also at each of a set of paragraph macros, specified by the pairs of characters in the 'paragraphs' option. Blank lines containing only whitespace are also paragraph boundaries.
The w flag in formatoptions only makes a difference when autoformatting a paragraph as you type: it will split lines and leave whitespace at the end of lines which do not end a paragraph. I suppose the help text is a bit confusing.

Why does Vim position the caret one character off with $ (end of line)?

Observe a line in a Vim instance:
Now I hit $:
Why does my cursor not go all the way to the end? Once I try inserting, the text gets inserted before the last character! Even if I try to move right again while still in normal mode I get the bell. Oddly, when in edit mode I can move to the actual end of line with the right arrow key!
Does anyone know why Vim does this? On 7.3 by the way. Thanks for the help.
Pressing $ while in command mode causes the cursor to move to the end of the line, effectively highlighting the last character. Hit i here to insert before the last character, or a to append to the line. It is slightly ambiguous here, because you're using a pipe character as a cursor rather than a rectangular block cursor. Have a look at ":help termcap-cursor-shape" if you want to change that.
If the goal is to append to the end of the line, A will jump to the end of the line and enter insert mode with a single keypress.
Use a to append a character after the current.
Or, to go to the end of the line and append in 1 step, use capital A. I.e. shiftA.
Similarly shift-I to insert at the beginning of the line without first having to press ^.
The cursor can't be between two characters, it is always on a character.
If you press $ then x, you will correctly delete the last printable character of the current line.
What you are observing is the fact that using i, you are always inserting your text before the selected character. If you want to insert after the selected character, you have to use a or better A as it has already been mentioned.
In other words:
i means "insert before character under cursor".
a means "insert after character under cursor".
mnemonic for a : a for "append".

Resources