--- a/format.c
+++ b/format.c
@@ -34,9 +34,10 @@
  * string.
  */
 
-int	format_replace(struct format_tree *, const char *, size_t, char **,
-	    size_t *, size_t *);
-void	format_window_pane_tabs(struct format_tree *, struct window_pane *);
+int	 format_replace(struct format_tree *, const char *, size_t, char **,
+	     size_t *, size_t *);
+char	*format_get_command(struct window_pane *);
+void	 format_window_pane_tabs(struct format_tree *, struct window_pane *);
 
 /* Format key-value replacement entry. */
 RB_GENERATE(format_tree, format_entry, entry, format_cmp);
@@ -348,6 +349,21 @@
 	return (buf);
 }
 
+/* Get command name for format. */
+char *
+format_get_command(struct window_pane *wp)
+{
+	char	*cmd;
+
+	cmd = osdep_get_name(wp->fd, wp->tty);
+	if (cmd == NULL || *cmd == '\0') {
+		cmd = wp->cmd;
+		if (cmd == NULL || *cmd == '\0')
+			cmd = wp->shell;
+	}
+	return (parse_window_name(cmd));
+}
+
 /* Set default format keys for a session. */
 void
 format_session(struct format_tree *ft, struct session *s)
@@ -427,25 +443,38 @@
 		format_add(ft, "client_last_session", "%s", s->name);
 }
 
-/* Set default format keys for a winlink. */
-void
-format_winlink(struct format_tree *ft, struct session *s, struct winlink *wl)
-{
-	struct window	*w = wl->window;
-	char		*layout, *flags;
+/* Set default format keys for a window. */
+void
+format_window(struct format_tree *ft, struct window *w)
+{
+	char	*layout;
 
 	layout = layout_dump(w);
-	flags = window_printable_flags(s, wl);
 
 	format_add(ft, "window_id", "@%u", w->id);
-	format_add(ft, "window_index", "%d", wl->idx);
 	format_add(ft, "window_name", "%s", w->name);
 	format_add(ft, "window_width", "%u", w->sx);
 	format_add(ft, "window_height", "%u", w->sy);
+	format_add(ft, "window_layout", "%s", layout);
+	format_add(ft, "window_panes", "%u", window_count_panes(w));
+
+	free(layout);
+}
+
+/* Set default format keys for a winlink. */
+void
+format_winlink(struct format_tree *ft, struct session *s, struct winlink *wl)
+{
+	struct window	*w = wl->window;
+	char		*flags;
+
+	flags = window_printable_flags(s, wl);
+
+	format_window(ft, w);
+
+	format_add(ft, "window_index", "%d", wl->idx);
 	format_add(ft, "window_flags", "%s", flags);
-	format_add(ft, "window_layout", "%s", layout);
 	format_add(ft, "window_active", "%d", wl == s->curw);
-	format_add(ft, "window_panes", "%u", window_count_panes(w));
 
 	format_add(ft, "window_bell_flag", "%u",
 	    !!(wl->flags & WINLINK_BELL));
@@ -456,8 +485,8 @@
 	format_add(ft, "window_silence_flag", "%u",
 	    !!(wl->flags & WINLINK_SILENCE));
 
+
 	free(flags);
-	free(layout);
 }
 
 /* Add window pane tabs. */
@@ -527,7 +556,7 @@
 		format_add(ft, "pane_start_path", "%s", wp->cwd);
 	if ((cwd = osdep_get_cwd(wp->fd)) != NULL)
 		format_add(ft, "pane_current_path", "%s", cwd);
-	if ((cmd = osdep_get_name(wp->fd, wp->tty)) != NULL) {
+	if ((cmd = format_get_command(wp)) != NULL) {
 		format_add(ft, "pane_current_command", "%s", cmd);
 		free(cmd);
 	}