0

I'm using Janus (isn't pivotal to understand the question though). Basically what they do, is rewrite :e to be :Edit to work with NERDTree, but NERDTree doesn't support force opening a file (e.g. e!). So I had written something that rewrites :Edit! to :e!, but I like the benefits of using just :Edit!, so I'm trying to spoof if as if it was already built in. This is what I came up with:

ca Edit! e! <bar> Edit

The only problem, is that you can't do that for files that haven't been saved at least once. But I know doing

ca Edit! bd! <bar> Edit

will work: it will close the unsaved buffer and keep moving like nothing happened. All I need to do is write logic that will use the correct command in the right scenario all the time. Preferably the logic doesn't run on every page: only when I actually run the command should it attempt to figure it out. Any ideas?

Edit: I tried asking the #vim channel (gave me tons of help)... they said using expand("%") and checking to see if it exists might help my case? Don't know, just providing as much detail as I can.

Rey
  • 3,639
  • 5
  • 33
  • 40
  • I just asked a similar question on superuser. The answer might be helpful http://superuser.com/questions/348359/how-do-i-create-a-shortcut-for-a-find-and-replace-command-in-vim – CamelBlues Oct 20 '11 at 18:01
  • I can see that helping a little but the main problem I'm having, is figuring out how to detect whether I'm in an unsaved buffer or a once saved file and then using that inside the command (or user command). – Rey Oct 20 '11 at 18:17
  • You must be careful using command-line abbreviations. See http://stackoverflow.com/questions/7513380/vim-change-x-function-to-delete-buffer-instead-of-save-quit/7515418#7515418 for more information. – Peter Rincker Oct 20 '11 at 19:45

1 Answers1

0

Try this. Define a function like this:

function! CommandAbbrForEdit()
    if &modified
        return 'bd! | Edit'
    endif
    return 'e! | Edit'
endfunction

Then define your abbreviation like this:

cabbrev <expr> Edit! CommandAbbrForEdit()
Kurt Hutchinson
  • 2,959
  • 23
  • 30