diff --git a/cmd-attach-session.c b/cmd-attach-session.c
index e4c0b23..26f3d8d 100644
--- a/cmd-attach-session.c
+++ b/cmd-attach-session.c
@@ -47,24 +47,73 @@ cmd_attach_session(struct cmd_q *cmdq, const char *tflag, int dflag, int rflag,
 {
 	struct session		*s;
 	struct client		*c;
+	struct winlink		*wl;
+	struct window		*w;
+	struct window_pane	*wp;
 	const char		*update;
 	char			*cause;
 	u_int			 i;
 	int			 fd;
 	struct format_tree	*ft;
-	char			*cp;
+	char			*cp, *tmptflag, *tptr;
 
 	if (RB_EMPTY(&sessions)) {
 		cmdq_error(cmdq, "no sessions");
 		return (CMD_RETURN_ERROR);
 	}
 
-	if ((s = cmd_find_session(cmdq, tflag, 1)) == NULL)
+	tmptflag = tptr = NULL;
+	if (tflag != NULL) {
+		tmptflag = xstrdup(tflag);
+		if ((tptr = strchr(tmptflag, ':')) != NULL)
+			*tptr = '\0';
+	}
+
+	s = cmd_find_session(cmdq, tmptflag, 1);
+	if (tmptflag != NULL)
+		free(tmptflag);
+	if (s == NULL) 
 		return (CMD_RETURN_ERROR);
 
+	wl = NULL;
+	wp = NULL;
+	w = NULL;
+	if (tptr != NULL) {
+		wl = cmd_find_pane(cmdq, tflag, &s, &wp);
+		if (wl == NULL)
+			return (CMD_RETURN_ERROR);
+		else
+			w = wp->window;
+
+	} else {
+		if (tflag != NULL) {
+			if (*tflag == '%') {
+				wp = cmd_lookup_paneid(tflag);
+				if (wp != NULL)
+					w = wp->window;
+
+			} else if (*tflag == '@') {
+				w = cmd_lookup_windowid(tflag);
+			}
+
+			if (w != NULL) {
+				if ((wl = winlink_find_by_window(&s->windows, w)) == NULL)
+					return (CMD_RETURN_ERROR);
+			}
+		}
+	}
+
+
 	if (cmdq->client == NULL)
 		return (CMD_RETURN_NORMAL);
 
+	if (wl != NULL) {
+		if (wp != NULL)
+			window_set_active_pane(w, wp);
+
+		session_set_current(s, wl);
+	}
+
 	if (cmdq->client->session != NULL) {
 		if (dflag) {
 			/*
diff --git a/tmux.h b/tmux.h
index dbc2c49..5a83479 100644
--- a/tmux.h
+++ b/tmux.h
@@ -1732,6 +1732,8 @@ int		 cmd_find_index(struct cmd_q *, const char *,
 struct winlink	*cmd_find_pane(struct cmd_q *, const char *, struct session **,
 		     struct window_pane **);
 char		*cmd_template_replace(const char *, const char *, int);
+struct window	*cmd_lookup_windowid(const char *);
+struct window_pane	*cmd_lookup_paneid(const char *);
 extern const struct cmd_entry *cmd_table[];
 extern const struct cmd_entry cmd_attach_session_entry;
 extern const struct cmd_entry cmd_bind_key_entry;
