How to Maintain Visible Context When Scrolling to EOF in Vim/vi Using CTRL-F


3 views

Many veteran vi users migrating to Vim notice an important behavioral difference when using CTRL-F (forward scroll) to navigate files. In classic vi, when scrolling reaches EOF (End Of File), the editor maintains several lines of visible context above the final line. However, Vim's default behavior places the last line at the top of the screen, obscuring previous context.

This difference becomes particularly noticeable when:

  • Reviewing log file tails
  • Examining stack traces
  • Debugging code near file endings

Losing visible context forces additional scrolling maneuvers, interrupting workflow efficiency.

The key to replicating vi's behavior lies in Vim's scrolloff option:

" Add to your .vimrc or execute directly:
set scrolloff=10

This maintains 10 lines of context when scrolling near file boundaries. For vi-like behavior, values between 5-15 work well.

For different use cases:

" General programming (balanced context)
set scrolloff=7

" Log file analysis (more context needed)
set scrolloff=15

" Terminal-specific adjustment (for Putty)
if &term =~ "xterm"
    set scrolloff=12
endif

For granular control:

augroup ScrollSettings
    autocmd!
    autocmd FileType python setlocal scrolloff=8
    autocmd FileType javascript setlocal scrolloff=5
    autocmd FileType log setlocal scrolloff=20
augroup END

To confirm the setting is active:

:echo &scrolloff

If experiencing issues in terminal sessions (like Putty), ensure:

  • Terminal type is correctly set (set term=xterm)
  • No conflicting plugins override the setting
  • Syntax highlighting isn't interfering

When working with large files in terminal-based editors, the scroll behavior at end-of-file (EOF) can significantly impact workflow efficiency. Traditional Vi maintains a buffer of ~15 lines when reaching EOF with CTRL-F, while Vim by default snaps the last line to the top of the screen. This difference becomes particularly noticeable when using terminal emulators like Putty for remote access.

Add this to your .vimrc:

if !has('gui_running')
    set scrolloff=10
    set nostartofline
endif

scrolloff: This maintains 10 lines of context (adjust the number as needed). The if !has('gui_running') wrapper ensures the setting only applies to terminal sessions.

nostartofline: Prevents the cursor from moving to the first non-blank character when scrolling, which better matches Vi's behavior.

For users who need more precise control:

augroup TerminalScrollSettings
    autocmd!
    autocmd BufEnter * if &scrolloff < 10 | set scrolloff=10 | endif
    autocmd VimEnter * set nostartofline
augroup END

To verify your settings, open a large file and try these commands:

:set scrolloff?
:set startofline?

For Putty users, ensure these additional settings are configured:

set ttyfast
set lazyredraw

These optimize screen redrawing in terminal environments.

If you prefer keeping default settings but want Vi-like behavior only for CTRL-F:

nnoremap  10

This mapping automatically scrolls back up 10 lines after each CTRL-F.

If the solution doesn't work as expected:

  1. Check for conflicting plugins with :scriptnames
  2. Verify terminal capabilities with :set termcap
  3. Test in minimal vim mode using vim -u NONE