If we jump, using YouCompleteMe, in vim to a line that is already inside current view, it will put cursor to that line, and make that line be in center by scroll window.
for example, current window shows line 100 to line 200, and cursor is on line 160. Now we jump to line 110, which can be seen in current window view. After jumping, line 110 is in the center and window shows line 60 to 160.
I expect that any jump(including come back by ) won't scroll window if target line is inside current view, just put cursor to target place. How can I do this?
PS. I'm actually using YCM for jumping.
After you go to the target line, try pressing zz.
I think this is what you are looking for. There are zt, zb, do :h zt to check out what those commands do.
You can create something else, e.g. I have:
nnoremap zl zt4<c-y>¬
Related
While going back and forward on history of undo/redo, I would like to keep vim screen vertically locked (let's say lock on line 100), is that possible? how?
I mean that line should be fixed on screen.
Possible, but requires work. Something like...
Make a mapping that will set (and possibly also reset) the lock. Something like <leader>l to set the b:lock_line variable to the current line. Then use nnoremap to change u, Ctrl-R to execute b:lock_line . "zz" after it does the original binding, if b:lock_line is set.
You can't change the operation of built-in commands like :earlier, :later etc., as Vim does not allow you to redefine them. You can define new commands like :Earlier that would do what you want instead.
No. It is not possible. At anytime, if you want to see the line no. 100, you can type :100 in the command mode or 100G to go to the line.
Once you are in line 100, you can type zz, zt, zb to move the cursor to middle of screen, top of screen, bottom of screen respectively.
I am looking for a way to allow my cursor to move past the end of the file (without adding a whole lot of blank lines in need to delete later) so that when I'm writing a long file I am not always stuck looking at the very bottom edge of my monitor.
What would be perfect is the same behavior I get from visualedit=all except for newlines after the file ends instead of spaces after the line ends.
I am not aware of any possibility to move the cursor past the end of the buffer.
However, if the question is just about adjusting the focus of the viewport, good options are:
CTRL-E and CTRL-Y to scroll the window
zz to center the cursor line
zt to put the cursor at the top of the window
More in the 'Scrolling' quickref at :h Q_sc.
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.
Is there a key-combination that behaves as though I'd press ctrl-E followed by a j, that is the text scrolls up a line but the cursor keeps where it is, relativ to the screen.
I am aware that I could achieve what I want with a :map but before I do I thought I'd rather want to know if there is already some "built-in" functionality
Yes, use CTRL-D with a count of 1 (not that that saves you anything, really).
The CTRL-D command does the same as CTRL-E, but also moves the cursor down the same number of lines
There is the z command
z. Redraw, line [count] at center of window (default
cursor line). Put cursor at first non-blank in the
line.
zz Like "z.", but leave the cursor in the same column.
Careful: If caps-lock is on, this commands becomes
"ZZ": write buffer and exit! {not in Vi}
These mappings makes it possible to scroll up and down one line with focus on center line (hard to describe so that it sound correct, try it instead)
"scroll with line in center
map <C-Up> <ESC>0kzz
map <C-Down> <ESC>0jzz
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).