2

In gvim the command :browse confirm e launches a GTK file browser. The default file mask in this window depends on the filetype from which the browse window is invoked. I would like to change that default file mask depending on the filetype. For example from .cpp file the mask is *.cpp *.c++ and I want to add *.hpp *.ipp there. From python file the mask is *.py I would like it to be *. And so on.

I use gvim version 8.1.141 on linux devuan beowulf.

The GTK browser invoked by gvim

(I could not find proper tags for this question, I tried to use tags: file browser, gtk, wildcard, glob, mask)

So far by grepping gvim sources I have found:

grep -E "\*\.cpp \*\.c++" . -r --color

./runtime/ftplugin/pyrex.vim:                   \ "C++ Source Files (*.cpp *.c++)\t*.cpp;*.c++\n" .
./runtime/ftplugin/c.vim:    let b:browsefilter = "C++ Source Files (*.cpp *.c++)\t*.cpp;*.c++\n" .
./runtime/ftplugin/c.vim:         \ "C++ Source Files (*.cpp *.c++)\t*.cpp;*.c++\n" .

So what is b:browsefilter and how to use it?

The :h browsefilter says that "You can override the filter setting on a per-buffer basis by setting the b:browsefilter variable.".

But I want to override it not per-buffer, but per-filetype from where the browse dialog was invoked.

  • I'm voting to close this question as off-topic because this site is about vi/vim, not the GTK file browser. – D. Ben Knoble Feb 16 at 16:10
  • 1
    Well, the glob information is passed on from gvim. How else the browser would know which buffer inside gvim was active upon invocation? I am thinking about grepping gvim sources to find the variable name for that. Was hoping that someone knows it. OK, so far I got this: grep -E "\*\.cpp \*\.c\+\+" . -r --color yields: ./runtime/ftplugin/c.vim: let b:browsefilter = "C++ Source Files (*.cpp *.c++)\t*.cpp;*.c++\n". So what is b:browsefilter and how to use it ? – cosurgi Feb 16 at 17:05
  • ah interesting; didnt know that. Your best first stop is always :help browsefilter – D. Ben Knoble Feb 16 at 17:09
3

The browsefilter variable is a convenient way to add custom glob like filters for the graphical :browse dialog and select only the relevant filetypes for display.

As you discovered, those are usually set in the filetype plugins. If you want to customize those filters, create a file in ~/.vim/after/ftplugin/<filetype>.vim (replace <filetype> by the wanted filetype, e.g. c and python) and add the following content (this example is for your c filetype):

if exists("b:browsefilter")
    let b:browsefilter = "My C Source Files (cpp,c++,hpp,ipp)\t*.cpp;*.c++;*.hpp;*.ipp\n". b:browsefilter
endif

It is important to place this in the after directory, as the default filetype needs to be run first to initialize the b:browsefilter variable that you can than customize further.

| improve this answer | |
  • Thank you! It works perfectly. A bonus question: how can I tell a running gvim session to reload ~/.vim/after/ftplugin/<filetype>.vim ? Ah OK :source ~/.vim/after/ftplugin/c.vim worked :) – cosurgi Feb 17 at 16:13

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.