From 87518b31365db356d27594988e607b9a3dd95730 Mon Sep 17 00:00:00 2001 From: Mike Percy Date: Wed, 27 Apr 2016 01:09:46 -0700 Subject: [PATCH] Implement "edit link in Vim" shortcut Added a shortcut called `ge` that is similar to `gx` except that it opens the file under the cursor for editing in Vim. This is particularly useful when editing markdown that contains relative links to other markdown files. --- README.md | 4 ++++ ftplugin/markdown.vim | 15 +++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/README.md b/README.md index 90987f2..9c6665e 100644 --- a/README.md +++ b/README.md @@ -240,6 +240,10 @@ The following work on normal and visual modes: Known limitation: does not work for links that span multiple lines. +- `ge`: open the link under the cursor in Vim for editing. Useful for relative markdown links. `Markdown_EditUrlUnderCursor` + + The rules for the cursor position are the same as the `gx` command. + - `]]`: go to next header. `Markdown_MoveToNextHeader` - `[[`: go to previous header. Contrast with `]c`. `Markdown_MoveToPreviousHeader` diff --git a/ftplugin/markdown.vim b/ftplugin/markdown.vim index c2fff46..daaa2af 100644 --- a/ftplugin/markdown.vim +++ b/ftplugin/markdown.vim @@ -576,6 +576,19 @@ function! s:OpenUrlUnderCursor() endif endfunction +" We need a definition guard because we invoke 'edit' which will reload this +" script while this function is running. We must not replace it. +if !exists("*s:EditUrlUnderCursor") + function s:EditUrlUnderCursor() + let l:url = s:Markdown_GetUrlForPosition(line('.'), col('.')) + if l:url != '' + execute 'edit' l:url + else + echomsg 'The cursor is not on a link.' + endif + endfunction +endif + function! s:VersionAwareNetrwBrowseX(url) if has('patch-7.4.567') call netrw#BrowseX(a:url, 0) @@ -598,6 +611,7 @@ call MapNormVis('Markdown_MoveToPreviousSiblingHeader', 'MoveToP call MapNormVis('Markdown_MoveToParentHeader', 'MoveToParentHeader') call MapNormVis('Markdown_MoveToCurHeader', 'MoveToCurHeader') nnoremap Markdown_OpenUrlUnderCursor :call OpenUrlUnderCursor() +nnoremap Markdown_EditUrlUnderCursor :call EditUrlUnderCursor() if !get(g:, 'vim_markdown_no_default_key_mappings', 0) call MapNotHasmapto(']]', 'Markdown_MoveToNextHeader') @@ -607,6 +621,7 @@ if !get(g:, 'vim_markdown_no_default_key_mappings', 0) call MapNotHasmapto(']u', 'Markdown_MoveToParentHeader') call MapNotHasmapto(']c', 'Markdown_MoveToCurHeader') call MapNotHasmapto('gx', 'Markdown_OpenUrlUnderCursor') + call MapNotHasmapto('ge', 'Markdown_EditUrlUnderCursor') endif command! -buffer -range=% HeaderDecrease call s:HeaderDecrease(, ) -- 2.39.5