From adff4425cd7046004e8a06e3e99284405529088a Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Mon, 30 Oct 2017 23:21:54 +1300 Subject: 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. --- vim/config/join.vim | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) (limited to 'vim') 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 J :call StableNormalJoin() + +endif -- cgit v1.2.3