Determining Priority of Programs' Handling Keyboard Input - linux

When I run vim in a terminal screen buffer under the gnu screen terminal multiplexer, the effect of Cntrl-A is initiation a screen binding. But when I run vim in a terminal screen buffer without gnu screen, the effect of Cntrl-A is vim incrementing the number under the cursor. What is giving screen priority over vim in this case? Can I override or customize this priority scheme?

Ctrl-A is GNU Screen's default escape character, also called the command character; it precedes every screen command. You can change it to something else using a command-line switch (-e xy) when you run screen or within screen using ctrl-a : escape xy. In both cases x is the new escape character, for example ^B, and y is the key to send that escape character to the program in the window. Or you can put the "escape" command in your .screenrc initialization file. The default is -e ^Aa meaning you can send a ctrl-A to vi by typing ctrl-a followed by "a".
This is explained in the GNU Screen manual in the sections on Invoking Screen and Command Character.

Related

How to use tabs with vim in the Linux "screen" program?

When I use the Linux screen program, pressing the tab key will move between multiple screen windows. But when I want to press Tab to add blank space in a file using vim, it is useless, and will move to another screen. How can I use Tab with vim in screen?
EDIT: (taken from comments)
I have this keybinding in my .screenrc file:
escape ^Ii
And if I remove that, the Tab key works again.
The problem is your escape ^Ii entry in the .screenrc file.
In many/most terminal emulators, ^I (CtrlI) is the same key code as the Tab key (ASCII 0x11, aka "TAB" or "Horizontal Tab").
So the only solution is to choose a different escape key for use in screen.

How to enter ^A in screen command [duplicate]

This question already has answers here:
Screen command disable the control key ctrl-a to use it in vim?
(2 answers)
Closed 5 years ago.
I wish to enter Ctrl-A to the terminal which will move the cursor to the very beginning of my current command line.
However, if I were using screen within the terminal, I was not aware of any way of entering Ctrl-A directly to the terminal instead of the default behavior.
Does anyone know how I could enter Ctrl-A directly to the terminal?
Gnu screen documents how to use its command key Ctrl-A, check the exception in "5.1 Default Key Bindings" section of the documentation:
https://www.gnu.org/software/screen/manual/screen.html#Default-Key-Bindings
5.1 Default Key Bindings
As mentioned previously, each keyboard command consists of a C-a followed by one other character. For your convenience, all commands that are bound to lower-case letters are also bound to their control character counterparts (with the exception of C-a a; see below).
C-a a
(meta)
Send the command character (C-a) to window.
So, press Ctrl-A then A to send Ctrl-A signal into program running inside the gnu screen.

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.

Vim "show my last command" command?

Is there a command which shows what was the last command in normal mode?
Suppose I accidently hit random key and got some unexpected result.
Sure I can undo it, but could I reveal what key was pressed and how it was interpreted?
Hit the colon (:) and then use the up arrow to start going back through previous commands. You can use the up/down arrows too to move around the list.
q: will show you command history in Vim.
q/ will show you history of searches.
And must importantly, :q will quit the mode.
The text from the last command is stored in the . register. You can see all registers by :display. Unfortunately it doesn't say what the started the normal command.
To see commands from : (command mode) you can use :hist or q: which is limited to the last 20 (by default).
Another ability is to save the undo buffer :wundo undo.bin -- but the undo buffer is binary.
But none of these actually answer your question. I'm curious if it can be done.
Entering colon : then ctrl+p shows your previous command, i.e., moving backward through your vim command history. ctrl+n moves forward.
This is very convenient if you're used to using the command line and prefer not to change your keyboard hand positioning to use arrow keys.
It is difficult to know it. You can play with the variables:
v:operator
v:count (and v:prevcount)
v:register
But you cannot fully get the last normal mode command issued.
However if you want to systematically record everything you type while in Vim, you can launch vim -W ~/.vim-last-scriptout (a Windows version: vim -W "%HOMEPATH%\Vim\.last-scriptout) You can alias it in your shell on a UNIX machine. Every single key, or control-key, will be recorded into that file. Note that if you happen to use gvim or vim -g (the GUI) you might encounter this bug.
If you want to replay this file you can use :source! (with the exclamation mark) or the -s option from the command line.
On Windows I have set gvimportable.exe -W gvim_directory\last_scriptout as my default editor in my Commander program (FreeCommander). This way I can always remember what I have typed to do something and repeat a sequence of commands on another file. Of course I have another shortcut for opening Vim and playing the scriptout.
Note that the file might be written only when Vim exits, so you have to lose your session to know what you've done.

Resources