Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Version 2.0: Initial upload

  • Loading branch information...
commit 24b37cf43afb51861a7fc451870a88d03f4d344b 0 parents
Paul "Joey" Clark authored vim-scripts committed
18  README
... ...
@@ -0,0 +1,18 @@
  1
+This is a mirror of http://www.vim.org/scripts/script.php?script_id=4286
  2
+
  3
+Navigation Enhancer v2.0 by joey.neuralyte.org
  4
+
  5
+aka Retrace Your Steps
  6
+
  7
+When moving the cursor between windows, given a choice of target windows,
  8
+Vim normally uses the *cursor position* to choose the destination window.
  9
+
  10
+However I think a more intuitive approach when there may be more than one
  11
+potential target window is to pick the one which was *used most recently*.
  12
+
  13
+Or perhaps more accurately, we want the window that we *last entered from, in
  14
+that direction*.
  15
+
  16
+So now if I casually move between windows in one direction, and then in the
  17
+opposite direction, I should always return to the window I started from!
  18
+
115  plugin/navigation_enhancer.vim
... ...
@@ -0,0 +1,115 @@
  1
+" Navigation Enhancer v2.0 by joey.neuralyte.org
  2
+"
  3
+" aka Retrace Your Steps
  4
+"
  5
+" When moving the cursor between windows, given a choice of target windows,
  6
+" Vim normally uses the *cursor position* to choose the destination window.
  7
+"
  8
+" However I think a more intuitive approach when there may be more than one
  9
+" potential target window is to pick the one which was *used most recently*.
  10
+"
  11
+" Or perhaps more accurately, we want the window that we *last entered from, in
  12
+" that direction*.
  13
+"
  14
+" So now if I casually move between windows in one direction, and then in the
  15
+" opposite direction, I should always return to the window I started from!
  16
+
  17
+" BUG: When you split a window, it may cause the numbers of other windows to
  18
+" change!
  19
+
  20
+"" Override Vim's default keymaps for window navigation:
  21
+noremap  <silent> <C-W>k         :call <SID>SeekBestWindow("k","j")<Enter>
  22
+noremap  <silent> <C-W>j         :call <SID>SeekBestWindow("j","k")<Enter>
  23
+noremap  <silent> <C-W>h         :call <SID>SeekBestWindow("h","l")<Enter>
  24
+noremap  <silent> <C-W>l         :call <SID>SeekBestWindow("l","h")<Enter>
  25
+"" I also like them to work in Insert mode!
  26
+inoremap <silent> <C-W>k         <Esc>:call <SID>SeekBestWindow("k","j")<Enter>a
  27
+inoremap <silent> <C-W>j         <Esc>:call <SID>SeekBestWindow("j","k")<Enter>a
  28
+inoremap <silent> <C-W>h         <Esc>:call <SID>SeekBestWindow("h","l")<Enter>a
  29
+inoremap <silent> <C-W>l         <Esc>:call <SID>SeekBestWindow("l","h")<Enter>a
  30
+"" You may also use <C-W><Up>/<Down>/<Left>/<Right>
  31
+"" Warning: these may conflict with other plugins, e.g. windows_remember_size.vim
  32
+map  <silent> <C-W><Up>    <C-W>k
  33
+map  <silent> <C-W><Down>  <C-W>j
  34
+map  <silent> <C-W><Left>  <C-W>h
  35
+map  <silent> <C-W><Right> <C-W>l
  36
+imap <silent> <C-W><Up>    <C-W>k
  37
+imap <silent> <C-W><Down>  <C-W>j
  38
+imap <silent> <C-W><Left>  <C-W>h
  39
+imap <silent> <C-W><Right> <C-W>l
  40
+"" Also in GVim, we need to re-load joeykeymap.vim to get <C-Up> etc. calling
  41
+"" the script binds above.
  42
+
  43
+"" Or leave the defaults unchanged, and instead override my preferred shortcuts:
  44
+"" This became a pain when my compatibility mappings were intercepting these,
  45
+"" so I tried to unmap all my shortcuts.
  46
+" silent! unmap <C-Up>
  47
+" silent! unmap <C-Down>
  48
+" silent! unmap <C-Left>
  49
+" silent! unmap <C-Right>
  50
+" silent! iunmap <C-Up>
  51
+" silent! iunmap <C-Down>
  52
+" silent! iunmap <C-Left>
  53
+" silent! iunmap <C-Right>
  54
+"" Problem: We must unmap these or C-Up mapping fails in xterms.
  55
+"" But then we should re-map them for terms which recognise them only!
  56
+" silent! unmap 
  57
+" silent! unmap 
  58
+" silent! unmap 
  59
+" silent! unmap 
  60
+" noremap  <silent> <C-Up>         :call <SID>SeekBestWindow("k","j")<Enter>
  61
+" inoremap <silent> <C-Up>    <Esc>:call <SID>SeekBestWindow("k","j")<Enter>a
  62
+" noremap  <silent> <C-Down>       :call <SID>SeekBestWindow("j","k")<Enter>
  63
+" inoremap <silent> <C-Down>  <Esc>:call <SID>SeekBestWindow("j","k")<Enter>a
  64
+" noremap  <silent> <C-Left>       :call <SID>SeekBestWindow("h","l")<Enter>
  65
+" inoremap <silent> <C-Left>  <Esc>:call <SID>SeekBestWindow("h","l")<Enter>a
  66
+" noremap  <silent> <C-Right>      :call <SID>SeekBestWindow("l","h")<Enter>
  67
+" inoremap <silent> <C-Right> <Esc>:call <SID>SeekBestWindow("l","h")<Enter>a
  68
+
  69
+function! s:SeekBestWindow(realDirection,reverseDirection)
  70
+  " User has requested travel in realDirection, and reverseDirection should be
  71
+  " the opposite of that.
  72
+
  73
+  let l:startWin = winnr()
  74
+  let l:moveDone = 0
  75
+
  76
+  if exists("w:lastWinInDir_".a:realDirection)
  77
+    let l:targetWin = eval("w:lastWinInDir_".a:realDirection)
  78
+    " echo "Last window from that direction was ".l:targetWin
  79
+    if l:targetWin == l:startWin
  80
+      " Window numbers have become confused.  This move is no use!
  81
+    else
  82
+      " Go to the recommended target window
  83
+      noautocmd exec l:targetWin."wincmd w"
  84
+      " Check that this movement is still valid
  85
+      " We do that by moving back again, and seeing if we get back where we
  86
+      " started from.
  87
+      noautocmd exec "wincmd ".a:reverseDirection
  88
+      if winnr() == l:startWin
  89
+        " Yes this move is fine
  90
+        exec l:targetWin."wincmd w"
  91
+        let l:moveDone = 1
  92
+      else
  93
+        " No this move might not be valid!
  94
+        " (This could be caused by window layout having changed.)
  95
+        " Or it might be a valid move, but on the way back Vim would naturally
  96
+        " pick a different route.  In that case, travelling *this way* is
  97
+        " probably trivial, and we can let Vim do it normally.
  98
+        " echo "We may no longer reach ".l:targetWin." from here!"
  99
+        noautocmd exec l:startWin."wincmd w"
  100
+      endif
  101
+    endif
  102
+  endif
  103
+
  104
+  if !l:moveDone
  105
+    " echo "Doing normal move"
  106
+    exec "wincmd ".a:realDirection
  107
+  endif
  108
+
  109
+  " Record the route back (but not if we didn't actually move!)
  110
+  if winnr() != l:startWin
  111
+    exec "let w:lastWinInDir_".a:reverseDirection." = ".l:startWin
  112
+  endif
  113
+
  114
+endfunction
  115
+

0 notes on commit 24b37cf

Please sign in to comment.
Something went wrong with that request. Please try again.