For even more dynamic length output that moosaka's answer (for example, if you have few very long usernames, but which are very rarely used and you don't want screen wasted most of the time), you can use:
ps -eo user:$(ps axho uid | sort -u | xargs getent passwd | cut -f1 -d: | wc -L),pid,ppid,c,stime,tname,time,cmd
It will make the lenght of the username column just as long as longest username of currently running process. (Note that it is not bulletproof however, and if new process with longer username starts in the split second while command is running, you might still get a number displayed. But for 99.99% of the time it's much nicer output)
Explanation: $(ps axho uid [...] | wc -L)
calculates maximum username length of CURRENTLY running process, and then we execute normal ps with that length of username
Alternatively, if you want ps to look like usual for short usernames (<=8 chars), and do not mind for few long usernames in output to be misaligned with headers, you can do something like:
ps ax -o user:40,pid,ppid,c,stime,tname,time,cmd | perl -pe 'if (/^(\S+)/ and length $1 > 8) {s/^(\S+)\s+/$1 /} else { s/^(.{9})\s+/$1/}'
what that does is make output username column very long (-o user:40
), and then postprocesses the output so long usernames (length $1 > 8
) have just one space between them and next column, and short usernames (else {
) are trimmed back to default (up to 8 chars username, and the rest up to 9th character are spaces)