How to clear the screen after exiting less, man and vim - vim

When I exit from the man page, less and vim the screen doesn't clear. I don't mean the clear command rather I want to see the previous screen that I entered before the man or less.
There is a less -X (--no-init) command that doesn't clear the screen after exiting, but there are not any aliases for the less command on my system and I want the inverse of this option.
My terminal picture:
As it clear, I can see the less output even after I exit from it.

My problem was solved by removing the TERM environment variable in the .bashrc.

Related

Save and restore terminal content

I am writing automation scripts (perl/bash). Many of them benefit from some basic terminal GUI. I figured I'd use standard ANSI sequences for basic drawing. Before drawing in terminal I do clear but doing that I lose some terminal command history. I want to be able to restore terminal command history when my program exists. Many terminal programs (e.g. less, man, vim, htop, nmon, whiptail, dialog etc) do exactly that. All of them restore terminal window bringing the user back to where he was prior to calling the program with all the history of commands previously executed.
To be honest I don't even know where to start searching. Is it a command from curses library? Is it an ANSI escape sequence? Should I mess with tty? I am stuck and any pointers would be really helpful.
EDIT: I'd like to clarify that I am not really asking "how to use the alternative screen". I am looking for a way to preserve terminal command history. One possible answer to my question could be "use alternative screen". The question "what is alternative screen and how to use it" is a different question which in turn already has answers posted elsewhere. Thanks :)
You should use the alternate screen terminal capability. See
Using the "alternate screen" in a bash script
An answer to "how to use the alternate screen":
This example should illustrate:
#!/bin/sh
: <<desc
Shows the top of /etc/passwd on the terminal for 1 second
and then restores the terminal to exactly how it was
desc
tput smcup #save previous state
head -n$(tput lines) /etc/passwd #get a screenful of lines
sleep 1
tput rmcup #restore previous state
This'll only work on a terminal has the smcup and rmcup capabilities (e.g., not on Linux console (=a virtual console)).
Terminal capabilities can be inspected with infocmp.
On a terminal that doesn't support it, my tput smcup simply return an exit status of 1 without outputting the escape sequence.
Note:
If you intend to redirect the output, you might want to write the escape sequences directly to /dev/tty so as to not dirty your stdout with them:
exec 3>&1 #save old stdout
exec 1>/dev/tty #write directly to terminal by default
#...
cat /etc/passwd >&3 #write actual intended output to the original stdout
#...

In a bash script, print command (as a suggestion) on the prompt after the script exit

I'd like to write a shell script that can suggest a command by printing it after the command line prompt.
Then, after the scrip exit, the user would only have to press the [enter] key to run this suggested command.
Given that I'm new to bash, I don't even know if it is possible to do this. I thought about using a copy-past command, moving the cursor, use the $PS1 variable, without finding a suitable way..
Does anyone have any clues on how such feature could be implemented ?
Following the suggestion of Landen in the comments (thanks a lot!), I've been able to produce a workaround for my problem.
This workaround needs the xautomation package (xte command, emulating key pressed), is not very robust, and may depends on the keyboard layout handling of xautomation, and system shortcuts. For example, I had to change the shortcut for the unity HUD.
But given that my command is very fast, and that is is mostly for personal use, this solution fits perfeclty my needs:
COMMAND_PASSED='sudo apt-get update'
# Displays the content of $COMMAND_PASSED on the next prompt
xte "str $COMMAND_PASSED"
sleep 0.1
tput cub ${#COMMAND_PASSED}
sleep 0.1 and tput cub ${#COMMAND_PASSED} commands are needed to prevent the keys from being also displayed before the command prompt.
sleep 0.1 makes the whole command to be printed before the prompt.
tput cub ${#COMMAND_PASSED} move the cursor backward to make sure that all unnecessary prints are erased.
Thanks everyone!

How to set TERM environment variable for linux shell

I've got very odd problem when I set export TERM=xterm-256color in ~/.bash_profile. When I try to run nano or emacs I get the following errors.
nano:
.rror opening terminal: xterm-256color
emacs:
is not defined.type xterm-256color
If that is not the actual type of terminal you have,
use the Bourne shell command `TERM=... export TERM' (C-shell:
`setenv TERM ...') to specify the correct type. It may be necessary
to do `unset TERMINFO' (C-shell: `unsetenv TERMINFO') as well.
If I manually enter the following into the shell it works
export TERM=xterm-256color
I'm stumped.
Looks like you have DOS line feeds in your .bash_profile. Don't edit files on Windows, and/or use a proper tool to copy them to your Linux system.
Better yet, get rid of Windows.
In more detail, you probably can't see it, but the erroneous line actually reads
export TERM=xterm-256color^M
where ^M is a literal DOS carriage return.
Like #EtanReisner mentions in a comment, you should not be hard-coding this value in your login files, anyway. Linux tries very hard to set it to a sane value depending on things like which terminal you are actually using and how you are connected. At most, you might want to override a particular value which the login process often chooses but which is not to your liking. Let's say you want to change to xterm-256color iff the value is xterm:
case $TERM in xterm) TERM=xterm-256color;; esac
This is not a programming question and yet an extremely common question on StackOverflow. Please google before asking.

File list by ls are misaligned

I catted a binary file and hit Ctrl-Z to stop it. Now ls results are misaligned. What did it happen and how could I have them listed correctly again ?
Type reset to reset your terminal, and press Enter. Then maybe press Ctrl+L to clear the screen. You should be back to normal.
Oh and by the way, that binary cat you ran, when you pressed Ctrl-Z that just suspended it, it's probably still running. Run jobs to see the list of jobs, and if it's there, say job number 2, do kill %2 (or whichever job number).
When you catted the binary file, your terminal probably inadvertently interpreted some of its data as control sequences and tried to execute them, screwing its properties and state.
You can either kill the terminal altogether (quit it if you're in a GUI, or relog if you're on a tty), or use another control sequence to reset the terminal. echo -e \\033c should do the trick. Some systems also have a reset builtin/command, which accomplishes the same thing.

undelete the deleted command in bash

If you have written a really long command, say cd /very/long/path, and then you do ctrl+c or ctrl+u (if the cursor is at the end), and then you realise that you want the command back, is there any way to get the full line back without re-typing. Is there any trick to change .bashrc so that bash_history keep track of keys pressed on the shell and not just after the enter is hit.
I have answered a question at In bash, how does one clear the current input? and realised if we have some option like this it would be very helpful.
To undo, use either
Ctrl+X, Ctrl+U; or
Ctrl+_ (underscore).
See bind -P for a full list of keybindings in bash.

Resources