Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
With pearofducs/ansible-vim plugin installed, following mapping behaves like `gf`, navigates to role under cursor's tasks/main.yml file. Ideal to be used in playbooks
" vim-plug example
call plug#begin('~/.vim/plugged')
Plug 'pearofducks/ansible-vim'
call plug#end()
let g:ansible_goto_role_paths = './roles,../_common/roles'
function! FindAnsibleRoleUnderCursor()
if exists("g:ansible_goto_role_paths")
let l:role_paths = g:ansible_goto_role_paths
let l:role_paths = "./roles"
let l:tasks_main = expand("<cfile>") . "/tasks/main.yml"
let l:found_role_path = findfile(l:tasks_main, l:role_paths)
if l:found_role_path == ""
echo l:tasks_main . " not found"
execute "edit " . fnameescape(l:found_role_path)
au BufRead,BufNewFile */ansible/*.yml nnoremap <leader>gr :call FindAnsibleRoleUnderCursor()<CR>
au BufRead,BufNewFile */ansible/*.yml vnoremap <leader>gr :call FindAnsibleRoleUnderCursor()<CR>

This comment has been minimized.

Copy link
Owner Author

@mtyurt mtyurt commented May 13, 2020



This comment has been minimized.

Copy link

@rjshrjndrn rjshrjndrn commented May 16, 2020

Thanks for the snippet. But why are we having 2 au lines at the end. And some people, atleast me, uses yaml as the extention.

    au BufEnter,BufNewFile */ansible/*.y[a]\\\{0,1\}ml nnoremap <silent> <leader>gr :call FindAnsibleRoleUnderCursor()<CR>

I think this will support both yml and yaml
BufEnter will be more efficient than BufRead as we've to process only when we enter the butter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.