Skip to content

Instantly share code, notes, and snippets.

@nornagon
Created March 12, 2012 23:49
Show Gist options
  • Save nornagon/2025487 to your computer and use it in GitHub Desktop.
Save nornagon/2025487 to your computer and use it in GitHub Desktop.
Open related file in vim (a la Sublime Text 2's Alt-O)
" Opens a file in [path/to/current_file.*] that isn't already open.
function! RelatedFiles()
return filter(split(globpath(expand('%:h'), expand('%:t:r').'.*')), '!bufloaded(v:val)')
endfunction
function! OpenRelatedFile()
let files = RelatedFiles()
if len(files) > 0
" always open .h files on the left.
if match(files[0], "\.h$") >= 0
exec 'lefta vsplit '.files[0]
else
exec 'rightb vsplit '.files[0]
endif
endif
endfunction
nmap ^[o :call OpenRelatedFile()<CR>
nmap <M-o> :call OpenRelatedFile()<CR>
nmap ø :call OpenRelatedFile()<CR> " OSX is adorable
" Alt keys can be fiddly; you might need to replace <M-o> above with ^[o (literal ^[, type Ctrl-V Alt-O)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment