Contextual tabs for vim/neovim
Neovim (and just recently vim!) has a neat feature: each tab can have a different working directory. This plugin adds some functions and fzf commands to change the working directory of tabs.
nvim-contabs depends on fzf and, optionally, on vim-airline. If you use vim-plug:
Plug 'm00qek/nvim-contabs'
You can use contabs
in two ways. The first is by calling functions to change
the tab working directory - which is great if you don't want to use FZF. Add to
your config
" Change the working directory of current tab
command! -nargs=1 -complete=dir EP call contabs#project#edit(<q-args>)
" Open a new tab setting the working directory
command! -nargs=1 -complete=dir TP call contabs#project#tabedit(<q-args>)
Now you can run :EP ~/dev/myproject
or :TP ~/dev/myproject
.
The other way contabs
work is by listing you projects in a FZF buffer. To use
that you need to inform where to find your projects by setting
g:contabs#project#locations
in your config
let g:contabs#project#locations = [
\ { 'path': '~/dev/aproject' },
\ { 'path': '~/dev/anotherproject' },
\]
Each item in the array is a location
. You can add more options to the map:
option | effect | default |
---|---|---|
depth | the level of subdirectories | 0 |
git_only | list only git repos | v:false |
entrypoint | file to show when opening project | [] |
formatter | function to format project name on FZF buffer | { x -> x } |
Some examples of locations
:
" directories using git satisfying '~/projects/*/*'
echo { 'path': '~/projects', 'depth': 2, 'git_only': v:true }
" point to '~/.config/nvim' and change its display on FZF buffer
echo { 'path': '~/.config/nvim', 'formatter': { _ -> 'Neovim Config' } }
" directories satisfying '$GOPATH/src/github.com/libgit2/*'
echo { 'path': '$GOPATH/src/github.com/libgit2', 'depth': 1 }
" directories using git satisfying '~/haskell/*' and show 'λ | ' before their paths
echo { 'path': '~/haskell', 'depth': 1, 'git_only': v:true, 'formatter': { dirpath -> 'λ | ' . dirpath } }
" directories using git satisfying '~/clojure/*' and, on project selection, open one of the entrypoint files
echo { 'path': '~/clojure', 'depth': 1, 'git_only': v:true, 'entrypoint': ['project.clj', 'tasks/build.boot'] }
The following nmaps open the FZF buffer:
"invoke fzf with the list of projects configured in g:contabs#project#locations
"the enabled hotkeys are { 'ctrl-t': 'tabedit', 'ctrl-e, <cr>': 'edit' }
nnoremap <silent> <Leader>p :call contabs#project#select()<CR>
"invoke fzf with the list of buffers of current tab working directory
"the enabled hotkeys are defined in g:fzf_action, but if this var is not
"defined the command will use { 'ctrl-t': 'tabedit', 'ctrl-e, <cr>': 'edit', 'ctrl-v': 'vsp', 'ctrl-x': 'sp' }
nnoremap <silent> <Leader>b :call contabs#buffer#select()<CR>
When using contabs
and vim-airline
, by defautl, your tab titles will be the
basename of it's current working directory. If you want to disable this feature
add to your config:
let g:contabs#integrations#airline = v:false
If you use gvim
, add to your config:
set guitablabel=%{contabs#integrations#tabline#label(tabpagenr())}
or, if you are using vim/neovim on terminal:
set tabline=%!contabs#integrations#tabline#create()
If you want to create your own tabline function, you can use the function
contabs#integrations#tabline#raw_label(tabpagenr())
to just eval the chosen
formatter.
The labels are provided by formatters
, which are functions operating over the
tab current directory and location config. You can choose one of the
predefined
formatters using:
" you can use 'basename', 'path', 'pathshorten', 'project/path',
" 'project/pathshorten' or 'location/formatter'
let g:contabs#integrations#tabline#theme = 'project/path'
If you want to write you own formatter
use
let g:contabs#integrations#tabline#theme = 'mytheme'
call contabs#integrations#tabline#register('mytheme',
\ { location, cwd -> location.path . " | " . cwd })
where location
is the related entry in g:contabs#project#locations
and cwd
is the current tab working directory
To test you changes to the code you should use
make prepare
make nvim
make vim