VIM Screenshot Utility

I need to write a report for a school computer lab project with screenshots of the steps I took. Instead of working on the homework I wrote a small script and macro for Vim which takes a screenshot and inserts the screenshot file name into the file you’re editing.

After taking the screenshot, it is displayed with feh so you can verify that you captured what you need.

You will need feh and imagemagick installed.

Screenshot Script

Save this script somewhere in your path as and make it executable.

# Take a screenshot in the same directory as a given path
# Waits 4 second before taking screenshot.
# Displays it with feh after taking screenshot,
# echoes file name afterwards
# Used for adding screenshots to vim
cd $(dirname $1)
file=$(pwd)/$(date +%s).jpg

if [[ $2 =~ ^[0-9]+$ ]]; then
  sleep $2
  sleep 4

import -window root $file
feh $file 2>&1
echo $file

The VIM Macro

If you add this VIM macro to your .vimrc you can take a screenshot at any time by pressing @t.

let @t=':read! %^M'

NOTE: The ^M is a control character. You won’t be able to copy and paste it. While in insert mode you need to press and hold ctrl-V, then press the letter m.

How it works:

:read!  — runs an external command and puts the results into the current buffer % — Passes the current file (%) to (the script above). The script above uses the path of the file you’re editing to determine where to save the screenshot, then prints the saved screenshot as output so that it ends up in your file.


This entry was posted in Computers, Programming and tagged , , . Bookmark the permalink.

One Response to VIM Screenshot Utility

  1. Caroline says:

    I will enjoy this to the utmost extent that I can! I like how the code starts wide at the top and gets skinny at the bottom. And the pound signs are lovely. And mixing first and second persons gives it a creative, surprising touch. Well done! (Sorry I am computer illiterate!)

Leave a Reply

Your email address will not be published. Required fields are marked *