Terminal input offset incorrect after output with no trailing newline and then cycling up through commands
Affects | Status | Importance | Assigned to | Milestone | |
---|---|---|---|---|---|
gnome-terminal (Ubuntu) |
New
|
Undecided
|
Unassigned |
Bug Description
Steps to recreate problem:
1. Run a very long command, the command should be wider than the terminal width so it wraps round to the next line. For this example, run a long echo command, such as:
echo "joi fowiejf fsweiofj woifj weiof weif wioejf weif woifjw ofwioe fdwiojf wfw fiowejfeiiiiiii
2. Run program that outputs to stdout without a trailing newline, e.g.
printf "oiwjgoiwgoiwoi
So your terminal should look like:
oiwjgoiwgoiwoia
3. Use the up arrow key to go back to the long command you entered
4. Press the home button. The cursor goes to the wrong location, behind the start of the actual command and the start of where the command would be had it not been for the printf command.
Then move the cursor to just before the echo statement, and type less to change "echo" to "lessecho" (a version of echo which expands meta characters). You should notice what the problem is now, the location your cursor appears to be at is not the actual location in the command, but where it would be if the printf command had not just been run.
This is annoying because it can cause you to incorrectly modify old commands.
ProblemType: Bug
DistroRelease: Ubuntu 14.04
Package: gnome-terminal 3.6.2-0ubuntu1
ProcVersionSign
Uname: Linux 3.13.0-44-generic x86_64
ApportVersion: 2.14.1-0ubuntu3.6
Architecture: amd64
CurrentDesktop: Unity
Date: Sat Feb 7 20:04:37 2015
InstallationDate: Installed on 2015-01-18 (20 days ago)
InstallationMedia: Ubuntu 14.04.1 LTS "Trusty Tahr" - Release amd64 (20140722.2)
SourcePackage: gnome-terminal
UpgradeStatus: No upgrade log present (probably fresh install)
This is not a gnome-terminal bug, you'll see the same behavior in every terminal.
The problem is that there's no reliable way of querying where the cursor is, so bash assumes it's in the 1st column when it prints the prompt and edits the command line. If it's not there, screen corruption is bound to happen.
I recomment you to accept this and get used to pressing Enter if a command doesn't terminate with a newline -- or if it's one of your commands then alter it accordingly.
One trick I've seen is: from the beginning of the prompt (or maybe using the PROMPT_COMMAND variable) emit a widthful of spaces, followed by a carriage return (but no newine). If the cursor was in the 1st column then it will stay in the same line and this operation will basically have no effect (well, it will fill up the line with spaces which causes different behavior when copy-pasting; after the carriage return you might also output \e[K to remove them). If the cursor was anywhere else than in the first column then it overflows to the next line and then moves back the cursor to the beginning of the next line. So effectively this is a "print a newline unless the cursor is in the 1st column". (I've seen it in a variant where the first character printed is not a space but an inverse-colored % symbol, giving you an obvious clue that the final newline was not printed by the app.)