Is there a function that can be called to open a file for editing? I know how to open and edit files in Vim, and I know I can use :e filename, but is there a built in function I can call that does this?

If a function exists that does this, there probably is a reason for it to exist in addition to the :e command and knowing about its existence and what it is called would enable me to look at it in the help and see the purpose of its existence.

One problem with using :e filepath is it gets confused when there are spaces in the path. I tried surrounding the path in quotes, and this allows the command to run without error. But then I can't save, as it seems to think the file is inside a non-existent directory. If I escaped the spaces with a \ character, it works when the command is run directly, but it is causing problems inside a plugin test of mine. I haven't figured out a good way to ask for help with that exact problem yet, so I want to keep this question focused on how to consistently open a path for editing regardless of what characters may be in it. A built in function to do this would be nice, but now I see making my own would be good as well.

  • 8
    Not sure why you would need this. If you want to use it in a script you can just use e filename in the script. If you really need it for some reason you can write it a custom function that takes an argument called file and then calls e a:file.
    – Tumbler41
    Commented May 2, 2016 at 18:00
  • 2
    I agree with @Tumbler41: it looks like a typical XY problem. You should give more details about what you want to do in the first place.
    – statox
    Commented May 2, 2016 at 18:56
  • 1
    It could be useful to see the "why" of this question. For example, the OP could have something like Emacs in mind, where each operation is either an interactive function (command) or a function, but in any case has a function associated. In Vim, things are not so "canonical", so to speak. It would be more elegant perhaps for Vim to be able to expose a more powerful API. In any case, the question doesn't show its purpose.
    – VanLaser
    Commented May 3, 2016 at 10:19
  • 3
    Voted for reopening, after the edit - pun intended :) As to the issue, not being able to call something like "edit <filename>" from a plugin, when <filename> has spaces, I think :h fnameescape() can help, e.g. the example found right there: :exe "edit" . fnameescape(fname) may be what you are after.
    – VanLaser
    Commented May 3, 2016 at 14:08
  • 2
    Cool :) In this case, maybe you want to look at :h function-list to see what (more) functions are available - they may further simplify your code.
    – VanLaser
    Commented May 3, 2016 at 17:06

1 Answer 1


Here is a function that does this. It is not tested, but it is based on File.edit() from https://github.com/still-dreaming-1/vim-elhiv/blob/develop/src/File.vim, which I did test.

function! Edit(filepath)
    execute "e ".fnameescape(a:filepath)
  • I sometimes typo :E instead of :e: command! -nargs=1 -complete=file E call Edit(<q-args>) helps. Commented Jul 25, 2020 at 13:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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