VIM: Stay away from the edges on searches - search

I recently saw some VIM configuration where search matches would scroll to N lines past the match, so that there would be N lines below the search match instead of it being on the last line (to give context). I cannot find the page where this was mentioned, and apparently I do not know the right keywords to google for!
What is this feature called, and how could I have used the VIM manual to find it assuming that I don't know what it is called?
Thanks.

As far as I tell this can't be set just for search, but is a setting that will be used in all situations.
You are looking for scrolloff: Minimal number of screen lines to keep above and below the cursor. This will make some context visible around where you are working. sidescrolloff does the same thing but horizontally.
This is what I have in my .vimrc:
" When the page starts to scroll, keep the cursor 8 lines from the top and 8
" lines from the bottom and 15 lines on the left
set scrolloff=8
set sidescrolloff=15
set sidescroll=1

Related

Vim spell check automatically splits the screen

I typed z= underneath a misspelled word, and vim split the screen horizontally, thereby keeping the misspelled word in context, but also providing a list words to change the misspelled word from. Usually, this latter screen replaces the former screen when I hit z=.
I like this behavior, but can not replicate it. I must have hit something before z= but I do not know what.
The behavior you saw accidentally happens when there are only a few suggestions and they don't fill the entire window.
You can force a maximum size for the suggestion list (example: 20 suggestions) with
set spellsuggest=best,20
Now, as long as your window exceeds 20 lines, you will see the misspelled word in context, and the bottom 20 lines of your window filled with the suggestion list
I can't say what caused the behavior you saw, maybe it is some plugin.
But here are two options to stay in the context with spellchecker:
1) Use CTRL-X s in insert mode:
In Insert mode, when the cursor is after a badly spelled word, you can use
CTRL-X s to find suggestions. This works like Insert mode completion. Use
CTRL-N to use the next suggestion, CTRL-P to go back. |i_CTRL-X_s|
2) Use vimple plugin which turns few full-screen windows (including spell suggestions) into "overlays" (actually split windows where you can select the word you need).

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.

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

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.

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.

Stay in same column while navigating up and down in a vim editor

I have set nowrap so that I can view a file properly. So what happens is that when navigating up or down in the file, if there is an empty line in between, vim takes me to the beginning of that line. I lose the view that I had previously, and I get it back only after I move a few lines up where there is text in them. This is rather irritating as while reading this particular file I am usually horizontally scrolled to a position much beyond the starting few columns that fit on the screen. Navigating to an empty line causes this view to be lost and I get completely empty view (because nothing is written in those columns in those rows). Is there any work around for this ?
You need to set the virtualedit option:
set virtualedit=all
See :help virtualedit for an explanation.

Resources