8

I have VIM netrw split setting to open any file in a right-side Preview Buffer, and it works fine.

My problem is that a new file is automatically open in netrw buffer, when I create the new file by % command. I want to open the new created file in Preview Buffer, not in netrw buffer. Is this possible?

Thanks for your help in advance.

Joe Cho
  • 149
  • 1
  • 7
  • I use `:Lexplore` to have a little window at the side with `netrw` in it. This behaviour ruins my workflow too! – LondonRob Jun 20 '18 at 12:38
  • Can you explain what a Preview Buffer is, and how to use it? I think this is what I want too. I usually open new panes with `v` and if I already have a pane, I press `` to change my file. – mazunki Jun 05 '20 at 22:26

1 Answers1

5

I think you will have to overwrite the mapping.

autocmd filetype netrw call Netrw_mappings()
function! Netrw_mappings()
  noremap <buffer>% :call CreateInPreview()<cr>
endfunction

In this function you will have to rebuild the netrw function, but it is not that hard:

function! CreateInPreview()
  let l:filename = input("please enter filename: ")
  execute 'pedit ' . b:netrw_curdir.'/'.l:filename
endf

Note: this only opens the buffer in the preview. It does not save the file.

If you just want to create the file, without opening it anywhere, you can use the external command touch (At least in Unix systems).

function! CreateInPreview()
  let l:filename = input("please enter filename: ")
  execute 'silent !touch ' . b:netrw_curdir.'/'.l:filename 
  redraw!
endf
Doktor OSwaldo
  • 5,732
  • 20
  • 41
  • Thank you so much for the answer. What if I just want to create a file but not open the file in a new buffer? – Joe Cho Aug 17 '17 at 02:28
  • By the way, I am getting this error: Error detected while processing function CreateInPreview: line 2: E21: Cannot make changes, 'modifiable' is off E21: Cannot make changes, 'modifiable' is off Press ENTER or type command to continue – Joe Cho Aug 17 '17 at 02:38
  • Sorry had a small error, fixed it, and added the function to just create the file. – Doktor OSwaldo Aug 17 '17 at 06:14
  • 1
    On Windows I had problems with the "silent !touch ". I replaced that line with the following - now it works: execute 'pedit ' . b:netrw_curdir.'/'.l:filename execute 'wincmd k' execute 'wq' redraw! – Toni Schilling Oct 19 '21 at 21:45