Gvim Tip to move by n lines? - vim

Is there anyway I can move up/down in Gvim by n lines ?

nj or nk where n is the number of lines you want to move. You can also use :n to jump to line number n as well.
eg. to move 10 lines down you would type 10j. To move to line 10 you would use :10 and hit enter.

:help scrolling will tell you everything you're asking, like :
CTRL-E Scroll window [count] lines downwards in the buffer.
Mnemonic: Extra lines.
and
CTRL-Y Scroll window [count] lines upwards in the buffer.
with [count] you can type nCTRL-E for n-lines :)
There are many other possibilities that :help will give to you.

Ctrl-E / Ctrl-Y are the scroll only keys (where the cursor stays on the same line/col).
However, for page-down/up line behaviour use Ctrl-U / Ctrl-D
By default the number of lines comes from the 'scroll' option. You can use the 'scrolloff' option to always keep n additional lines in view above/below the cursor line.

Related

What vimrc setting controls number of scroll lines

When using Ctrl-E and Ctrl-Y in vim, I would like it to scroll multiple lines instead of one at a time. How would I set up my vimrc to specify the number of lines for just these 2 commands?
You can override the default bindings with counted versions.
noremap <C-e> 2<C-e>
noremap <C-y> 2<C-y>
Use noremap to avoid recursion.
You don't need to do any particular setup. Instead, you can just scroll one page at a time using CTRL+F (forward) or CTRL+B (backwards) as an alternative solution.
This is not exactly what you want, but I hope it helps.
Regarding Ctrl+E and Ctrl+Y, I'm afraid that the only way may be to precede the command with a line count. So, if you want to scroll down five lines, you press 5, then Ctrl+E.
But you can, by default, use Ctrl+D to scroll half a page downwards, and Ctrl+U to scroll half a page upwards, and you can also set how many lines you want to go up or down with the scroll variable.
So, in your .vimrc, add the line set scroll=N, where N is the number of lines you want to scroll with the Ctrl+D and Ctrl+U commands.

Vim go to previous cursor position when switching to a buffer

I switch from buffer 1 to buffer 2 and my cursor is at line 100. I use j/k to scroll around and land at line 120. Then I use ctrl-O to switch back to buffer 1, and then ctrl-I to go to buffer 2.
I expect that the cursor should be at line 120, as that is where I left off, but to my annoyance, the cursor is at line 100. How can I get vim to go to line 120?
The commands you are using are working as advertised. The problem is that you don't use the right commands for what you are trying to do.
<C-o> and <C-i> are not related to buffers at all. Their job is to let you move up and down the jump list, no more no less. The fact that they lead you to another buffer is a side effect. Don't rely on side effects: if you don't want to navigate the jump list, don't use them.
If you need to alternate between two buffers, the right command is <C-^> (or <C-6> on some keyboards).
If you need to switch to a specific buffer, use :b <tab> or :ls followed by :bn with n being the number of the desired buffer.

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.

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

Vim: Move cursor to its last position

Is it possible in (g)Vim to move the cursor to its previous position (while in normal mode)? Something to cycle back and forth in the list of previous cursor positions would be ideal. But also just to switch to the last location would suffice (something like cd - in bash with directories).
Here's a little demonstration:
line |1| <- cursor position
line 2
line 3
line 4
And suppose I did 2j, here's how it is now:
line 1
line 2
line |3| <- cursor position
line 4
Now I'd like to press something (other than 2k obviously) to move back to the first position and possibly to previous positions.
The quickest way is to hit either:
''
(two apostrophes) or:
``
(two backticks). Note that the difference is that the backtick goes to the same location on the line, whereas the apostrophe goes to the start of the line. On a UK keyboard, the apostrophe is more accessible, so I tend to use that one. There are loads of useful marks like this, see :help mark-motions.
For some other motions (not 2j I think), there's also the jump-list that lets you navigate back and forth among a number of motions. CtrlO and CtrlI do this navigation, but see :help jump-motions for more information.
You can also use g; and g, to move back- and forward in the list of your previous edit locations.
On Non-US Keyboards
On my Swiss and German keyboard layouts, typing ; inconveniently requires using the Shift key. Hence, I defined g- as a more convenient alias for g; in $MYVIMRC:
" Map g- as an alias for g;
nnoremap g- g;
Why no one figured out the problem with DrAl's answer?
The '' or `` will not solve the original problem of this post!
These two command will not work for some cursor movement like 2j, at least for me. It will make newbie to vim more confused.
The behavior of '' or ``, and CtrlI or CtrlO are based on jump list. The 2j will not save the position changes into the jump list so these command will not work for 2j.
'' or `` switch between the last position and the current position.
CtrlI and CtrlO work through the jump list history.
g; and g, move through edit positions, which are also very frequently used.
Right from the help (:help jump):
:ju[mps] Print the jump list (not a motion command). {not in
Vi} {not available without the |+jumplist| feature}
*jumplist*
Jumps are remembered in a jump list. With the CTRL-O and CTRL-I command you
can go to cursor positions before older jumps, and back again. Thus you can
move up and down the list. There is a separate jump list for each window.
The maximum number of entries is fixed at 100.
{not available without the |+jumplist| feature}

Resources