Home Vim How To Display Undo And Redo Timestamps In Vim Status Bar Using vim-airline

How To Display Undo And Redo Timestamps In Vim Status Bar Using vim-airline

Track Undo and Redo Operations in Vim Editor with vim-airline Plugin

By sk
320 views 7 mins read

Vim is a powerful, keyboard-driven and highly customizable text editor. But by default, it offers little visibility into your editing history. If you've ever pressed u or <Ctrl-r> and wondered what exactly changed, and when, you're not alone. Thankfully, with a few simple customizations to your .vimrc file and the help of the vim-airline plugin, you can display real-time undo and redo timestamps directly in your Vim status bar.

In this tutorial, we will discuss how to:

  • Count and display the number of undo and redo statistics
  • Show the last time an undo or redo was triggered
  • Integrate these metrics into a dynamic Vim status line using vim-airline

Why Track Undo and Redo in Vim?

Vim has built-in support for undo (u) and redo (<Ctrl-r>), but it doesn’t show any feedback by default. You press a key, something changes, but there's no timestamp, no counter, and no record of what happened.

Tracking the undo and redo actions helps in a few key ways:

  • Awareness: Know exactly when the last change was reverted.
  • Control: See how many times you've undone or redone.
  • Feedback: Get confirmation that your undo/redo command did something.

By setting up vim-airline to display undo and redo timestamps, you can easily keep track of when changes were undone/redone and how many times you’ve undone/redone actions.

In the following steps, we are going to learn how to integrate this functionality into our Vim setup.

Prerequisites

Before proceeding with this tutorial, make sure you have the following:

  • Vim or Neovim installed on your system.
  • vim-plug or vundle or any other plugin manager for easy installation of Vim plugins.
  • A basic understanding of how to edit Vim's configuration files (i.e., .vimrc or init.vim).

Install vim-airline Plugin

vim-airline is a lightweight and easy-to-use status line plugin for Vim. It provides a beautifully customizable status bar that integrates with other Vim plugins seamlessly.

To begin, let's install vim-airline.

1. Open the configuration file:

For Vim:

vim ~/.vimrc

For Neovim:

nvim ~/.config/nvim/init.vim

2. Add the vim-plug plugin setup:

Inside the file, add the following lines:

call plug#begin('~/.vim/plugged')
Plug 'vim-airline/vim-airline'
call plug#end()

These lines tell vim-plug:

  • Start loading plugins (plug#begin).
  • Load vim-airline from GitHub.
  • Stop loading plugins (plug#end).

Save the file and close it by typing :wq and hitting the ENTER key.

3. Install the plugin

After saving the file, open Vim (or Neovim) and run:

:PlugInstall

Once installed, vim-airline will automatically take over the status bar in Vim.

Here is how the status bar looks after installing and enabling vim-airline:

Vim Status bar
Vim Status bar

As you can see, vim-airline plugin has added a neat status bar in the Vim editor.

By default, vim-airline displays information like:

  • Section A: Vim mode (e.g., NORMAL, INSERT)
  • Section B: Environment status (VCS information)
  • Section C: File name and flags (e.g., modified +, readonly RO)
  • Section X: Filetype (vim)
  • Section Y: File encoding
  • Section Z: Current time or line info

Display Undo and Redo Timestamps in Vim using vim-airline

Open your .vimrc or init.vim file:

vim .vimrc

Add the following lines at the end:

" ----------------------------
" Undo/Redo tracking setup
" ----------------------------
let g:undo_count = 0
let g:redo_count = 0
let g:last_undo_time = ""

" Map 'u' to update undo count and timestamp
nnoremap <silent> u :let g:undo_count += 1 \| let g:last_undo_time = strftime("%Y-%m-%d %H:%M:%S") \| normal! u<CR>

" Map Ctrl-r to update redo count and timestamp
nnoremap <silent> <C-r> :let g:redo_count += 1 \| let g:last_undo_time = strftime("%Y-%m-%d %H:%M:%S") \| redo<CR>

" Display undo and redo stats in airline section X
let g:airline_section_x = '%{g:last_undo_time != "" ? g:last_undo_time . " (undos:" . g:undo_count . ", redos:" . g:redo_count . ")" : strftime("%H:%M:%S")}'

Type :wq and press ENTER to save the file and close it.

From now on, if undo or redo has happened, it will display something like the following in the status bar:

2025-04-29 16:33:10 (undos: 3, redos: 1)

If not, it falls back to showing the current time.

Testing Your Configuration

To test, just open any file. In the insert mode, type some text on several lines.

This is first line.
This is second line.
This is third line.

Then exit insert mode and press u to undo a line, and <Ctrl-r> to redo.

Watch the vim-airline status line update with new counts and timestamps. In the right section of the status bar, you should see a timestamp like:

2025-04-29 17:00:17 (undos:2, redos:2)
Display Undo and Redo Timestamps in Vim
Display Undo and Redo Timestamps in Vim

You will also see the undo/redo counters go up as you interact with the file.

Advanced Tips and Customizations

Leader mappings:

Instead of using <Ctrl-r>, you can map redo to something like <Leader>r.

nnoremap <Leader>r <C-r>

Persistent undo:

Enable persistent undo with:

set undofile
set undodir=~/.vim/undodir

Logging:

To persist tracking info between sessions, you could write the counts to a file on VimExit.

Troubleshooting

If the timestamp isn't showing up as expected, check the following:

  • Ensure that you are using Vim 8 or higher (since strftime feature is available in Vim 8+).
  • Check that vim-airline is correctly installed: Use :PlugStatus to see if the plugin is loaded.

My .vimrc Configuration

Here's my complete .vimrc file configuration. You can simply copy/paste the whole or part of the content to your .vimrc file.

" ----------------------------
" Basic settings
" ----------------------------
set nocompatible

" ----------------------------
" vim-plug plugin manager setup
" ----------------------------
call plug#begin('~/.vim/plugged')

" vim-airline status bar
Plug 'vim-airline/vim-airline'

call plug#end()
filetype plugin indent on

" ----------------------------
" Undo/Redo tracking setup
" ----------------------------
let g:undo_count = 0
let g:redo_count = 0
let g:last_undo_time = ""

" Map 'u' to update undo count and timestamp
nnoremap <silent> u :let g:undo_count += 1 \| let g:last_undo_time = strftime("%Y-%m-%d %H:%M:%S") \| normal! u<CR>

" Map Ctrl-r to update redo count and timestamp
nnoremap <silent> <C-r> :let g:redo_count += 1 \| let g:last_undo_time = strftime("%Y-%m-%d %H:%M:%S") \| redo<CR>

" Display undo and redo stats in airline section X
let g:airline_section_x = '%{g:last_undo_time != "" ? g:last_undo_time . " (undos:" . g:undo_count . ", redos:" . g:redo_count . ")" : strftime("%H:%M:%S")}'

Breakdown of my .vimrc

Here's the breakdown of what each lines does in my .vimrc configuration:

Basic Vim Setup

set nocompatible
  • Disables compatibility with the very old "vi" editor.
  • Enables all Vim-specific features and makes sure plugins behave correctly.

Plugin Manager Setup (vim-plug)

call plug#begin('~/.vim/plugged')
  • Initializes plugin loading with vim-plug.
  • ~/.vim/plugged is the directory where your plugins will be stored.
Plug 'vim-airline/vim-airline'
  • Loads the vim-airline plugin, which provides a lean, customizable status bar.
call plug#end()
  • Ends the plugin block. All plugins should be listed between plug#begin() and plug#end().
filetype plugin indent on

It Enables:

  • Filetype detection
  • Loading of filetype-specific plugins
  • Automatic indentation for recognized filetypes

Undo/Redo Tracking Variables

let g:undo_count = 0
let g:redo_count = 0
let g:last_undo_time = ""
  • g:undo_count: Tracks how many times you press u (undo).
  • g:redo_count: Tracks how many times you press <C-r> (redo).
  • g:last_undo_time: Stores the timestamp of the last undo or redo.

Custom Key Mappings

Undo:

nnoremap <silent> u :let g:undo_count += 1 \| let g:last_undo_time = strftime("%Y-%m-%d %H:%M:%S") \| normal! u<CR>

Replaces the default u command with a custom version that:

  1. Increments undo_count
  2. Updates last_undo_time
  3. Performs the actual undo with normal! u
  4. <silent> stops the command from showing in the command area.

Redo:

nnoremap <silent> <C-r> :let g:redo_count += 1 \| let g:last_undo_time = strftime("%Y-%m-%d %H:%M:%S") \| redo<CR>
  • Similar to the undo mapping, but for redo.
  • Increments redo_count and updates the timestamp.

Airline Status Line Configuration

let g:airline_section_x = '%{g:last_undo_time != "" ? g:last_undo_time . " (undos:" . g:undo_count . ", redos:" . g:redo_count . ")" : strftime("%H:%M:%S")}'
  • Customizes the X section of the vim-airline status bar.
  • If last_undo_time is set, it shows:
    YYYY-MM-DD HH:MM:SS (undos:X, redos:Y)
  • If no undos or redos yet, it just shows the current time (strftime("%H:%M:%S")).

Conclusion

With just a few lines of Vimscript and a lightweight plugin, we've added powerful visibility to Vim's editing history. You can now work smarter by tracking and displaying useful information like undo and redo timestamps in your status bar.

Tracking undo and redo operations in your status bar improves awareness, makes editing safer, and helps you work more confidently.

Whether you're learning Vim or leveling up your daily workflow, this setup gives you the insight you never knew you needed until now.

Suggested Read:

You May Also Like

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More