Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vim: Add clickable label

Tags:

vim

hyperlink

I know that in emacs it is possible to insert some kind of "clickable text". I.e. you can insert a text, that, when the user presses enter on it, opens another file.

Is there something like this for vim?

like image 752
phimuemue Avatar asked Oct 07 '11 10:10

phimuemue


2 Answers

It is possible, but is filetype-specific. A best example will be vim's own help system that is nothing fancier than an unmodifiable buffer with specific mappings.

See vimwiki and vimorgmode for examples to have such links.

like image 125
mike3996 Avatar answered Oct 03 '22 12:10

mike3996


For simple ad hoc cases you could write a function to which opens a certain file based on the word under the cursor. You could then map this function to the double-click event.

For example:

function! CustomLoad()
    let word = expand("<cword>")
    let path = "/path/to/file/to/be/opened"
    if ( word == "special_keyword" && filereadable(path) )
        sil exe "split " . path
    endif
endfunction

And map it using:

nnoremap <2-LeftMouse> :call CustomLoad()<CR>

Thus double-clicking (in normal mode) on the word special_keyword will open the file /path/to/file/to/be/opened if it is readable. You could add multiple cases for different keywords, or do some text-processing of the keyword to generate the filename if required. (Note that the filereadable condition is not necessary, but probably a good idea.)

Hope this helps.

like image 22
Prince Goulash Avatar answered Oct 03 '22 14:10

Prince Goulash