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
- Installation:
sudo pacman -S kitty
(Arch Linux) - Other distros: https://sw.kovidgoyal.net/kitty/binary/
- Installation:
- 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
- Installation:
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
- Press Alt+E while in Kitty terminal
- Your scrollback buffer opens in Vim
- Use standard Vim navigation commands to move around:
j/k
to move up/down/
to search for textv
to start visual selection
- In visual mode, press Enter to copy the selected text to clipboard
- 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 yankingset relativenumber
: Shows relative line numbers for easier navigationxnoremap <buffer> <cr> "+y
: Maps Enter in visual mode to copy to clipboardnnoremap <buffer> q <cmd>q!<cr>
: Maps ‘q’ to quitnnoremap <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!