Create a new file but not open a buffer in Vim netrw
Asked Answered
J

2

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.

Jessie answered 6/8, 2017 at 20:33 Comment(2)
I use :Lexplore to have a little window at the side with netrw in it. This behaviour ruins my workflow too!Bilingual
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 <cr> to change my file.Barbe
R
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
Rebozo answered 7/8, 2017 at 8:34 Comment(4)
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?Jessie
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 continueJessie
Sorry had a small error, fixed it, and added the function to just create the file.Rebozo
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!Ulibarri
T
0

A simple solution is that you can simply run the normal bash command in the netrw :!touch test.txt. This way you can create a file and netrw will not enter into the file.

Thagard answered 13/5 at 2:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.