131

I'm using both Vim and MacVim (or well, trying to). I installed the Tagbar plugin but find it impossible to switch to the actual Tagbar window to make my selection.

I've been reading loads about window switching in VIM, found a lot of default keybindings (CMD+`, CTRL+W+ or ), tried setting my own. NONE worked.

I always use VIM for quick file edits and got intrigued by using it as my full-time IDE. After spending a full hour just trying to figure out how to switch a window I'm totally turned off by the idea though. Still, I feel I have to know. How do you switch the window? Am I being totally blind here?

1
  • 10
    Vim is one of those tools where everything makes perfect sense - after you know about it. Before then it may as well be in welsh.
    – Phoshi
    Commented May 7, 2011 at 19:13

4 Answers 4

192

Control+W followed by W to toggle between open windows and,

Control+W followed by H/J/K/L to move to the left/bottom/top/right window accordingly,

Control+W followed by Left/Down/Up/Right arrow to move to the left/bottom/top/right window accordingly.

7
  • 4
    Nope, not doing anything.
    – Naatan
    Commented May 7, 2011 at 18:14
  • 7
    Ugh, I was pressing them at the same time. Tried Ctrl+W, THEN (seperately) one of the last keys and it worked. Sigh. Thanks!
    – Naatan
    Commented May 7, 2011 at 18:15
  • 7
    Besides the H/J/K/L directional keys you can also hit W again (with or without Control) to switch back and forth between the previously focused window.
    – Caleb
    Commented May 7, 2011 at 18:33
  • 9
    The arrow keys can also be used besides h/j/k/l. Commented Jan 4, 2014 at 9:28
  • 7
    You can also add prefix the direction with a number if you need to go more than one window; e.g. C-w 2k
    – Walf
    Commented Aug 1, 2016 at 6:12
30

Control + W twice does the trick for me on Macbook Pro.

5
  • 3
    It does also work on Linux.
    – tomrozb
    Commented Mar 4, 2014 at 12:44
  • This is the only thing that's worked for me on OSX. None of those <C-w><h/j/k/l> or <C-w><C-h/j/k/l> things the internet says does anything. Too bad <C-w><C-w> only cycles through the windows rather than going directly... Commented Sep 8, 2016 at 4:59
  • 4
    Control + W sucks when running a terminal in a browser Commented Sep 18, 2018 at 13:39
  • Running a terminal in a browser... interesting. Btw. if it sucks, you can remap it to a different keys.
    – user254319
    Commented Sep 11, 2019 at 7:40
  • Surprisingly it worked on vim in termux(android terminal emulator.). Commented Sep 7, 2020 at 22:17
17

It's possible to remap these to a more convenient keys (i.e. instead of Control+W followed by h/j/k/l)

Add this to your .vimrc

nnoremap <C-H> <C-W>h
nnoremap <C-J> <C-W>j
nnoremap <C-K> <C-W>k
nnoremap <C-L> <C-W>l

After that, Control + h/j/k/l will switch window focus.

3
  • This is nice. I placed it into my vimrc.
    – Enis Arik
    Commented Jun 28, 2021 at 14:40
  • 1
    This is clutch for those running a terminal in a browser (as mentioned in another comment) since Ctrl+W on Windows is "close window". Oof.
    – Excel Help
    Commented Jun 22, 2022 at 12:07
  • ❤️ that one! Thanks! Commented Aug 20, 2022 at 0:07
0

These mappings allow cycling through the windows. Whereas something like <C-W>l stops at the most right window <C-w>w moves the focus to the the first window.

In your .vimrc:

nnoremap <C-h> <C-w>W
nnoremap <C-l> <C-w>w
1
  • 1
    As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Feb 18, 2023 at 20:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .