This patch also subsumes the recent change to fix bug#42862. While this seems to work well for file-name-history and buffer-name-history, whose elements typically don't contain a line feed character, that's not the case for e.g. extended-command-history: the above changes don't actually test that the history element consists of a *single* line longer than window-width, but only whether the *first* line of the element was longer than window-width. So if the element contains two or more such lines, as in: M-: (setq bla "A long long long long long long long long long long long long long long long value" bli "Another long long long long long long long long long long long long long long long value") moves by visual lines through the lower ones but by logical lines through the top one, which seems wrong. I tried to accommodate such cases, and came up with this: