2

Is there a way to effectively combine :w and :e commands so that the current file is saved and a new one is opened for editing in one go? And to do so in a way that allows tab completion of path and filename on the command line?

5

You can use command to add new commands. These have to start with a capital letter.

Something like this should do what you want:

command! -nargs=1 -complete=file WE write | edit <args>

You can then do:

:WE new-file
  • -nargs=1 - accept one argument
  • -complete=file - do file completion
  • WE - the command name
  • write | edit <args> - first call :w, and then :e with the argument we gave it.

Note that there's also the autowrite option:

Write the contents of the file, if it has been modified, on each :next, :rewind, :last, :first, :previous, :stop, :suspend, :tag, :!, :make, CTRL-] and CTRL-^ command; and when a :buffer, CTRL-O, CTRL-I, '{A-Z0-9}, or `{A-Z0-9} command takes one to another file.

and the autowriteall option:

Like 'autowrite', but also used for commands :edit, :enew, :quit, :qall, :exit, :xit, :recover and closing the Vim window. Setting this option also implies that Vim behaves like 'autowrite' has been set.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.