291

I have split my windows horizontally. Now how can I return to normal mode, i.e. no split window just one window without cancelling all of my open windows. I have 5 and do not want to "quit", just want to get out of split window.

eckes
  • 64,417
  • 29
  • 168
  • 201
Sudeep
  • 3,015
  • 3
  • 17
  • 8

11 Answers11

383

Press Control+w, then hit q to close each window at a time.

Update: Also consider eckes answer which may be more useful to you, involving :on (read below) if you don't want to do it one window at a time.

Warren P
  • 65,725
  • 40
  • 181
  • 316
  • If you have 5 splitq opened, I am not sure that your solution would really help. – Xavier T. Jan 27 '11 at 09:03
  • 9
    I agree but the OP didn't pick eckes answer, and I can't change it myself. – Warren P Feb 25 '13 at 18:09
  • 4
    I am pretty sure that this is the correct answer. This did it for me and if you look at op's question, he did not want to close his other open windows, just the recently split one. – Zak Jul 08 '14 at 22:34
  • 1
    Does not help if the shown buffer contains unsaved changes. It says I should override with ! but I don't know how. As a newbie I'm lost now. – musiKk Aug 06 '15 at 17:48
  • 2
    so save it. You don't need this answer if you don't know how to save. It is possible you don't even have PERMISSION to save. – Warren P Aug 07 '15 at 20:03
  • 1
    Coming from traditional GUI editors, this is a pretty weird concept. Setting `hidden` seems to help though. Though, what I meant was, how do you add a ! to a shortcut? Is it "C-w !q", "C-w q!", "!C-w q", "C-! w q"? I think none of those but I already forgot how to create new buffers so this is an exercise for the reader. – musiKk Aug 20 '15 at 16:51
  • 1
    I believe the ! goes before the q, so !q – Warren P Aug 20 '15 at 21:05
  • 3
    If the buffer contains unsaved changes, you can quit from it with `:q!` . Meaning, "I want to quit this one! Really!" – Mike S Oct 07 '15 at 20:07
  • It is not working in vim for mac, refer @Benoit answer below – Sm Srikanth Nov 23 '18 at 06:31
  • 1
    I had to do `Control` + `w` followed by a `:q`. Wanted to edit your A'er but figured something like this wouldn't have gone unnoticed for this long. Someone else posted an A'er that you had said was a dup of yours, but yours is missing this minor detail as well. – slm Jan 29 '19 at 14:20
  • OP, you need a `:` before the `q`. It should be `ctrl + w` then `:q`. Just `q` activates/stops key recording. Open vim and type `:help q ` to see the official help for `q`. – Hawkeye Parker Nov 02 '21 at 18:38
316

To close all splits, I usually place the cursor in the window that shall be the on-ly visible one and then do :on which makes the current window the on-ly visible window. Nice mnemonic to remember.


Edit: :help :on showed me that these commands are the same:

  • :on
  • :only
  • CTRL-w CTRL-o
  • And yes, also CTRL-W o has the same effect (as Nathan answered).

Each of these four closes all windows except the active one.

Community
  • 1
  • 1
eckes
  • 64,417
  • 29
  • 168
  • 201
  • 8
    +1 If the other window contains any unintended changes, `:on` will respond with `E445: Other window contains changes` and will not close the other window. For example, I usually create a new window, and read into it a file that I'd like to yank from. In this case, Vim considers the other window to have unwritten changes. In such a case, use `:on!` to force a close. – Sabuncu Jun 16 '13 at 19:44
68

to close all windows but the current one use:

CTRL+w, o

That is, first CTRL+w and then o.

Nathan Fellman
  • 122,701
  • 101
  • 260
  • 319
  • 3
    I also wonder if there is a way to undo a `CTRL+W o` typed accidentally. – Paolo Jun 01 '16 at 15:28
  • No, there is no way to undo, unless you took actions to allow for undo. See https://vi.stackexchange.com/questions/241/undo-only-window/537#537 – Rob Bednark Oct 11 '20 at 00:35
39

Two alternatives for closing the current window are ZZ and ZQ, which will, respectively, save and not save changes to the displayed buffer.

jfmercer
  • 3,641
  • 3
  • 29
  • 35
Benoit
  • 76,634
  • 23
  • 210
  • 236
17

From :help opening-window (search for "Closing a window" - /Closing a window)

  • :q[uit] close the current window and buffer. If it is the last window it will also exit vim
  • :bd[elete] unload the current buffer and close the current window
  • :qa[all] or :quita[ll] will close all buffers and windows and exit vim (:qa! to force without saving changes)
  • :clo[se] close the current window but keep the buffer open. If there is only one window this command fails
  • :hid[e] hide the buffer in the current window (Read more at :help hidden)
  • :on[ly] close all other windows but leave all buffers open
chaptuck
  • 289
  • 3
  • 8
8

The command :hide will hide the currently focused window. I think this is the functionality you are looking for.

In order to navigate between windows type Ctrl+w followed by a navigation key (h,j,k,l, or arrow keys)

For more information run :help window and :help hide in vim.

cledoux
  • 4,717
  • 1
  • 22
  • 30
  • This is great. Chrome (with the ssh plugin) traps as "close tab" effectively disabling all vim's window commands. Having an alternative is useful. – rein May 13 '13 at 22:01
7

I found that ctrl + w to the window you want to close, then just do :q. This works for me.

slm
  • 15,396
  • 12
  • 109
  • 124
dmarges
  • 361
  • 2
  • 7
  • This is a duplicate answer really, duplicate of something that's been sitting there for 3+ years. – Warren P Sep 26 '16 at 12:58
  • @WarrenP does not look like a dup. I did not find any other answer which mentions `^W^W:q` as a possible solution so get rid of a split. Note that `^W to the window you want to close` apparently implies multiple uses of `^W^W` to switch to the window you want to close .. – Tino Apr 09 '20 at 12:41
  • My bad. The other is ^W then o – Warren P Apr 17 '20 at 23:16
2

I understand you intention well, I use buffers exclusively too, and occasionally do split if needed.

below is excerpt of my .vimrc

" disable macro, since not used in 90+% use cases
map q <Nop>
" q,  close/hide current window, or quit vim if no other window
nnoremap q :if winnr('$') > 1 \|hide\|else\|silent! exec 'q'\|endif<CR>
" qo, close all other window    -- 'o' stands for 'only'
nnoremap qo :only<CR>
set hidden
set timeout
set timeoutlen=200   " let vim wait less for your typing!

Which fits my workflow quite well

If q was pressed

  • hide current window if multiple window open, else try to quit vim.

if qo was pressed,

  • close all other window, no effect if only one window.

Of course, you can wrap that messy part into a function, eg

func! Hide_cur_window_or_quit_vim()
    if winnr('$') > 1
        hide
    else
        silent! exec 'q'
    endif
endfunc
nnoremap q :call Hide_cur_window_or_quit_vim()<CR>

Sidenote: I remap q, since I do not use macro for editing, instead use :s, :g, :v, and external text processing command if needed, eg, :'{,'}!awk 'some_programm', or use :norm! normal-command-here.

qeatzy
  • 1,363
  • 14
  • 21
2

Just like the others said before the way to do this is to press ctrl+w and then o. This will "maximize" the current window, while closing the others. If you'd like to be able to "unmaximize" it, there's a plugin called ZoomWin for that. Otherwise you'd have to recreate the window setup from scratch.

psyho
  • 7,142
  • 5
  • 23
  • 24
1

Provide the window number to close a specific window without leaving the current one:

:[N]close
:close[N]
:[N]quit
:quit[N]
Ctrl-W[N]c

The window number can be displayed in the status line by the following settings:

:set statusline+=%{tabpagewinnr(tabpagenr())}
Konstantin Glukhov
  • 1,898
  • 3
  • 18
  • 25
0

Okay I just detached and reattach to the screen session and I am back to normal screen I wanted

Sudeep
  • 3,015
  • 3
  • 17
  • 8
  • This does not leave `vim`'s split window mode, this just leaves `screen`'s split pane mode - which can be easily mistaken, so perhaps others do, too. BTW I find this screen feature annoying, hence I now default to `tmux` which allows a reattach with all splits intact. – Tino Apr 09 '20 at 12:48