Skip to content

Instantly share code, notes, and snippets.

@marczych
Created October 13, 2012 02:03
Show Gist options
  • Save marczych/3882918 to your computer and use it in GitHub Desktop.
Save marczych/3882918 to your computer and use it in GitHub Desktop.
Vim: Take the word under the cursor, append the current buffer's extension, and open it using find.
nnoremap <silent> <C-o> :call FindFile()<CR>
function! FindFile()
" Get the word under cursor.
let cursorWord = expand("<cword>")
" Get the current file name and keep only the extension.
let currentFile = expand("%")
let extPos = stridx(currentFile, ".")
" Append an extension only if the current file has an extension.
if extPos != -1
let extension = strpart(currentFile, extPos)
else
let extension = ""
endif
" Construct the file name.
let fileName = cursorWord.extension
" Open the file in the current buffer.
execute "find ".fileName
endfunction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment