3

The problem is solved

My purpose is that: I use vim to edit my m-file and debug it in Matlab. There is no easy-to-use interface plugin between vim and Matlab in Win10. So I just edit m-file in vim and run it in Matlab. Sometimes these m-files may be in different directories, so this function can help me to find them quickly.

My system is Win10, and I use gvim.

In vim, I know pwd and cwd in command mode can get the current directory.

And :let g:myvars=expand('%:p:h') to pass the directory to vim script variable.

I also know :! some_cmd_command to execute the cmd command in vim.

I want to implement such a function, that is, let vim open the file manager of the current path, How should I do? Thanks a lot.

4
  • @statox I need a file explorer to call other programs on my computer. :Explorer is useful to help me edit the files, but it's too difficult for it to startup other programs.
    – Hewie Ding
    Jul 16 at 9:33
  • Sorry I deleted my first comment just before you answered. I hadn't understood you were talking about the OS's file explorer but it's clear now :)
    – statox
    Jul 16 at 9:35
  • 2
    Whether or not you can vote, you can accept an answer (though only one) and that is a good way to say thank you. Edits to the post are, well, not
    – D. Ben Knoble
    Jul 16 at 10:45
  • 1
    @HewieDing I just noticed your addition to your question. You're welcome. I upvoted your question so you now have enough rep to upvote whatever answers you feel merit it (and then you can remove the thanks from your question per DBK's comment). :)
    – B Layer
    Jul 18 at 8:41
2

I use following to open directory where current file is and select it if possible:

"" Open explorer where current file is located
"" Only for win for now.
func! File_manager() abort
    " Windows only for now
    if has("win32")
        if exists("b:netrw_curdir")
            let path = substitute(b:netrw_curdir, "/", "\\", "g")
        elseif expand("%:p") == ""
            let path = expand("%:p:h")
        else
            let path = expand("%:p")
        endif
        silent exe '!start explorer.exe /select,' .. path
    else
        echomsg "Not yet implemented!"
    endif
endfunc

nnoremap <silent> gof :call File_manager()<CR>

The thing you might be interested in is silent exe '!start explorer.exe /select,' .. path which starts explorer and selects the path provided.

enter image description here

0
2

Since your command contains a dynamic element (IOW it's not made up entirely of static strings) you'll need to build it with :execute.

Assuming 'shell' and 'shellcmdflag' have the defaults for win32 (cmd.exe and /c, respectively) then this will work:

exe '!c:\windows\explorer.exe ' . expand("%:p:h")

If c:\windows is already in your PATH envvar then you can leave off c:\windows\.

Prepend silent to the above and the extra console window that pops up will be dismissed automatically.

0
1

If you are using gvim, you can simply type :browse e in command mode
you will have a mini-file explorer opened with current directory

1

Simply !start %:p:h should be enough.

However, normall you'd better make use of some builtin "file explorer" plugin. Be it standard netrw or something else. There's rarely a benefit in spawning a full-featured Windows Explorer window.

2
  • There is a benefit for sure. For example I run asciidoc->pdf conversion and then would like to send pdf file. So I open file explorer from within vim and copy&paste&email&whatever.
    – Maxim Kim
    Jul 16 at 9:40
  • @MaximKim I said "rarely", not "never". Also, all this could be done within Vim too.
    – Matt
    Jul 16 at 10:03

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.