aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2017-10-30 23:21:54 +1300
committerTom Ryder <tom@sanctum.geek.nz>2017-10-30 23:25:25 +1300
commitadff4425cd7046004e8a06e3e99284405529088a (patch)
tree34e2c72671658d8102b0cea77b70df8d74c30139
parentAdd some comments to 'formatoptions' switching (diff)
downloaddotfiles-adff4425cd7046004e8a06e3e99284405529088a.tar.gz
dotfiles-adff4425cd7046004e8a06e3e99284405529088a.zip
Reimplement stable normal-mode J join mapping
This is a tidier method of preserving the cursor position after a normal-mode join that doesn't involve wiping away a mark, though I don't use those too often anyway. It still works with a preceding count via the `v:count1` variable, with an accidental feature: this joins the *next* v:count1 lines, as opposed to joining a *total* of v:count1 lines counting the current one. The latter is what Vim does, but the former is what I'd actually expect, thinking of it as a "repeated operation", so I'm going to leave it this way.
-rw-r--r--vim/config/join.vim28
1 files changed, 24 insertions, 4 deletions
diff --git a/vim/config/join.vim b/vim/config/join.vim
index 5f75d89b..7365c85e 100644
--- a/vim/config/join.vim
+++ b/vim/config/join.vim
@@ -1,4 +1,24 @@
-" Don't jump my screen around when I join lines, keep my cursor in the same
-" place; this is done by dropping a mark first and then immediately returning
-" to it; note that it wipes out your z mark, if you happen to use it
-nnoremap J mzJ`z
+
+" Keep my cursor in place when I join lines
+if has('eval')
+
+ " Declare function
+ function! s:StableNormalJoin()
+
+ " Save current cursor position
+ let l:lc = line('.')
+ let l:cc = col('.')
+
+ " Build and execute join command
+ let l:command = '.,+' . v:count1 . 'join'
+ execute l:command
+
+ " Restore cursor position
+ call cursor(l:lc, l:cc)
+
+ endfunction
+
+ " Remap J to the above function
+ nnoremap <silent> J :<C-U>call <SID>StableNormalJoin()<CR>
+
+endif