Terminal scrollback history can be difficult to navigate with standard mouse selection, especially for complex outputs or when you need to select specific portions of text. By combining Kitty terminal’s pipe capabilities with Vim’s powerful navigation, you can effortlessly browse, search, and copy content from your terminal history.

Prerequisites

Before getting started, make sure you have the following installed:

  • Kitty Terminal: A fast, feature-rich, GPU-based terminal emulator
  • Vim: The ubiquitous text editor
    • Installation: sudo pacman -S vim (Arch Linux)
    • Most Linux distributions come with Vim pre-installed or easily available in their package repositories

You’ll also need basic familiarity with Vim navigation commands for the best experience, though even Vim beginners can benefit from this setup.

The Problem with Standard Terminal Selection

Selecting text in a terminal with a mouse can be frustrating when:

  • You need to select text that spans multiple screens
  • You want to select text with precise column alignment
  • You need to search for specific content in a long output
  • You prefer keyboard-based navigation

Setting Up Vim for Kitty Scrollback

The solution combines Kitty terminal’s pipe feature with Vim to create a seamless scrollback navigation experience.

1. Configure Kitty

Add this line to your ~/.config/kitty/kitty.conf file:

map alt+e pipe @screen_scrollback overlay nvim - -c 'set filetype=scrollback' -c 'source ~/.vim/ftplugin/scrollback.vim'

This maps Alt+E to pipe the current screen’s scrollback buffer to Vim in an overlay window.

2. Create a Scrollback-Specific Vim Configuration

Create a file at ~/.vim/ftplugin/scrollback.vim with the following content:

set clipboard^=unnamedplus
set signcolumn=no
set nolist
set laststatus=0
set scrolloff=0
set nowrapscan
set relativenumber

xnoremap <buffer> <cr> "+y
nnoremap <buffer> q <cmd>q!<cr>
nnoremap <buffer> i <cmd>q!<cr>
nnoremap <buffer> I <cmd>q!<cr>
nnoremap <buffer> A <cmd>q!<cr>
nnoremap <buffer> H H
nnoremap <buffer> L L

if exists(':HideBadWhiteSpace')
    HideBadWhiteSpace
endif

call cursor(line('$'), 0)
silent! call search('\S', 'b')
silent! call search('\n*\%$')
execute "normal! \<c-y>"

How It Works

  1. Press Alt+E while in Kitty terminal
  2. Your scrollback buffer opens in Vim
  3. Use standard Vim navigation commands to move around:
    • j/k to move up/down
    • / to search for text
    • v to start visual selection
  4. In visual mode, press Enter to copy the selected text to clipboard
  5. Press q to exit and return to your terminal

Key Features of This Setup

  • Efficient Navigation: Use Vim’s powerful movement commands
  • Easy Copying: Visual select + Enter copies to clipboard
  • Quick Exit: Press ‘q’ or attempt to enter insert mode to exit
  • Relative Line Numbers: Makes jumping to specific lines easier
  • Positioning: Automatically positions cursor at the end of content

Configuration Explained

  • set clipboard^=unnamedplus: Uses the system clipboard for yanking
  • set relativenumber: Shows relative line numbers for easier navigation
  • xnoremap <buffer> <cr> "+y: Maps Enter in visual mode to copy to clipboard
  • nnoremap <buffer> q <cmd>q!<cr>: Maps ‘q’ to quit
  • nnoremap <buffer> i/I/A <cmd>q!<cr>: Exit when trying to enter insert mode
  • Cursor positioning at the end helps you start from the most recent output

This approach transforms how you interact with terminal output, making it much easier to extract and use information from your command history. Whether you’re a developer working with logs, a system administrator analyzing command outputs, or just a power user who prefers keyboard-driven workflows, this Vim+Kitty combination offers a superior scrollback navigation experience.

Try it out and watch how your terminal workflow becomes more efficient and enjoyable!

Source