Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to swap files between windows in VIM? [closed]

Tags:

vim

editing

When I work with VIM, I always have multiple windows visible. Sometimes I would like to have an easy way, to swap those windows in places. Is there any Plugin, Macro, etc to make this more easy? BTW, I use MiniBufExplorer.

like image 446
mdrozdziel Avatar asked Oct 11 '22 11:10

mdrozdziel


People also ask

How do I switch between windows in Vim?

To move from the current Vim window to the next one, type CTRL-W j (or CTRL-W <down> or CTRL-W CTRL-J). The CTRL-W is the mnemonic for “window” command, and the j is analogous to Vim's j command, which moves the cursor to the next line.

How do I use multiple windows in Vim?

To split the vim screen horizontally, or open a new workspace at the bottom of the active selection, press Ctrl + w , followed by the letter 's' . In the example below, the left section has been split into two workspaces. To navigate to the bottom section hit Ctrl + w , followed by the letter 'j' .

What is SWP file in Vim?

An SWP file is a swap file created by the Vi text editor or one of its variants, such as Vim (Vi iMproved) and gVim. It stores the recovery version of a file being edited in the program. SWP files also serve as lock files, so no other Vi editing session can concurrently write to the currently-open file.

How do I go to a previous file in Vim?

CTRL-^ Edit the alternate file. Mostly the alternate file is the previously edited file. This is a quick way to toggle between two files. It is equivalent to ":e #", except that it also works when there is no file name.


2 Answers

There are a few useful commands built in which give you a certain amount of control, but it's not comprehensive. The main ones are:

  • Ctrl-W, r (i.e. hold CTRL, press W, release CTRL, press r) - which rotates the windows (The first window becomes the second one, the second one becomes the third one, etc.)

  • Ctrl-W, x - swap the current window with the next one

  • Ctrl-W, Shift-H - move this window to the far left

  • Ctrl-W, Shift-K - move this window to the top

(and similarly for Ctrl-W, Shift-J and Ctrl-W, Shift-L). See:

:help window-moving

for more information.

like image 335
DrAl Avatar answered Oct 12 '22 23:10

DrAl


I wrote and have been using the following code snippet in my vimrc for copy-pasting my Vim windows.

This defines for example the following shortcuts:

  • <c-w>y: "Yanks the window", i.e. stores the number of the buffer in the current window in a global variable.
  • <c-w>pp: "Puts the window in Place of the current window", i.e. it reads the buffer number stored previously and opens that buffer in the current window. It also stores the number of the buffer that used to be in the current window.

If by "swapping those windows in places", you mean "opening the buffer in window A in window B, and vice versa, without changing the position of the windows", you can use the following keyboard sequence to swap the windows:

  1. Select window A (either with mouse or with keyboard commands)
  2. Press <c-w>y (yanking the buffer number)
  3. Select window B
  4. Press <c-w>pp (pasting the buffer)
  5. Select window A
  6. Press <c-w>pp (pasting the buffer again)

It works only in Vim >= 7.0.

if version >= 700
function! HOpen(dir,what_to_open)

    let [type,name] = a:what_to_open

    if a:dir=='left' || a:dir=='right'
        vsplit
    elseif a:dir=='up' || a:dir=='down'
        split
    end

    if a:dir=='down' || a:dir=='right'
        exec "normal! \<c-w>\<c-w>"
    end

    if type=='buffer'
        exec 'buffer '.name
    else
        exec 'edit '.name
    end
endfunction

function! HYankWindow()
    let g:window = winnr()
    let g:buffer = bufnr('%')
    let g:bufhidden = &bufhidden
endfunction

function! HDeleteWindow()
    call HYankWindow()
    set bufhidden=hide
    close
endfunction

function! HPasteWindow(direction)
    let old_buffer = bufnr('%')
    call HOpen(a:direction,['buffer',g:buffer])
    let g:buffer = old_buffer
    let &bufhidden = g:bufhidden
endfunction

noremap <c-w>d :call HDeleteWindow()<cr>
noremap <c-w>y :call HYankWindow()<cr>
noremap <c-w>p<up> :call HPasteWindow('up')<cr>
noremap <c-w>p<down> :call HPasteWindow('down')<cr>
noremap <c-w>p<left> :call HPasteWindow('left')<cr>
noremap <c-w>p<right> :call HPasteWindow('right')<cr>
noremap <c-w>pk :call HPasteWindow('up')<cr>
noremap <c-w>pj :call HPasteWindow('down')<cr>
noremap <c-w>ph :call HPasteWindow('left')<cr>
noremap <c-w>pl :call HPasteWindow('right')<cr>
noremap <c-w>pp :call HPasteWindow('here')<cr>
noremap <c-w>P :call HPasteWindow('here')<cr>

endif
like image 7
hcs42 Avatar answered Oct 13 '22 00:10

hcs42