2

I have, for many years, mapped the space key to page-down. I love this mapping, except when I don't love it.

Specifically, I don't love it when I accidentally press the space key, which happens more than I care to admit.

I'm toying with a workaround that so far seems OK: I use two key mappings that will restore my window after an accidental space key: One maps the space key to call winsaveview(), then press page-down; the other maps 'qqq' to call winrestview()). My vimrc has the mappings as follows..

"--- This is to restore the window view if SPACE key is accidentally pressed
map <Space> :let winrestdict = winsaveview()<CR><pagedown>
map qqq :call winrestview(winrestdict)<CR>

(I use 'qqq' simply because it is quick and easy to type)

The above solution appears to work for me, so feel free to use it if nobody has an alternative.

My question is this... is there a cleaner way (i.e., without having to define the two key mappings above) to accomplish my goal of returning the cursor (and view) to exactly the previous location after pressing a page-down.

Note: Page-down (Ctrl-F) doesn't get added to the jump list, so Ctrl-O is not a solution.

(although maybe there's a setting to add page-down to the commands that update the jump list? I couldn't find anything like this)

5
  • I would simply press Ctrl-B in this case (rsp. pageup).
    – Naumann
    Jan 25, 2018 at 19:04
  • Ctrl-B doesn't restore the cursor to its previous position. Jan 25, 2018 at 19:52
  • Double backquote? ``
    – D. Ben Knoble
    Jan 25, 2018 at 22:45
  • 1
    @DavidBenKnoble Ctrl-F isn't a jump, so `` won't do it.
    – Rich
    Jan 25, 2018 at 23:24
  • @BinaryBob I can think of a solution that doesn't require the qqq mapping, but it's certainly not "cleaner". Are you interested?
    – Rich
    Jan 25, 2018 at 23:28

2 Answers 2

2

I map navigations such as <c-d> to add the current location to the jump list before jumping so I can always go back. In your case you'd need something like:

nnoremap <space> mp`p<c-d>

then you can return after an accidental space with <c-o>.

0

I am not sure we can make this cleaner. The issue is you are pressing something accidentally. The best way to fix this is to break the bad habit. Personally, I would recommend making the "restore" mapping far more annoying, that way it helps you break the habit.

nnoremap <space> :let s:winrestdict = winsaveview()<cr><c-f>
nnoremap 1<space> :<c-u>call winrestview(s:winrestdict)<cr>

Ramp up the difficulty with with crazier and crazier key mappings: q<space>, <lt><space>, 91<space>, etc.

If you really do want to make this feel more native you could use m' to make it so double-backtick will work, but like you said this will not restore the window position exactly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.