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.?
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.
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>
.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With