Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opening a file from .netrw in a specified split

Tags:

vim

netrw

This is the same question as on SuperUser, but I feel it has a better chance of being answered here, so ...

In Vim, I have four splits - two by two - and in the upper left one netrw is open. Is there a way to open a file from netrw in the lower right split, lower left, etc.?

like image 679
Rook Avatar asked Dec 06 '11 04:12

Rook


3 Answers

there're some hints here https://superuser.com/questions/377501/keep-cursor-in-netrw-window-when-browsing-files-in-vim

Put this in your .vimrc,

let g:netrw_preview = 1

to configure vertical preview splits, then when the cursor is over the desired file, type p to open a preview window. To close the window, type CtrlWz.

The term used in vim for a window that's opened without the cursor moving to it is a "preview window". To find out more about this, see

:help netrw-preview
:help CTRL-W_z

or just

:help netrw

and browse the table of contents for other browsing commands and other netrw features.

like image 181
delta32 Avatar answered Oct 21 '22 12:10

delta32


You can set the g:netrw_chgwin variable to make netrw open the files in a specific window. See:

:h netrw-C

So to make the current window the target of netrw type this while you're in that window:

:let g:netrw_chgwin = winnr()

Another way is to launch netrw in the target window (:E), hit C to select it for editing and close netrw with <c-o>.

like image 28
Nikita Avatar answered Oct 21 '22 12:10

Nikita


To bring above answers together some inspirations: I use the following window configuration together with netrw (together with :let g:netrw_liststyle = 2):


    -------------------------------- ...
    Netrw-split: topleft spilt
    -------------------------------- ...
             |          |          |
    working  | working  | working  |
    window 1 | window 2 | window 3 | ...
             |          |          |

Thus I can go directly to Netrw-split from every other window. Then I put to my .vimrc:


    augroup netrw
        autocmd!
        autocmd WinLeave * if &ft !=# "netrw" | let g:netrw_chgwin = winnr() | endif
        autocmd filetype netrw call Netrw_mappings()
    augroup END

The WinLeave command sets the global g:netrw_chgwin variable to the window just left (except if we are in the netrw-window). Thus netrw will open any file in the window from which I accessed it and due to the window layout I can access netrw from any other window.

The autocmd 'filetype' is used to also create a new file in the window from which I accessed netrw. For this netrw's '%' command has to be overwritten:


    function! Netrw_mappings()
        noremap <buffer>% :call CreateInLastWindow()<cr>
    endfunction

With a function creating the new file in window g:netrw_chgwin:


    function! CreateInLastWindow()
        let l:filename = input("new file's name: ")
        let l:netrwdir = b:netrw_curdir
        execute g:netrw_chgwin . "wincmd w"
        execute 'edit ' . l:netrwdir.'/'.l:filename
    endfunction

like image 22
Stephan Lukits Avatar answered Oct 21 '22 11:10

Stephan Lukits