Skip to content

Zod Date in VIM

Posted on:March 21, 2023 at 02:15 PM

I made a VIM keybinding that inserts a valid zod datetime.

This blog uses metadata for the published date time validated by a zod schema:

import { z } from "astro:content";

export const blogSchema = z
    author: z.string().optional(),
    title: z.string(),
    postSlug: z.string().optional(),
    featured: z.boolean().optional(),
    draft: z.boolean().optional(),
    tags: z.array(z.string()).default(["others"]),
    ogImage: z.string().optional(),
    description: z.string(),

export type BlogFrontmatter = z.infer<typeof blogSchema>;

An example of the pubDatetime is this:

pubDatetime: 2023-03-21T10:05:48-04:00

This would be incredibly painful to type out by hand so I set about to find a nice way to insert this with a VIM keymap.

First, we need sane date logic from the command line. I’m on MacOS so need to install coreutils:

brew install coreutils

Check out the awesome date stuff we get in the coreutils docs!

After installed you can prefix bash commands with g to get the good stuff.

We could get fancy with all the formatting, but what we really want is the ISO string:

gdate --iso-8601=seconds

This date passes our zod validation so now we just need to wire it up to a VIM mapping:

["<leader>pd"] = "g:r!gdate --iso-8601=seconds<cr>"