0

There is a file browser plugin called fern and I would like to open a split with the fern file system explorer open. This is what I have so far:

function File_tree()
    execute 'vert sb'
    execute 'Fern .' " <-- Error here
    execute 'vertical resize 35'
    execute 'wincmd l'
endfunction

function Terminal_window()
    execute 'bel split term://fish'
    execute 'resize 12'
    execute 'wincmd k'
endfunction

function Workspace()
    execute 'call Terminal_window()'
    execute 'call File_tree()'
endfunction

call Workspace()

I suspect it's because Fern . isn't a valid nvim command unlike vert sb, etc. And that command is from the fern plugin mentioned above. So is there a way for me to run a command that hails from a plugin?

However, when I run :call File_tree() there is no issue, everything works as expect so what's the issue here?


Update to the functions

function File_tree()
    vert sb 
    Fern . 
    vertical resize 35 
    wincmd l

endfunction

function Terminal_window()
    bel split term://fish 
    resize 10 
    wincmd k
endfunction

function Workspace()
    call File_tree()
    call Terminal_window()
endfunction

nnoremap <leader>fw :on<cr>
nnoremap <leader>suw :call Workspace()<cr>

autocmd VimEnter * call Workspace()
1
  • 2
    I didn't read the post properly after the edit. My bad, i'll just revert it.
    – YJH16120
    Apr 29, 2021 at 10:49

1 Answer 1

2

If you're running that code block from inside a vimrc file (or init.vim for NeoVim), that will explain why the :Fern command will not be available yet, since the code in a vimrc will run before plug-in initialization code is executed and custom plug-in commands are only defined there.

In order to have the call only happen once plug-ins have initialized (at which point :Fern will have been defined), use a VimEnter event, which triggers only once, at startup, but only after initialization is completed.

In your case:

autocmd VimEnter * ++nested call Workspace()

Note: You need the ++nested to have the code in your functions trigger any other autocmd's needed, such as the BufReadCmd implementing term:// to open a terminal. See NeoVim's :help terminal-start for an explanation of that exact situation.


Unrelated, but note also that you don't need to wrap each command in an execute here. You only need execute if you need to build the command string from dynamic contents, which is not the case here.

(In some cases, you might need execute if you're joining multiple commands with |, in to prevent a command that would interpret the | literally. See :help :bar for details on that. But that is not the case here either.)

You could just drop every single one of the execute calls in the code from your sample.

In your specific case, the function definition could simply read:

function File_tree()
    vert sb
    Fern .  " Comment is fine here.
    vertical resize 35
    wincmd l
endfunction

And similarly for the two other functions.

11
  • So for example. I can write :vert sb | Fern . | vertical resize 20 | wincmd l in the function and that works the same as using multiple execute's?
    – YJH16120
    Apr 28, 2021 at 8:33
  • Yes. The more straightforward way is to just write them line-by-line though, nothing wrong with that. Note that :Fern . | some other command is only possible if the command was defined with :command -bar, but I checked the source code and confirmed that's the case. See :help :bar for the commands that will recognize a | as their own argument, so they would need an :execute to be followed by another command, on the same line, with a | as a separator.
    – filbranden
    Apr 28, 2021 at 8:39
  • I implemented your suggestion (I also edited the question), this is the desired outcome, but instead this happens.
    – YJH16120
    Apr 28, 2021 at 8:50
  • "Do you mean you get the result where fish doesn't seem to open, or the tree from Fern, when using VimEnter? " Both.
    – YJH16120
    Apr 28, 2021 at 11:01
  • I updated the question to reflect those changes. And I've tried it, but it still gets the wrong result.
    – YJH16120
    Apr 29, 2021 at 13:58

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.