Scrolling not disabled in alternate screen mode - vim

So I have this minimal program in NIM which just enters alternate screen mode, prints a few numbers and exits the alternate screen mode.
import os
echo "\e[?1049h"
echo "\nAlternate screen works but scrolling up is allowed, no keys sent."
for i in 0..4:
echo $i
sleep(1000)
echo "\e[?1049l"
My problem is that scrolling in alternate screen mode is still enabled, unlike with vim for example. I have checked my settings and tried unchecking to see that it then correctly behaves with vim as it does with my program and it does:
So it seems as though vim is sending some ansi escape sequences that my program does not, i.e. is there something I need to send to tell the terminal I want it to send arrow keys instead of scrolling the screen? Since the escape sequences I sent should (according to other stackoverflow answers) put me into alternate screen mode (and it does basically) the option in Mac OS Terminal as seen in the picture should take effect but for some reason it doesn't while it works with vim, which leaves me quite clueless as to what kind of black magic is going on here.
Any help is greatly appreciated :)
For reference:
> echo $TERM
xterm-256color

Related

How do I prevent strange escape chars from appearing on prompt

I am using Cygwin on windows 7
mintty 2.3.6 (x86_64-pc-cygwin)
And randomly a [28~ will appear on my prompt line. Sometimes it even appears while I am in the middle of typing a command. And often it appears even when the terminal window doesn't have focus.
This is really hard to google for, because google just sees the 28. I would appreciate suggestions on how to improve my google-fu to search for the specific sequence.
This can be caused by an application such as Caffeine, which stops your screensaver by pressing a key periodically (like F15 which has ansi sequence \[[28~) . You can stop the program or add an entry in your ~/.inputrc to ignore the sequence. in ~/.inputrc add the line:
"\e[28~":""
to remap F15 to nothing in the terminal.
For the record, there are two other ways to fix this:
Prevent F15 from being interpreted on the mintty side: Add the following line to your mintty config file (e.g. ~/.minttyrc). It tells mintty to ignore F15 by itself or with the Ctrl modifier, which does the trick for me (on my machine, it would produce ~ by itself, and with Ctrl, ;5~):
KeyFunctions=F15:void;C+F15:void
Prevent Caffeine from sending F15 by running it with the -useshift option.

What mechanism allows ViM to temporarily overwrite the entire console?

When you enter vim, it "clears" the screen. Upon exiting, it "restores" the original contents.
I understand one can use \x1b[2J to clear the console and reset the cursor position, but this will overwrite terminal contents.
I assume Vim uses ncurses under the hood, in which case I suppose the better question is how ncurses does this, but how is it done?
Regarding the answer by #Keith Thompson — not exactly:
vim does not use the screen optimization of ncurses which sends smcup and rmcup automatically. Rather, it is a termcap application. It follows a convention used by most (not all) termcap applications. There are some implementations of vi which do not for instance (on IRIX64 perhaps).
as for "most terminals" — actually, xterm look-alikes are a small part of the terminal database (even counting variations, less than 10%). Rephrase that to something like "the most common terminal emulators on Linux.
the terminal does not save and restore the screen contents. Instead, it switches between two screens (in xterm's documentation "normal" and "alternate"). In xterm for instance, one can always switch between the two using a menu entry. The xterm FAQ Why doesn't the screen clear when running vi? gives more detail.
for better context, note that smcup is an (obscure) abbreviation for set-mode-cursor-positioning, or start cursor-positioning mode. (also cursor-addressing). The r in rmcup means "reset" (and m means "mode"). set/reset have different connotations from save/restore; with the latter the user is led to believe that the values can be stacked up.
Most terminal emulators are able to save and restore the contents of the screen.
The terminfo codes for this are smcup to enter full-screen mode and rmcup to leave it. (The older termcap codes are ti and te.)
If these capabilities are enabled in the terminfo database, any program that uses ncurses will print the smcup string on entry, and the rmcup string on exit.
On the system I'm using at the moment, the strings are (with \E representing the Escape character):
smcup: \E7\E[?1;47h
rmcup: \E[2J\E[?1;47l\E8
This restores the previous contents of the screen as well as the cursor position.
The specific meanings of the sequences (for xterm) are documented here:
smcup:
\E7 Save Cursor
\E[?1;47h Application Cursor Keys; Use Alternate Screen Buffer
rmcup:
\E[2J Erase screen
\E[?1;47l Application Cursor Keys; Use Normal Screen Buffer
\E8 Restore Cursor
(This assumes I'm understanding the use of the semicolon correctly; I'm not 100% sure of that.)

How to scroll within vim's shell command output?

When I enter a shell command via vim -- e.g., :!rake routes -- I'll get some output and then:
Press ENTER or type command to continue
If I press anything at that point, I am always taken back to the main vim UI. How do I stay in that shell mode for a bit longer, specifically I can scroll back to see all the output of the command?
For terminal vim scrolling is provided by terminal emulator (or terminal multiplexer like tmux/screen if you use it). I.e. if your terminal scrolls when using <S-PageUp> it must also scroll with Vim’s !. Same for mouse wheel (it works for me even if vim was configured to handle mouse on its own).
If have terminal vim and you need to view output after you pressed <CR> then you can use <C-z>/:susp. Both these capabilities will be spoiled with incorrect configuration of either vim or terminal (terminal multiplexer) (i.e. terminal configured not to support alternate screens (which is normally issue for terminal multiplexers rather then terminal emulators: screen requires altscreen on in .screenrc) or vim setting telling vim what to output to the terminal on startup/resume and shutdown/suspend was spoiled).
If you have GUI vim your options are very limited. It is better to follow link provided by #glts or #EricAndres advice in this case as I do not use GUI vim.
:r! [shell command] will read the output from the command into the buffer. So if you open a new buffer then use that command, you can search and scroll through it all you like. See Vimcasts episode 57 for more information.

xterm keep page from less in terminal after exit

I often view files using less and want to remember what i have just seen in the file. However when I quit less by pressing the q key my xterm window removes the page of less showing the file and only shows my command prompt.
How do I keep the less output on my terminal when I quit?
less -X
Use the -X option from the less man page described below:
-X or --no-init
Disables sending the termcap initialization and deinitialization strings to the terminal.
This is sometimes desirable if the deinitialization string does something unnecessary, like
clearing the screen.
Actually, if you are using xterm (rather than some other program), the choice of whether to honor alternate-screen escape sequences can be done readily using a menu selection Enable Alternate Screen Switching (or resource setting). The less option does not apply to other programs such as vi.
In the manual, this is the titeInhibit, named after the termcap settings
ti (terminal initialization)
te (terminal ending)
and in the FAQ, this is Why doesn't the screen clear when running vi?.

after quitting VIM editor I am not able to see the original screen contents that were present before entering vim

I want to see the original contents of screen after quitting vim
as they were before opening a file , as of not my file quits but the original display is not there
THanks
The feature of returning screen contents after running a full screen application vs, leaving the contents there, is not specific to vi, but to your terminal emulator. The feature you want to turn on to return to the previous text is often known as 'altscreen'. If you are using xterm as your terminal emulator, this behaviour is default. However if you are running GNU Screen inside of an xterm (or other terminal), you need to add the line
altscreen on
to your ~/.screenrc file. Other terminals that support this feature will have other mechanisms to turn it on and off.
Instead of quitting, you can put vim into the background by typing control-z. This restores the previous screen, but leaves you the editor running 'stopped' with the current file. To get vim back, enter the command
fg %1 at the shell prompt. This brings vim back to the foreground again - at least assuming you only have one stopped job. The command jobs will give you a list of stopped jobs, which you can access by number.
So the work sequence becomes edit, save, control-z, compile, test, fg...
This works on linux, and Mac OS X - YMMV on other Unix variants.
If you are using xterm, then see :help xterm-screens, or ... read that anyway as it describes that your problem should be related to some terminfo setting - probably.
HTH

Resources