@@ -382,11 +382,17 @@ const LiveRegion = struct {
382382 self.drawn = true;
383383 }
384384
385+ fn caretCell(self: *LiveRegion, width: usize) usize {
386+ const w = if (self.caret_line < self.widths.items.len) self.widths.items[self.caret_line] else 0;
387+ if (self.caret_cell > 0 and self.caret_cell == w and self.caret_cell % width == 0) return self.caret_cell - 1;
388+ return self.caret_cell;
389+ }
390+
385391 fn caretRow(self: *LiveRegion, width: usize) usize {
386392 var row: usize = 0;
387393 const lines = @min(self.caret_line, self.widths.items.len);
388394 for (self.widths.items[0..lines]) |cells| row += rowsForCells(cells, width);
389- return row + self.caret_cell / width;
395+ return row + self.caretCell(width) / width;
390396 }
391397
392398 fn totalRows(self: *LiveRegion, width: usize) usize {
@@ -420,7 +426,7 @@ const LiveRegion = struct {
420426 const up = if (end_row > caret_row) end_row - caret_row else 0;
421427 if (up > 0) out.appendSlice(a, std.fmt.allocPrint(a, "\x1b[{d}A", .{up}) catch "") catch {};
422428 out.appendSlice(a, "\r") catch {};
423- const col = caret_cell % width;
429+ const col = self.caretCell(width) % width;
424430 if (col > 0) out.appendSlice(a, std.fmt.allocPrint(a, "\x1b[{d}C", .{col}) catch "") catch {};
425431 return out.items;
426432 }