Vim to always only jump one row at a time [duplicate] - vim

I can get the bottom of the window to display partial lines by setting display=lastline
Is there a similar option that would allow partial lines to be displayed at the top of a window?
Without this functionality, my vim still scrolls (down) by more than 1 screen line when the topmost file line being displayed is wrapped into multiple screen lines.
For example, in the following scenario, when I press C-E, my vim will scroll down by 3 screen lines. Is there a way to make it scroll by only 1 screen line such that only the first two words in line 1 are hidden, but the following 3 words are still displayed? This will be very useful for editing long paragraphs of text.
Note: I'm referring to the scrolling of the entire screen, not the movement of cursor.
------------------
1 abcdefg abcefg
abcasdfsa sdfsf
sdfc
2 adfadf
3 adfadf
4 adfadf
------------------

I don't think it is possible to do exactly what you want.
Here's the description I get when I type :help CTRL-E
CTRL-E
CTRL-E Scroll windows [count] lines downwards in the buffer.
Mnemonic: Extra lines.
Note that while it says it is scrolling the window, it also mentions that it is scrolling lines in the buffer. You really are scrolling only one (wrapped) line at a time.
I don't think there's another way around this.
I can get the bottom of the window to display partial lines by setting display=lastline
I think display=lastline is a false solution - you still scroll the same amount, you just have more visible.
The real way to solve this is to disable wrapping:
:set nowrap
Edit
Some related threads that show that a "scrolling via screen lines" feature is under consideration, but will take a while to be implemented:
http://vim.1045645.n5.nabble.com/Feature-request-Display-partial-paragraph-at-the-top-of-the-window-td1166809.html (4 years ago)
http://vim.1045645.n5.nabble.com/Scrolling-screen-lines-I-knew-it-s-impossible-td3358342.html (the beginning of this year. Same dev mentioned wanting to implement it: "Ben Schmidt")

For anyone reading this thread:
This thread on the Vi stackexchange offers a slightly hacky (but useable) solution to visual scrolling across wrapped lines.

Now there's a built-in option for this:
'smoothscroll' 'sms' boolean (default off)
local to window
Scrolling works with screen lines. When 'wrap' is set and the first
line in the window wraps part of it may not be visible, as if it is
above the window. "<<<" is displayed at the start of the first line,
highlighted with hl-NonText.
NOTE: only partly implemented, currently works with CTRL-E, CTRL-Y
and scrolling with the mouse.

Related

Vim: Only scroll the current line

I would like vim to scroll horizontally like nano does, by scrolling only the current line, not the whole screen.
I tried playing with nowrap and scrolloff settings, without success.
Here are some screenshots (with the cursor at the end of a long line) to explain myself.
Nano:
Vim (wrap):
Vim (nowrap):
Thanks!
No, Vim cannot do this, and I think it would be hard to implement this in a way that isn't inconsistent or confusing to the user. There would need to be an indicator (like with side scrolling) that only the current line is scrolled. Also in Vim, there are several commands (like j / k and i_CTRL-Y / i_CTRL-E) that refer to the same column in above / below lines. A partially scrolled view state would make it difficult to use those.
That said, you can sort-of achieve this with a hack: The foldtext of folded lines does not scroll horizontally. So if you fold each individual line (other than the current one) via a custom 'foldexpr', set the fold text to be the line's text, and automatically close all surrounding folds, you'll get this. However, as you'll lose syntax highlighting and "normal" folding, this is more for demonstration than an actual solution.

Highlight the line number in the middle of screen in Vim?

As it is very convenient to scroll half screen up and down(by using ctrl-d), but it is a little hard to estimate which line will disappear after scroll. It will be great if the middle line number can be highlighted, then I can use it as a marker for half screen scrolling.
These are standard VI commands in Command Mode. You may find them helpful:
z<Enter> - Put current line on top of your window
z. - Put current line in the middle of your window
z- - Put current line on the bottom of your window
See :help z for more information in VIM. There's a lot of stuff on cursor movements there.
That's not possible in Vim, and implementations of highlights while scrolling are notoriously difficult to implement well (leading to the introduction of the built-in 'cursorline' option to address the top use case).
I frankly don't see the need for that highlight, and I think that once you've learned all the various ways of moving (including commands like zt / zz, <C-Y> / <C-E> (which are great for repositioning when you're off a single / few lines only), and plain j / k with the 'relativenumber' option), this becomes a non-issue.

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.

In vim, how to scroll, continuously, by screen lines when wrapping is enabled?

I can get the bottom of the window to display partial lines by setting display=lastline
Is there a similar option that would allow partial lines to be displayed at the top of a window?
Without this functionality, my vim still scrolls (down) by more than 1 screen line when the topmost file line being displayed is wrapped into multiple screen lines.
For example, in the following scenario, when I press C-E, my vim will scroll down by 3 screen lines. Is there a way to make it scroll by only 1 screen line such that only the first two words in line 1 are hidden, but the following 3 words are still displayed? This will be very useful for editing long paragraphs of text.
Note: I'm referring to the scrolling of the entire screen, not the movement of cursor.
------------------
1 abcdefg abcefg
abcasdfsa sdfsf
sdfc
2 adfadf
3 adfadf
4 adfadf
------------------
I don't think it is possible to do exactly what you want.
Here's the description I get when I type :help CTRL-E
CTRL-E
CTRL-E Scroll windows [count] lines downwards in the buffer.
Mnemonic: Extra lines.
Note that while it says it is scrolling the window, it also mentions that it is scrolling lines in the buffer. You really are scrolling only one (wrapped) line at a time.
I don't think there's another way around this.
I can get the bottom of the window to display partial lines by setting display=lastline
I think display=lastline is a false solution - you still scroll the same amount, you just have more visible.
The real way to solve this is to disable wrapping:
:set nowrap
Edit
Some related threads that show that a "scrolling via screen lines" feature is under consideration, but will take a while to be implemented:
http://vim.1045645.n5.nabble.com/Feature-request-Display-partial-paragraph-at-the-top-of-the-window-td1166809.html (4 years ago)
http://vim.1045645.n5.nabble.com/Scrolling-screen-lines-I-knew-it-s-impossible-td3358342.html (the beginning of this year. Same dev mentioned wanting to implement it: "Ben Schmidt")
For anyone reading this thread:
This thread on the Vi stackexchange offers a slightly hacky (but useable) solution to visual scrolling across wrapped lines.
Now there's a built-in option for this:
'smoothscroll' 'sms' boolean (default off)
local to window
Scrolling works with screen lines. When 'wrap' is set and the first
line in the window wraps part of it may not be visible, as if it is
above the window. "<<<" is displayed at the start of the first line,
highlighted with hl-NonText.
NOTE: only partly implemented, currently works with CTRL-E, CTRL-Y
and scrolling with the mouse.

how to put the current line under edit on the top of the screen in vim

It is very inconvenient when the line I'm editing is at the bottom of the screen . Because you can't see any following lines and that prevent you from take any "eye refernce" to the content of the file following the current line .
so my question is there a vim command can refresh the display and put the current line under edit to on the top of the screen ? thanks in advance.
See this reference.
z<return>
:h scroll-cursor
Jonathan's answer is valid, however I prefer zt (as "top") that keeps the cursor where it was. In the same idea, there is zb (as "bottom") and zz (not as "middle", but that puts the current line at the center of the window).
There is also an option to make vim always show context around the current line:
:set scrolloff=5
(or, :se so=5) will show +/−5 lines around your current line. From vim's help about 'scrolloff':
Minimal number of screen lines to keep above and below the cursor. This will make some context visible around where you are working. If you set it to a very large value (999) the cursor line will always be in the middle of the window (except at the start or end of the file or when long lines wrap).

Resources