Vim Tips Wiki
No edit summary
No edit summary
Line 71: Line 71:
 
A solution that works in insert and visual modes too:
 
A solution that works in insert and visual modes too:
   
  +
<pre>
 
nnoremap <silent> <PageUp> <C-U><C-U>
 
nnoremap <silent> <PageUp> <C-U><C-U>
 
vnoremap <silent> <PageUp> <C-U><C-U>
 
vnoremap <silent> <PageUp> <C-U><C-U>
Line 78: Line 79:
 
vnoremap <silent> <PageDown> <C-D><C-D>
 
vnoremap <silent> <PageDown> <C-D><C-D>
 
inoremap <silent> <PageDown> <C-\><C-O><C-D><C-\><C-O><C-D>
 
inoremap <silent> <PageDown> <C-\><C-O><C-D><C-\><C-O><C-D>
  +
</pre>
   
 
It assumes that "scroll" has its default value. Also there is an intermediate redraw, but that could be perceived as a feature, really.
 
It assumes that "scroll" has its default value. Also there is an intermediate redraw, but that could be perceived as a feature, really.

Revision as of 15:14, 13 October 2008

Tip 320 Printable Monobook Previous Next

created August 26, 2002 · complexity basic · author Simon "neoneye" Strandgaard · version 6.0


Borland behaviour = the cursor keeps the same xy position during pageup/down.

I'm new to Vim scripting, im sure it can be done smarter?

I read VimTip105 and it gave me a clue of how BorlandPageUp/Down could be done.

" i could'nt find any get_number_of_visible_lines function, so i made my own.
function GetNumberOfVisibleLines()
  let cur_line = line(".")
  let cur_col = virtcol(".")
  normal H
  let top_line = line(".")
  normal L
  let bot_line = line(".")
  execute "normal " . cur_line . "G"
  execute "normal " . cur_col . "|"
  return bot_line - top_line
endfunc

" noremap <PageUp> 39<C-U>:set scroll=0<CR>
function! MyPageUp()
  let visible_lines = GetNumberOfVisibleLines()
  execute "normal " . visible_lines . "\<C-U>:set scroll=0\r"
endfunction

" noremap <PageDown> 39<C-D>:set scroll=0<CR>
function! MyPageDown()
  let visible_lines = GetNumberOfVisibleLines()
  execute "normal " . visible_lines . "\<C-D>:set scroll=0\r"
endfunction

" BorlandPascal pageup/down behaviour!
" todo: when hitting top/bottom of file, then restore Y to lastY
noremap <PageUp> :call MyPageUp()<CR>
noremap <PageDown> :call MyPageDown()<CR>

Comments

For maintaining the same x coordinate, :help 'startofline'.


And CTRL-U (up), CTRL-D (down) may also be useful for what you want (half page scrolls)


A solution that I use (easier, I would say, but has a small side-effect) is this:

map <PageDown> :set scroll=0<CR>:set scroll^=2<CR>:set scroll-=1<CR><C-D>:set scroll=0<CR>
map <PageUp> :set scroll=0<CR>:set scroll^=2<CR>:set scroll-=1<CR><C-U>:set scroll=0<CR>

I found Vim's normal PgUp/PgDn behaviour weird - I think it's different from every other editor I've used and I was unable to get used to it. The above two lines are godsent!


See Combining move and scroll. You might find it useful to incorporate the improvements into this tip.


A solution that works in insert and visual modes too:

nnoremap <silent> <PageUp> <C-U><C-U>
vnoremap <silent> <PageUp> <C-U><C-U>
inoremap <silent> <PageUp> <C-\><C-O><C-U><C-\><C-O><C-U>

nnoremap <silent> <PageDown> <C-D><C-D>
vnoremap <silent> <PageDown> <C-D><C-D>
inoremap <silent> <PageDown> <C-\><C-O><C-D><C-\><C-O><C-D>

It assumes that "scroll" has its default value. Also there is an intermediate redraw, but that could be perceived as a feature, really.

I've spent a whole day trying to simulate sometheing like the <C-\><C-O> command for Visual mode (hoping for the universal solution -- a way to call any normal mode command from any mode), yet it looks like impossible if the command in question is supposed to be a custom movement command, modifying selection.