I want to map :e file.txt
to :tabnew file.txt
, or the equivalent setting for editing file in a new tab. What do I need to put into my vimrc?
-
3Are you aware that vim tabs are not the same as tabs in other applications? see stackoverflow.com/questions/102384/using-vims-tabs-like-buffers and in particular the first answer which I can only agree with: "Trying to beat Vim into 1 tab == 1 buffer is an exercise in futility." – Hotschke Oct 29 '18 at 10:05
You probably want to read :h user-commands
and put the following in your vimrc:
command! -nargs=1 -complete=file E tabnew <args>
You can't override :e
but you can create the command :E
which will take one argument (-nargs=1
), complete this argument as tabnew
does (-complete=file
) and executes tabnew
with the argument it took.
However as @Hotschke said in comments you probably don't need such a command.
Like everyone else, I recommend pretty strongly against doing this, but if you're dead set on the idea (and for future readers who might be doing something slightly different) you could use something like:
:cabbrev <expr> e getcmdline() == 'e' && getcmdtype() == ':' ? 'tabnew' : 'e'
This will replace :e
with :tabnew
as soon as you type the space after the e
. If you find the change too visually jarring, you could perhaps instead set up the the E
command from statox's answer and convert :e
into :E
instead.
See :help :cabbrev
for more details.