Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CTRL-K in Vim produces unexpected results

Tags:

vim

Hi I'm trying to optimise my window management in vim by mapping ctrlk to ctrl+w, k so i can just press ctrl+k to switch to the split window above the one I'm working in (I'm doing this for h,j and l also but it's only k that's causing the problem).

I've added this into my .vimrc

noremap <silent> <c-k> <C-W>k                                                                                                                
noremap <silent> <c-j> <C-W>j                                                                                                      
noremap <silent> <c-h> <C-W>h                                                                                                      
noremap <silent> <c-l> <C-W>l

However if I press ctrl+k, then something weird happens. It changes depending on where I am in the document.

  • If I'm at the top of a document with many lines beneath my curser, the cursor hops down a few lines and columns into a completely different place.
  • If I'm at the bottom of a document, it creates loads of spaces from the cursor onwards.

I've tested and removing the above lines causes the symptoms to stop happening. I'm just really confused as to what is going on!

Some info: I'm using the vim binary that comes with macvim via the command line.

Any help would be greatly appreciated!

Thanks!

like image 469
alexganose Avatar asked Jan 04 '12 14:01

alexganose


People also ask

What does CTRL w do in Vim?

Ctrl-w = tells Vim to assign an equal number of lines to each viewport. To move between the viewports while working, use Ctrl-w j to move down, and Ctrl-w k to move up. This should prove easy to remember — Ctrl-w for “window” commands, and the normal vi movement commands j for down and k for up.

What does Silent do in Vim?

Sometimes when you execute a command call in Vim, it gets echoed. Adding <silent> removes the echo. It does not remove error message though.

What is CTRL L in Vim?

<ctrl>l. Redraw screen. <ctrl>g. Display current line number and. file information.

What does CTRL F do in Vim?

To scroll forward (move down) one screenful, press Ctrl-F. (Hold down the Control key and press the F key.) The cursor moves to the upper left corner of the new screen.

How do you Ctrl Z in Vim?

on linux, CTRL-Z in vi/vim/gvim mean escape to the console, or put this in the background. you then do whatever you want on the console and type fg (foreground) to bring you back into vim edit session.

What does capital B do in Vim?

Like most of the capitalized movement pairs, b moves by word, but B moves by WORD. The difference is that vim considers a "word" to be letters, numbers, and underscores (and you can configure this with the iskeyword setting), but a "WORD" is always anything that isn't whitespace.


2 Answers

I can’t explain the second problem, but if you pasted everything directly from the vimrc then you have lots of trailing spaces that must not be there. It can explain the first problem. Try running

:%sm/\s\+$

then save and see whether problem disappears. If it is so, use

:set list listchars=trail:-

to be able to see trailing spaces so that you won’t run into this problem again.

like image 162
ZyX Avatar answered Nov 09 '22 05:11

ZyX


Maybe <C-k> is already mapped to something else. Try :verbose map <C-k>.

like image 2
romainl Avatar answered Nov 09 '22 06:11

romainl