This Vim9 script plugin enables you to swap or clone Vim windows easily. As a Vim9 script plugin, it works with Vim exclusively, and requires Vim version 9 (though from 8.2.4883 should work too).
Vim’s native window CTRL-W commands, like CTRL-W r, CTRL-W x,
and CTRL-W T, work well in many scenarios.
However, there are limitations. For example, CTRL-W r, “only works
within the row or column of windows that the current window is in”.
Also, there is no ability to move windows to another existing tab.
vim9-winswap lets you:
-
Swap a chosen window with any other, including a window in another existing tab, or
-
Put (clone) a chosen window into any other window, leaving the “yanked” window as-is, or
-
Use
:Winswapto swap two chosen windows (within the same tab).
In all instances:
-
Window-local options are carried across to the moved or cloned window. That is, options like
list,colorcolumn, andspell, which are exclusively window-local. -
The state of the marked/“yanked” window is cleared following a swap or put.
|
ℹ️
|
The plugin vim-windowswap
is a legacy vimscript plugin, which provides some similar functionality for
swapping windows. Its “swap” key mapping, <Leader>ww, has
been used in this plugin’s optional mappings. There are
improvements in vim9-winswap, too; e.g., ensuring the window-local
settings are carried across.
|
-
Navigate to the first window you want to swap.
-
<Leader>wwto mark the first window and remember its settings. -
Navigate to the second window you want to swap with the first.
-
<Leader>wwto remember the second window’s settings and swap the windows.
|
ℹ️
|
<Leader>ww is not fixed - you can choose whatever you want - but it is
enabled if you used g:winswap_opt_maps = true in your (Vim9) .vimrc/_vimrc.
|
As shown in Demo 2:
-
Navigate to the window you want to put.
-
<Leader>wwto mark the window and remember its settings. -
Navigate to the window you want to substitute.
-
<Leader>wpto substitute the window with the cloned window from point 1.
|
ℹ️
|
<Leader>wp is not fixed - you can choose whatever you want - but it is
enabled if you used g:winswap_opt_maps = true in your (Vim9) .vimrc/_vimrc.
|
As shown in Demo 3, if you know the window numbers of the two windows
you want to swap, you can use the :Winswap command:
:Winswap 4 2This swaps windows 4 and 2. The command accepts any two valid window numbers (though only within the same tab).
|
ℹ️
|
For convenience, there is command abbreviation Ws, which expands
to Winswap.
|
If you are satisfied with using <Leader>ww and <Leader>wp, as explained
in How to swap windows
and How to put (clone) a window, all you need
to do is include the following in your .vimrc/_vimrc:
g:winswap_opt_maps = trueThere are only two inherent mappings, both of which only work in Normal mode.
They are <Plug> mappings, so they should not conflict with anything:
-
<Plug>(Winswap_ww)If there is no currently marked window, this marks it as the window that will either be swapped or put/cloned. If there already is a currently marked window, it swaps the two windows.
-
<Plug>(Winswap_wp)Provided there is a currently marked window, this will put it (clone it to) where the current window is.
If you want to map either or both of the <Plug> mappings to keys of your
own choosing, it is easy. For example:
nnoremap <C-W>2 <Plug>(Winswap_ww)
nnoremap <C-W>1 <Plug>(Winswap_wp)Now <C-W>2 will do the same as what <Leader>ww would do and <C-W>1 will
do the same as what <Leader>wp would do.
|
🔥
|
If you use sessions, mappings including <ScriptCmd>, which is what
vim9-winswap uses, will be broken when a session is restored. This is a
known issue, which, at the time of
writing may be fixed in future. However, to avoid the issue, an autocmd and
function
may be added to your .vimrc/_vimrc.
|
From your terminal:
git clone https://github.com/kennypete/vim9-winswap ~/.vim/pack/plugins/start/vim9-winswapAdjust this as necessary for Windows and/or for plugins, which can be whatever
you want. Also, if you want manual control of plugin loading, use opt instead
of start, then include packadd vim9-winswap in your .vimrc/_vimrc.
Copyright © 2025 Peter Kenny
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.



