Skip to content

Instantly share code, notes, and snippets.

@girishji
Last active March 11, 2024 14:29
Show Gist options
  • Save girishji/e3479918da89890b6e85b9efc4e95da5 to your computer and use it in GitHub Desktop.
Save girishji/e3479918da89890b6e85b9efc4e95da5 to your computer and use it in GitHub Desktop.
Extend scope.vim to search python files

You can extend scope.vim to fuzzy search python defs, classes, and keywords.

Place this file in ~/.vim/after/ftplugin/python.vim.

if exists('g:loaded_scope')
    import autoload 'scope/popup.vim'
    def Things()
        var things = []
        for nr in range(1, line('$'))
            var line = getline(nr)
            if line =~ '\(^\|\s\)\(def\|class\) \k\+('
                    || line =~ 'if __name__ == "__main__":'
                things->add({text: $"{line} ({nr})", linenr: nr})
            endif
        endfor
        popup.FilterMenu.new("Py Things", things,
            (res, key) => {
                exe $":{res.linenr}"
                normal! zz
            },
            (winid, _) => {
                win_execute(winid, $"syn match FilterMenuLineNr '(\\d\\+)$'")
                hi def link FilterMenuLineNr Comment
            })
    enddef
    # replace <your_key> with your favorite key combination
    nnoremap <buffer> <your_key> <scriptcmd>Things()<CR>
endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment