Vim: Why can't I map to the <j> key? - vim

I saw this key bind in tips.txt:
:map <C-U> <C-Y><C-Y><C-Y><C-Y><C-Y><C-Y><C-Y><C-Y><C-Y><C-Y><C-Y><C-Y>
Which results in smooth scrolling up for CTRL-U. It only moves the viewport though, and I'd like to also move the cursor, so for scrolling down I tried:
:map <C-D> <C-E><j><C-E><j><C-E><j><C-E><j><C-E><j><C-E><j><C-E><j><C-E><j>
Since CTRL-E moves the viewport down and j moved the cursor down. This doesn't work however. When I press CTRL-D the viewport just hops one line down and the terminal beeps.
How do I map CTRL+D to scroll and move the cursor?

You shouldn't have the angled brackets (</>) around j.

Related

iTerm2 cursor scrolling with mouse enabled in vim

iTerm2 has a very nice feature that allows, when scrolling the mouse, to move the cursor up and down.
However, if I enable the mouse in vim with :set mouse=a, the scrolling behavior changes: it now scrolls the file but does not move the cursor until the cursor gets out of view.
Is there a way to keep the iTerm2 scrolling along with mouse enabled in vim?
Does this help?
map <ScrollWheelDown> j
map <ScrollWheelUp> k
Have a look at :help scroll-mouse-wheel.

Don't center screen when hitting end in vim

With nowrap and when on a long line, hitting the end button takes the cursor to the end of the line (of course). Furthermore, the screen will be centered on the cursor.
It will take the cursor from here:
to here:
How can I make it scroll right only up to "line."? And thus have the last "this is a long line." aligned to the right of the window?
Edit: I already know about sidescroll and sidescrolloff but those only apply when using the arrow keys. It doesn't change behavior for the end key, and thus don't solve my problem.
if you check :h ze
ze Scroll the text horizontally to position the cursor
at the end (right side) of the screen. This only
works when 'wrap' is off. {not in Vi}
so if you create a mapping, map $ or <end> to $ze or <end>ze, it should work as you wish.
hope it helps.
EDIT
if you want to map like what you wanted in INSERT mode:
:set sidescroll=1
:inoremap <End> <Esc><End>zei

How do I press "<C-L> <C-W>l<C-W>_" in Vim?

In one of the vim config files I have noticed this keyboard mapping
map <C-L> <C-W>l<C-W>_ supposedly for easier moving in tabs and windows. What does that translate to keyboard presses? What is that underscore at the end for?
The command map <C-L> <C-W>l<C-W>_ maps Ctrl-L to Ctrl-W, l, Ctrl-W, _.
You invoke this binding by just pressing Ctrl-L. To invoke what it binds to you would type Ctrl-W, then l, followed by Ctrl-W again, and finally _ (which on a US keyboard is shift-hyphen). This is two separate bindings, <C-W>l moves the cursor to the window to the right, and <C-W>_ resizes current window to the maximum possible vertical size.
The Ctrl+wlCtrl+w_ keys sequence is somewhat too long so someone has created a shortcut ("mapping" in Vim-speak): Ctrl+L for it.
<C-w>l<C-w>_ moves the the cursor to the window on the right (<C-w>l) and maximizes it vertically (<C-w>_).
Mappings always follow the same structure:
map (or imap for insert mode mapping, nmap for normal mode mapping, etc.)
some whitespace
the shortcut you want, here <C-L>
some whitespace
the sequence of commands triggered by the shortcut
See :help windows for more info on window management and :help mapping for more info on mappings.

How to move screen without moving cursor in Vim?

I recently discovered Ctrl+E and Ctrl+Y shortcuts for Vim that respectively move the screen up and down with a one line step, without moving the cursor.
Do you know any command that leaves the cursor where it is but moves the screen so that the line which has the cursor becomes the first line? (having a command for the last line would be a nice bonus).
I can achieve this by manually pressing Ctrl+E (or Ctrl+Y) the proper number of times, but having a command that somehow does this directly would be nice.
Any ideas?
zz - move current line to the middle
of the screen
(Careful with zz, if you happen to have Caps Lock on accidentally, you will save and exit vim!)
zt - move current line
to the top of the screen
zb - move
current line to the bottom of the
screen
Additionally:
Ctrl-y Moves screen up one line
Ctrl-e Moves screen down one line
Ctrl-u Moves cursor & screen up ½ page
Ctrl-d Moves cursor & screen down ½ page
Ctrl-b Moves screen up one page, cursor to last line
Ctrl-f Moves screen down one page, cursor to first line
Ctrl-y and Ctrl-e only change the cursor position if it would be moved off screen.
Courtesy of www.lagmonster.org/docs/vi2.html
Vim requires the cursor to be in the current screen at all times, however, you could bookmark the current position scroll around and then return to where you were.
mg # This book marks the current position as g (this can be any letter)
<scroll around>
`g # return to g
I'm surprised no one is using the Scrolloff option which keeps the cursor in the middle of the page.
Try it with:
:set so=999
It's the first recommended method on the Vim wiki and works well.
I've used these shortcuts in the past (note: separate key strokes i.e. tap z, let go, tap the subsequent key):
z t ...or... z enter --> moves current line to top of screen
z z ...or... z . --> moves current line to center of screen
z b ...or... z - --> moves current line to bottom
If it's not obvious:
enter means the Return or Enter key.
. means the DOT or "full stop" key (.).
- means the HYPHEN key (-)
For what it's worth, z. avoids the danger of saving and closing Vi by accidentally typing ZZ if the caps-lock is on.
More info: :help scroll-cursor
Here's my solution in vimrc:
"keep cursor in the middle all the time :)
nnoremap k kzz
nnoremap j jzz
nnoremap p pzz
nnoremap P Pzz
nnoremap G Gzz
nnoremap x xzz
inoremap <ESC> <ESC>zz
nnoremap <ENTER> <ENTER>zz
inoremap <ENTER> <ENTER><ESC>zzi
nnoremap o o<ESC>zza
nnoremap O O<ESC>zza
nnoremap a a<ESC>zza
So that the cursor will stay in the middle of the screen, and the screen will move up or down.
To leave the cursor in the same column when you use Ctrl+D, Ctrl+F, Ctrl+B, Ctrl+U, G, H, M, L, gg
you should define the following option:
:set nostartofline
my mnemonic for scrolling...
Adding to other answers also pay attention to ze and zs, meaning: move screen to the left/right of the cursor (without moving the cursor)
+-------------------------------+
^ |
|c-e (keep cursor) |
|H(igh) zt (top) |
| ^ |
| ze | zs |
|M(iddle) zh/zH <--zz--> zl/zL |
| | |
| v |
|L(ow) zb (bottom) |
|c-y (keep cursor) |
v |
+-------------------------------+
also look at the position of h and l and t and b and (with qwertz keyboard) c-e and c-y (also the "y" somehow points to the bottom) on the keyboard to remember where the screen is moving.
Enter vim and type:
:help z
z is the vim command for redraw, so it will redraw the file relative to where you position the cursor. The options you have are as follows:
z+ - Redraws the file with the cursor at top of the window and at first non-blank character of your line.
z- - Redraws the file with the cursor at bottom of the window and at first non-blank character of your line.
z. - Redraws the file with the cursor at centre of the window and at first non-blank character of your line.
zt - Redraws file with the cursor at top of the window.
zb - Redraws file with the cursor at bottom of the window.
zz - Redraws file with the cursor at centre of the window.
You can prefix your cursor move commands with a number and that will repeat that command that many times
10Ctrl+E will do Ctrl+E 10 times instead of one.
zEnter does exactly what this question asks for.
It works where strangely zz would not work (vim 7.4.1689 on Ubuntu 2016.04 LTS with no special .vimrc)
You may find answers to "Scrolling Vim relative to cursor, custom mapping" useful.
You can use ScrollToPercent(0) from that question to do this.
Sometimes it is useful to scroll the text with the K and J keys, so I have this "scroll mode" function in my .vimrc (also bound to zs).
See scroll_mode.vim.
I wrote a plugin which enables me to navigate the file without moving the cursor position. It's based on folding the lines between your position and your target position and then jumping over the fold, or abort it and don't move at all.
It's also easy to fast-switch between the cursor on the first line, the last line and cursor in the middle by just clicking j, k or l when you are in the mode of the plugin.
I guess it would be a good fit here.

"Minimizing" vertical VIM window splits

I use horizontal and vertical window splits in religiously in VIM and up until recently, I enjoyed the comfort of two commands to effectively hide (or minimize) my horizontal splits. I set them up adding the following lines to my .vimrc file:
set winminheight=0
map <C-J> <C-W>j<C-W>_
map <C-K> <C-W>k<C-W>_
Hitting Control-j or Control-k navigates through horizontal splits by going up or down. What I'd like to accomplish is the same thing for vertical splits by showing or hiding the left or right split using Control-Shift-h and Control-Shift-l; h moving to the left, l moving to the right. I have tried the following with little to no success:
set winminwidth=0
map <S-C-L> 500<C-W>h<C-W>_
map <S-C-H> 500<C-W>l<C-W>_
The action would be similar to utilizing Control-w-< and Control-w->, only moving the vertical split completely to the left or write, not just one line at a time.
Any ideas on how to accomplish this? Thanks.
First up, you won't be able to use <S-C- (shift + control) in your code (see below). But you can use the 'mapleader' as your "shift" and then use the <C-h> and <C-l> like you want to. Like this:
set winminwidth=0
nmap <Leader><C-h> <C-W>h500<C-W>>
nmap <Leader><C-l> <C-W>l500<C-W>>
The common leader keys in vim are comma and back-slash:
:let mapleader = ","
But you'll find that this gets annoying to require 3 keystrokes for this, so you might as well just drop the control key stroke. This way (if your leader is comma) you can just press ",h" and ",l" to go to the splits to your left and right:
set winminwidth=0
nmap <Leader>h <C-W>h500<C-W>>
nmap <Leader>l <C-W>l500<C-W>>
" (FTW) :D
...
A guy named Tony Chapman answers why you can't use control + shift:
Vim maps its Ctrl+printable_key
combinations according to ASCII. This
means that "Ctrl+lowercase letter" is
the same as the corresponding
"Ctrl+uppercase letter" and that
Ctrl+<key> (where <key> is a printable
key) is only defined when <key> is in
the range 0x40-0x5F, a lowercase
letter, or a question mark. It also
means that Ctrl-[ is the same as Esc,
Ctrl-M is the same as Enter, Ctrl-I is
the same as Tab.
So yes, Ctrl-s and Ctrl-S (i.e. Ctrl-s
and Ctrl-Shift-s) are the same to
Vim. This is by design and is not
going to change.
Try
set winminwidth=0
map <S-C-L> <C-W>h<C-W>|
map <S-C-H> <C-W>l<C-W>|
This doesn't move a window completely to the left or right (that's <C-W>H and <C-W>L), it just moves the cursor to the left (or right) window and maximizes that window horizontally.
See :help CTRL-W_bar for more.
Crl-w 1 |
will minimize current window in Vim.

Resources