Set cursor anywhere in window in Vim - vim

How to setup Vim so you can f.e mouse click in the middle of new opened empty Vim window and place cursor exactly there ( f.e line 23 char 49), instead of line 1 char 1 because?
Is there better way than filling lines with spaces?

The cursor must be somewhere in a buffer and the screen lines that start with ~ are not part of the buffer so placing the cursor there is impossible, no matter how you set :help 'mouse' or :help 'virtualedit'.
You won't be able to achieve your goal without first populating the new buffer with empty lines.

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 :)

How to get VIM to map dd to c-bs

I've tried about 100 combinations of :inoremap and :imap, etc, but I am unable to find one that will allow me to remap to delete the current line and leave the cursor on the line that moves up, i.e. visually, the cursor does not move.
I'd prefer to have this work in input, replace and normal modes. Best if it covered all/most of the modes.
Can someone tell me how to do this?
you need to set ve=all to make sure the cursor staying on the same line, same col after deleting. Because the line below the deleted one could be shorter than the line you want to delete, E.g
foooooooo[I]oooo
bar
[I] is cursor, here if you press dd, the cursor won't go to the same column, because the bar line is shorter than fooo.. line. If you set ve=all, your cursor could be placed to any area in the buffer.
then you can just save the col before dd and restore it after dd. like:
:let c=col('.') |exec 'norm! dd'.c.'|'
without set ve=all, if your cursor column didn't exceed the length of the line below, the above command works as well. but if it exceeded, the cursor would be at the end of "below" line.
I hope I understood you right.
See the accepted answer in this question for an explanation of why I can't remap Ctrl-BackSpace.

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.

how to put cursor at the beginning of `tab`

When I open vim help, I press 0 to goto beginning of line;
When I press j to scroll down, the cursor jumps to right 8 spaces when focusing tab
And the cursor jumps back when focusing non-tab
It's not very good for eyes.
Is there an option to let vim focus the beginning of tab to let cursor stay at column 0?
Another option:
http://blog.killtheradio.net/how-tos/vim-cursor-at-beginning-of-tab-in-normal-mode/
set list lcs=tab:\ \
" Note the extra space after the second \
You can achieve this by setting
set virtualedit=all
This lets you position the cursor anywhere in the buffer, even beyond the end of the line and before a tab character. In help virtualedit there is a warning that it might break some scripts or plugins, so use it carefully!

Is there something in VIM that behaves like ^E + j?

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

Resources