├── README.md ├── doc └── githubinator.txt └── plugin └── githubinator.vim /README.md: -------------------------------------------------------------------------------- 1 |

vim-githubinator

2 |

Quickly show selected text in vim on Github if a remote exists.

3 |

4 | 5 |

6 | 7 | ## What? 8 | This plugin helps you select some text in vim visually and then open it in Github or other remote repository with the selection highlighted. 9 | This was inspired from [Githubinator](https://github.com/ehamiter/GitHubinator) for sublime. 10 | 11 | ## Default commands 12 | ```text 13 | gho 14 | Open selected text on git remote repository with the default 15 | browser using the `open` or `xdg-open` command if it is present, 16 | throws an error otherwise. 17 | 18 | ghc 19 | Same as gho except it doesn't open the 20 | browser but rather copies the said URL to the clipboard using 21 | pbcopy if it is present, throws an error otherwise. 22 | ``` 23 | 24 | ## Installation 25 | Using [vim-zen](https://github.com/danishprakash/vim-zen): 26 | ```vim 27 | Plugin 'danishprakash/vim-githubinator' 28 | ``` 29 | 30 | ## Contributing 31 | Do you want to make this better? Open an issue and/or a PR on Github. Thanks! 32 | 33 | ## License 34 | MIT License 35 | 36 | Copyright (c) 2018 [Danish Prakash](https://github.com/danishprakash) 37 | -------------------------------------------------------------------------------- /doc/githubinator.txt: -------------------------------------------------------------------------------- 1 | *githubinator* show selected text on Github 2 | 3 | Author: Danish Prakash 4 | Repo: https://github.com/danishprakash/vim-githubinator 5 | License: MIT 6 | 7 | ======================================================================= 8 | Githubinator is a nifty little plugin which allows you to open snippets 9 | of text inside your editor directly on remote repository if a remote 10 | repository for the project exists. 11 | 12 | There are just a total of 2 requirements for this to work 13 | - You have `open` or `xdg-open` command for |(githubinator-open)| 14 | - You have `pbcopy` or `xsel` command for |(githubinator-copy)| 15 | 16 | ======================================================================= 17 | MAPPINGS 18 | 19 | *(githubinator-open)* 20 | Open selected text on git remote repository with the default 21 | browser using the `open` or `xdg-open` command if it is present, 22 | throws an error otherwise. 23 | 24 | *(githubinator-copy)* 25 | Same as |(githubinator-open)| except it doesn't open the 26 | browser but rather copies the said URL to the clipboard using 27 | pbcopy if it is present, throws an error otherwise. 28 | 29 | ======================================================================= 30 | DEFAULT MAPPINGS 31 | 32 | map {lhs} {rhs} ~ 33 | --- -------- --------------------------- ~ 34 | nv *gho* |(githubinator-open)| 35 | nv *ghc* |(githubinator-copy)| 36 | 37 | ======================================================================= 38 | OPTIONS 39 | 40 | *g:githubinator_no_default_mapping* (default: 0) 41 | Disable the default mappings if the value is not 0. 42 | 43 | ======================================================================= 44 | LICENSE 45 | 46 | MIT 47 | 48 | ======================================================================= 49 | CONTRIBUTING 50 | 51 | Do you want to make this better? Open an issue and/or a PR on Github. 52 | Thanks! 53 | 54 | Github: https://github.com/danishprakash/vim-githubinator 55 | 56 | ======================================================================= 57 | vim:tw=78:ts=8:ft=help:norl:noet:fen: 58 | -------------------------------------------------------------------------------- /plugin/githubinator.vim: -------------------------------------------------------------------------------- 1 | " ================================================================ 2 | " Name: vim-githubinator: open highlighted text on Github 3 | " Maintainer: Danish Prakash 4 | " HomePage: https://github.com/danishprakash/vim-githubinator 5 | " License: MIT 6 | " ================================================================ 7 | 8 | " MIT License 9 | 10 | " Copyright (c) 2018 Danish Prakash 11 | 12 | " Permission is hereby granted, free of charge, to any person obtaining a copy 13 | " of this software and associated documentation files (the "Software"), to deal 14 | " in the Software without restriction, including without limitation the rights 15 | " to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 16 | " copies of the Software, and to permit persons to whom the Software is 17 | " furnished to do so, subject to the following conditions: 18 | 19 | " The above copyright notice and this permission notice shall be included in all 20 | " copies or substantial portions of the Software. 21 | 22 | " THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 23 | " IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 24 | " FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 25 | " AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 26 | " LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 27 | " OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 28 | " SOFTWARE. 29 | 30 | " returns line fragment 31 | function! s:get_line_fragment(mode) 32 | if a:mode ==# 'v' 33 | return printf('#L%d-L%d', line("'<"), line("'>")) 34 | else 35 | return printf('#L%d', line('.')) 36 | endif 37 | endfunction 38 | 39 | " get remote URL from .git/config 40 | function! s:generate_url(mode) 41 | let l:root_dir = system('git rev-parse --show-toplevel') 42 | if v:shell_error != 0 43 | echoerr printf('githubinator: %s is NOT managed by git', getcwd()) 44 | return 45 | endif 46 | 47 | " In order to support relative filepath, convert full path and remove the git root path in it. 48 | let l:file_name = expand('%:p')[len(root_dir) : -1] 49 | 50 | " Remove `tags/`. 51 | let l:branch = system('git name-rev --name-only HEAD') 52 | let l:branch = substitute(l:branch, '^tags//\|\n', '', '') 53 | 54 | " Change URL scheme to https. 55 | " Remove `.git` and newline. 56 | " Change `:` to `/` (in case of ssh remote). 57 | let l:remote = system('git config remote.origin.url') 58 | let l:remote = substitute(l:remote, 'git@\|git:', 'https://', 'g') 59 | let l:remote = substitute(l:remote, '\.git.$\|\n', '', '') 60 | let l:remote = substitute(l:remote, ':\([^/]\)', '/\1', 'g') 61 | 62 | " Build final URL. 63 | return printf('%s/blob/%s/%s%s', l:remote, l:branch, l:file_name, s:get_line_fragment(a:mode)) 64 | endfunction 65 | 66 | function! s:github_open_url(mode) 67 | let l:final_url = s:generate_url(a:mode) 68 | 69 | if executable('xdg-open') 70 | call system('xdg-open ' . l:final_url) 71 | elseif executable('open') 72 | call system('open ' . l:final_url) 73 | else 74 | echoerr 'githubinator: no `open` or equivalent command found. Try `ghc` to copy URL' 75 | endif 76 | endfunction 77 | 78 | function! s:github_copy_url(mode) 79 | let l:final_url = s:generate_url(a:mode) 80 | let @+ = l:final_url 81 | echom 'Githubinator: URL copied to clipboard' 82 | endfunction 83 | 84 | vnoremap (githubinator-open) :call github_open_url('v') 85 | vnoremap (githubinator-copy) :call github_copy_url('v') 86 | nnoremap (githubinator-open) :call github_open_url('n') 87 | nnoremap (githubinator-copy) :call github_copy_url('n') 88 | 89 | if get(g:, 'githubinator_no_default_mapping', 0) == 0 90 | vmap gho (githubinator-open) 91 | vmap ghc (githubinator-copy) 92 | 93 | nmap gho (githubinator-open) 94 | nmap ghc (githubinator-copy) 95 | endif 96 | --------------------------------------------------------------------------------