]> git.madduck.net Git - etc/vim.git/commitdiff

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Implement "edit link in Vim" shortcut
authorMike Percy <mpercy@cloudera.com>
Wed, 27 Apr 2016 08:09:46 +0000 (01:09 -0700)
committerMike Percy <mpercy@cloudera.com>
Wed, 27 Apr 2016 08:13:40 +0000 (01:13 -0700)
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
ftplugin/markdown.vim

index 90987f2ed164596d910f2fdf00778e02b043c7a0..9c6665ef2aeb88d74e125a360cb4662444b99adb 100644 (file)
--- 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. `<Plug>Markdown_EditUrlUnderCursor`
+
+    The rules for the cursor position are the same as the `gx` command.
+
 -   `]]`: go to next header. `<Plug>Markdown_MoveToNextHeader`
 
 -   `[[`: go to previous header. Contrast with `]c`. `<Plug>Markdown_MoveToPreviousHeader`
index c2fff46b6a9c69c6d2a08de8ad48940ed9357900..daaa2afc0cc354b5e84f3e2ad0c7f2f7a131bd4a 100644 (file)
@@ -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 <sid>MapNormVis('<Plug>Markdown_MoveToPreviousSiblingHeader', '<sid>MoveToP
 call <sid>MapNormVis('<Plug>Markdown_MoveToParentHeader', '<sid>MoveToParentHeader')
 call <sid>MapNormVis('<Plug>Markdown_MoveToCurHeader', '<sid>MoveToCurHeader')
 nnoremap <Plug>Markdown_OpenUrlUnderCursor :call <sid>OpenUrlUnderCursor()<cr>
+nnoremap <Plug>Markdown_EditUrlUnderCursor :call <sid>EditUrlUnderCursor()<cr>
 
 if !get(g:, 'vim_markdown_no_default_key_mappings', 0)
     call <sid>MapNotHasmapto(']]', 'Markdown_MoveToNextHeader')
@@ -607,6 +621,7 @@ if !get(g:, 'vim_markdown_no_default_key_mappings', 0)
     call <sid>MapNotHasmapto(']u', 'Markdown_MoveToParentHeader')
     call <sid>MapNotHasmapto(']c', 'Markdown_MoveToCurHeader')
     call <sid>MapNotHasmapto('gx', 'Markdown_OpenUrlUnderCursor')
+    call <sid>MapNotHasmapto('ge', 'Markdown_EditUrlUnderCursor')
 endif
 
 command! -buffer -range=% HeaderDecrease call s:HeaderDecrease(<line1>, <line2>)