Why does vim sometimes display linebreaks incorrectly? - vim

I have a long line of text in vim, soft-wrap is on, I've done :set linebreak, and breakat has the right value.
The problem is, when I edit some text in the middle of the "paragraph", the lines don't wrap at word boundaries anymore.
Example:
1) Cursor is in the middle of the line:
2) I type cw and the linebreaks change:
It stays like that when I go back to normal mode, but wraps correctly again when the cursor leaves the line.
This does not happen if I add text in the middle, or do a command like dw - seemingly just commands that delete some text and enter insert mode.
The problem began on a fresh install of Fedora 21, but my .vimrc is unchanged from my previous computer, where I did not have this problem.
How do I restore the correct behavior, or is this perhaps a bug in the Fedora package?

I believe, this is a bug, that has been fixed with 7.4.576

Related

Vim: Select all without scrolling away

is it possible in vim to select all lines in the current file, but leave the position where my cursor is unchanged?
Let's say I am currently at line 500 (of 3000) and want to quickly select everything (not yank), as my selection is simply set up to show whitespace characters. Can this be done without leaving my current line?
To achieve exactly what you like, you can press the following:
ggVG<Esc><Ctrl-O><Ctrl-O>
gg moves to the beginning of the file
V starts visual line mode
G moves to the and of the file (now you have selected the whole
file)
<Esc> leaves visual mode
<Ctrl-O> moves your cursor back to the prevois location (first to the beginning of the file, then the second time to your last position before pressing gg)
And if you like to select only the visible lines in you window (to not scroll away). You can use HVL instead of ggVG (H moves to the top of your window and L to the bottom).
You also could show whitespaces without using visual selection with something like this in your .vimrc:
set list listchars=tab:»·,trail:·,nbsp:·
This helps me to detect trailing whitespaces, and mixed (spaces/tabs) indentation.
usually pressing
ggVG
in normal mode will select all the lines, but it will leave your cursor at the last line of the file.
If you wants to highlights the whitespace characters then you can highlight this by using the below command in command mode (this white color chosen is for dark theme)
: hi ExtraWhitespace ctermbg=White guibg=White
Depending on what you are trying to achieve you can use something like :
%cmd
To apply the command to the whole file.
For example, %y will yank the whole file, %=will format the whole file, without moving your cursor. It does not really work if you do something like %d...
It is not a real selection though but rather a way to apply a command on the whole file.
To go further you can use something like
%norm Atest
To add 'test' at the end of each line. (Actually this is a bad example, because this command will move to the last line...)
It is not possible to have the cursor inside a visual selection. This caused by that, vim defines visual selection through two marks. As soon as you move the cursor one of the marks gets updated. Basically this means one of the marks is always lays where the cursor is(at least when using "v" to select). You cannot have the border in the middle of the region that the border defines :)

Vim Relative Numbering Reset on Scroll

When I scroll down a page the relative numbering is no longer based upon the cursor position.
Instead the line position relative to the top of the screen is displayed.
Sometimes I would like to delete or yank 200 lines and I dont want to have to do the subtraction and addition to figure out how many lines down my text is.
How can I show relative line numbers to the cursor even when scrolling?
I think what you want is, you scroll with mouse, and expect that vim keeps the cursor in original place. E.g. your cursor is at line 5, and you scroll down 5000 lines, you expect your cursor is still at line 5. That is, the cursor is out of the window.
AFAIK, the cursor won't go out of the window. That means, if you keep scrolling down, and the cursor line will be the top line of your current window. and the rnu are gonna re-calculated by the cursor line.
May be you could just explain what do you want to do. the cases in your question could be done by 200dd or 200Y but I guess it is not as simple as that.
You may want to find out the ending line by reading/scanning your text lines, and pick the line number (rnu), and do a xxxdd if this was the case. Here you should use normal line number. e.g. your cursor was at line 5, and you scroll down a lot, find the line you want to delete till from line 5. you could do :5,.d vim will delete from line 5 to your current line.
Or you can do 5, 23452d if you find out the lines between 5 and 23452 need to be removed.
If you can search your ending line by /pattern search, vim can do :.,/foo/d this will delete from current line till the next line, which matches foo.
You can still press V enter line-wise visual mode, and moving down by vim-motions. when it reaches the point you want to remove/yand press Y or d
You can take a look this Question/answer:
VIM to delete a range of lines into a register
At the end, I suggest you not using mouse in vim.
This is probably because the cursor moves down a page when you scroll down a page. In vim, the cursor is always on the screen. If you're scrolling down with, say, the mouse wheel, the cursor will just get "stuck" on the top line (modulo scrolloff) and stay there as you continue to scroll down.
Perhaps use ShiftV to start a line-based visual selection before scrolling, then use d or y on the selection?
I can confirm that the desired feature is available in Visual Studio Code (VSC) with the Vim extension installed. This is because VSC does not function like Vim by default and holds the cursor in place like other text editors do. This feature not only makes VSC bearable but proves more useful than vanilla Vim when coding large blocks of code also.
Additionally, VSC also allows for easy and language agnostic comment/uncomment toggling with <Ctrl> + / which is also very useful when used together with the above feature.

Wrap long lines in Vim?

I've noticed that gq does not work when I paste in a long line. For example, with a textwidth=72 and formatoptions=tcroqbnl, gq refuses to wrap this (in insert mode, I pasted the entire label contents, and then exited insert mode with ESC):
<label for="contact_reason_1">To get assistance with or to confirm a tire replacement recommendation</label>
If I add a line break in (after "to", for example), it'll wrap then. The funny thing is if I join the line back together, it'll happily wrap it again. So VIM seems to somehow be remembering "oh, this is one paste, don't wrap it".
How do I turn that feature off? I'd like gq in command mode to always work. Taking l out of formatoptions did not seem to help (and it shouldn't, this isn't insert mode).
clarification
Yes, I'm using a motion command, in particular, gq<Right>. formatexpr and formatprog are both unset. If it matters, this is in gvim on Debian GNU/Linux, vim version 7.2p284.
steps to reproduce
Pop up gvim on an open file.
Press i to get into insert mode, then type This is a long line. A long line. But not wrappable yet. Or yet. Soon.
Press ESC, then I. Type Now putting text in front of the long line. note: there is a space after the final period, can't get SO to show it, except when this note is here. FUN.
Press ESC, then A. Type And some after. note: space before the And, same SO problem.
Press ESC one last time. Now try gq<Left>, note it only wraps And some after.; I can't get vim to wrap the rest of the line (without going into insert mode and doing a line break by hand, then it works).
Fixing this state is doable; putting a newline after "now" and then hitting undo makes line wrap work again. WTF.
gq isn't enough to wrap the text. You have to give it a motion over which to wrap (like gqj) or tell it to wrap the current line with gqq. Are you sure you're not just mistyping it?
If you aren't, what are the formatexpr and formatprg options set to, if anything?
Update
The problem is the b setting in formatoptions. That's telling Vim to only wrap the text added during the last insertion.
I find that if I select the line before doing the gq, it works fine. Doesn't gq want to be combined with some text selection operation to work?
UPDATE
I confirm the bug. Running vim -u NONE, my formatoptions are vt.
Maybe Bram Molenar or at least the vim community would be interested?

Vim keep cursor location while scrolling

Is there a way to keep the cusror location off-screen in Vim / gVim while scrolling? Similar to many Windows editors.
I know about marks, and do use them. I also know the '.' mark (last edit location), But looking for other ideas.
I'm asking this because sometimes i want to keep the cursor at some location, scroll to another place using the mouse-wheel, and then just press an arow key or something to get me back to that location.
No. vim is a console application, so it doesn't really make sense to have the cursour off-screen (it's possible, but would just be confusing)
An alternative solution, to paraphrase posts from this thread from comp.editors:
Ctrl+o goes to the previous cursor location, Ctrl+i goes to the next (like undo/redo for motions)
Marks seem like the other solution..
Also, use marks. Marks are named by letters. For instance typing ma remembers
the current location under mark a. To jump to the line containing mark a,
type 'a. To the exact location use `a.
Lower-case-letter marks are per-file. Upper-case-letter marks are global;
`A will switch to the file containing mark A, to the exact location.
Basically ma, move around, then `a to jump back.
Another option which Paul suggested,
gi command switches Vim to Insert mode and places cursor in the same position as where Insert mode was stopped last time.
Why don't you split the window, look at what you wanted to look at, and then close the split?
:split
or
:vsplit (if you want to split vertically)
The only similar behavior that I've found in Vim:
zt or zENTER "scroll the screen down as far as possible without moving the cursor"
zb "scroll as far up as possible".
Ctrl+E "scroll one line down, if possible"
Ctrl+Y"scroll one line up, if possible"
Sometimes you can avoid jumping to marks before entering text — gi command switches Vim to Insert mode and places cursor in the same position as where Insert mode was stopped last time.
Google says that the cursor (and therefore current line) must be visible in Vi, so you'll have to use marks.
Also very useful are the '' (2x single quotes) and `` (2x back quotes).
The former jumps back to the line you were prior to the last jump (for instance, a page down).
The latter jumps back to the line and column you were prior to the last jump.

How to fix Home and End in Vim?

I'm using vim in gnome-terminal (2.26.0): although I use 95% of the time "$" to navigate to the EOL and "0" for the opposite, every now and then I hit "Home" or "End".
When I use Home, the text I have in the current line is moved on line down, leaving me in insert mode in the current line and the letter H appears at the beginning of the line.
When I hit End, it's the same but with an F instead of H.
Why does it happen? How can I fix it? (fixing would mean to have the standard functionality when hitting these keys).
This happens because pressing the home and end keys in a terminal sends an escape sequence consisting of several characters to vim, and vim isn't correctly associating these escape sequences back with the keys you pressed.
To fix this you need to adjust the term setting. Gnome-terminal is xterm compatible, so you could try adding this to your .vimrc:
set term=xterm-256color
The term setting is derived from the TERM environment variable, so you might want to investigate why it isn't set correctly in the first place.

Resources