Open file in vertical split in Vim / netrw
Asked Answered
P

4

18

If I open vim with vim . netrw provides me with a nice list of the files and directories in the current directory.

If I open a file using v the file opens in a very narrow split down the left hand side of the screen and the directory listing remains open in a wide split on the right hand side of the screen.

Ideally I'd like it to have the opposite effect, that is, to show the directory listing in a narrow split on the left hand side of the screen and show the file in a wide split on the right hand side of the screen.

Peracid answered 1/4, 2016 at 18:58 Comment(0)
C
6

I'm sure this could be improved upon you can write a custom mapping that target's the netrw filetype.

Stick this in your .vimrc:

" open file vertically to the right
augroup netrw_mappings
    autocmd!
    autocmd filetype netrw call Netrw_mappings()
augroup END
function! OpenToRight()
  :rightbelow vnew
  :wincmd p
  :normal P
endfunction
function! Netrw_mappings()
    noremap V :call OpenToRight()<cr>
endfunction

The only thing is that you need to use V rather than v. For some reason I was unable to override netrw's v command, but using the capital version seems better anyway since it's not overriding a default.

Concertina answered 1/4, 2016 at 19:20 Comment(3)
That's fantastic! - Sorry to be cheeky but... is it possible to control the width's of the splits?Peracid
@Peracid You can run commands like :30winc > to change the window widthConcertina
nnoremap <buffer> v :call OpenToRight()<cr> will do add <buffer>Billye
A
28

Netrw v153 and later (May 28, 2014) gives you the :Lexplore command, which, by default, opens a directory listing on the left hand side and opens files to the right (by pressing <cr>).

Aldous answered 3/5, 2016 at 20:30 Comment(1)
That looks perfect, thanks.. Could you give me a hint as to how / where to install the new version... Do I need to uninstall the existing netrw?Peracid
P
11

Whilst Jonathan.Brink's answer works perfectly well, simply adding

let g:netrw_altv=1

to .vimrc also seems to do the trick...

See https://superuser.com/questions/1056929/open-file-in-vertical-split-in-vim-netrw/1062063#1062063 for more info.

Peracid answered 6/4, 2016 at 18:24 Comment(0)
C
6

I'm sure this could be improved upon you can write a custom mapping that target's the netrw filetype.

Stick this in your .vimrc:

" open file vertically to the right
augroup netrw_mappings
    autocmd!
    autocmd filetype netrw call Netrw_mappings()
augroup END
function! OpenToRight()
  :rightbelow vnew
  :wincmd p
  :normal P
endfunction
function! Netrw_mappings()
    noremap V :call OpenToRight()<cr>
endfunction

The only thing is that you need to use V rather than v. For some reason I was unable to override netrw's v command, but using the capital version seems better anyway since it's not overriding a default.

Concertina answered 1/4, 2016 at 19:20 Comment(3)
That's fantastic! - Sorry to be cheeky but... is it possible to control the width's of the splits?Peracid
@Peracid You can run commands like :30winc > to change the window widthConcertina
nnoremap <buffer> v :call OpenToRight()<cr> will do add <buffer>Billye
W
0

You can add this to your .vimrc, it works well with v

set splitright

By default, the netrw plugin follows your splitright setting (as indicated by its default value =&spr for the g:netrw_altv variable) when deciding where to open new windows.

I think that netrw reads the value of splitright at Vim's startup. So, setting splitright using :set splitright in a session won't affect netrw's behavior in that session.

Wartime answered 8/6, 2023 at 22:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.