2

I want to automatically set the paste mode on when I open a new file (an empty file that doesn't exist).

autocmd BufNewFile * :set paste 

in the vimrc does the trick with vim newfile or :e newfile, but not with :enew.

How can I run an autocommand on :enew?

5
  • 1
    You could try with BufCreate instead of BufNewfile it should be triggered each time you load a file which isn't listed. Commented Oct 5, 2015 at 16:34
  • 1
    just to ask. Is "paste" mode useful? I always using + (plus) register, wich refers to system clipboard. Commented Oct 6, 2015 at 4:01
  • @statox: Thanks but unfortunately BufCreate will also set paste mode when I open existing files. Commented Oct 6, 2015 at 12:44
  • @dartNNN: "*p does seem to do the trick. It doesn't really solve the issue of executing an autocommand on :enew, but it does help with my specific use-case, thanks. Commented Oct 6, 2015 at 12:45
  • @emmanuel I didn't get exactly what you wanted, indeed BufCreated isn't a good solution then. Commented Oct 6, 2015 at 13:38

2 Answers 2

2

set paste will have many side effects, e.g. disable indention. I would imagine the requested behavior would become annoying quickly.

Alternatives:

  • Use 'pastetoggle' setting to setup a key to toggle 'paste',
  • unimpaired.vim's yo mappings which put Vim in insert mode with 'paste' set and disables 'paste' upon leaving insert mode
  • vim-bracketed-paste enables transparent pasting into Vim for certain terminals.

For more help see:

:h 'paste' :h 'pastetoggle' 
Sign up to request clarification or add additional context in comments.

1 Comment

I'm happy to toggle paste off once I'm done pasting (or on the rare occasions where I do write a file from scratch), but I'd still like it to be the default for empty buffers, as I rarely ever write a file entirely from scratch, and more often than not open a new buffer just to do some quick editing on something in my clipboard. I'll look into these plugins though, thanks :)
0

To answer the question, the following will run an autocommand on :enew:

autocmd BufCreate * if '' == expand("<afile>") | set paste | endif 

You can replace BufCreate with BufAdd or BufNew, it will work all the same. The important part is the if '' == expand("<afile>") that will only execute the command if it the new buffer has no name.

NB:

For the specific use-case of pasting text in new buffer, auto-switching to paste mode is not a great solution though, I wouldn't recommend using this.

As the paste is maintained even when you switch buffer. You'd need additional autocommands to unset the paste mode when you switch buffer/window (autocmd WinEnter * set nopaste will work for windows, but for some reason autocmd BufEnter * set nopaste doesn't help when switching buffer).

To ease pasting stuff in new buffers (or otherwise), I'd recommend either:

  • typing "*p or "+p in normal mode, as to use vim's native mechanism to paste from the clipboard registers, as suggested by @dartNNN (assuming you're on your local machine)
  • using one of the plugins recommended in @peter-rincker's answer

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.