Input Method in Vim - vim

From :help imcmdline:
When set the Input Method is always on when starting to edit a command line, ...
There is no link to Input Method. What Input Method is referring to?

An input method is a program (or os component) which allows keystrokes ore mouse movements (or clicks) to be received as input. Using a different input method, you can enter characters and symbols that are not on your input device. (such as a keyboard) For instance, a different input method would be used for languages that have more graphemes than there are keys on the keyboard. In this case, setting imcmdline would mean that the current input method is always used when entering command in vim. The are similar options for different modes as well, such as iminsert or imsearch. The function used to get the current input method is set with the imactivatefunc option. Another option is imdisable which entirely disables the current input method.

Input Method is a special input mode for those who need to type in Chinese, Japanese and other non-Latin layout. IM mode allows to avoid switching your keyboard layout to English when you need to enter some command or cursor motion.

Related

Is there a way to programatically add escape sequences to ncurses

I am working on a ncursesw app that uses function keys.
Unfortunately it seems that there are some terminal emulators (notablly putty) that claim to be of terminal type "xterm" but send different escape sequences for the f1 to f4 keys from what a modern xterm sends (from some googling it seems that very old versions of xterm did the same).
ncursesw on my system just passes these escape sequences through to the app without interpreting them.
I would like to make my program accept these additional escape sequences for function keys. Is there a way to programatically add escape sequences to ncurses or will I need to write my own escape sequence interpreter?
Yes it is possible, using the call "define_key" ( http://invisible-island.net/ncurses/man/define_key.3x.html )
The documentation is not clear on whether it allows more than one escape sequence for a given "key" or not. My testing shows that it does allow it. So one can simply define the additional sequences.
define_key("\e[11~",KEY_F1);
define_key("\e[12~",KEY_F2);
define_key("\e[13~",KEY_F3);
define_key("\e[14~",KEY_F4);
You may want to surround this with a termname check so it only applies when the claimed terminal type is xterm (I did in my actual program but my actual program was written in pascal).

jump over user defined text objects in vim

I am using the kana / vim-textobj-user for defining some custom user objects but the problem is I can't jump over them : case in point
let's say I am using the same indent text object which is mapped by ai and ii
I want to jump around the text in normal mode something like ]i and [i
currently I am using a very hacky way of selecting and exiting visual mode
So is there a simple way to do that and have some kind of mappings for all the other user text-objects as well .
Something like ]{text-object}
I am using the kana / vim-textobj-user for defining some custom user objects
[...]
let's say I am using the same indent text object which is mapped by ai and ii
I want to jump around the text in normal mode something like ]i and [i
Vim has a bunch of built-in commands like ]m, [M, etc. So I thought you meant ]i/[i to move the cursor to the next/previous text object. If so, vim-textobj-user supports both selecting and moving to a text object since its first release. But it's not automatic. At least you have to declare what keys (such as ]i/[i) to be used for the commands.
But I wonder about the following sesntence:
currently I am using a very hacky way of selecting and exiting visual mode
So you typed like vaio<Esc> and vai<Esc>? What you want to do is to move the cursor to the first/last line of the text object under the cursor? If so, vim-textobj-user currently doesn't provide API to define such commands.
In this case, it is probably possible to automate defining key mappings like nmap ]i vai<Esc>. But it seems to be fragile and overrides several built-in commands.
Text objects are only for applying a command (e.g. gU) or visually selecting an area of text. Motions over / to the next occurrence are highly related, but different commands. I think the vim-textobj-user plugin only provides the former, but not the latter.
My CountJump plugin is quite similar, and provides commands to set up both text objects and jumps based on regular expressions.

Is there a typeahead buffer for key presses in Linux terminal?

Does Linux buffer keys typed in terminal so that you can read them later one key at a time?
I am asking because I want to catch ESC and arrow key presses and can't find a way to reliably read the codes. I put terminal in non-canonical mode and want program to block if there is no input, but if there is, I want to fetch only one key press for processing.
Update 2: Arrow keys is just an example. I need to identify key presses even for the keys with unknown escape sequences for my program.
There are two conflicting cases:
read(1) returns one character. For both function keys and ESC key this character will be 0x1b. To check if it was an arrow key, you need to read(1), which will block if only single ESC is pressed.
Solution: blocked read(1), non-blocked read(1)
Problem: if second read didn't match any function key, it may mean that it was buffered ESC followed by some sequence, or an unknown function key. How to detect unknown function key press?
read(4) returns at most 4 characters, but if you press ESC four times to let it buffer, you'll get a string of four 0x1b. The same problem to find out if there is an unknown function key press as above.
Can anybody explain how to deal with these problems in Linux terminal, or at least post a proof that Linux just doesn't have input buffer for keys?
You should read up on VT100 escape sequences.
You've discovered that the character code for the escape button (which is sent as a real character, but tends to be used almost exclusively for signalling the beginning of an escape sequence) is 0x1b.
To move the cursor UP: <ESC>[{COUNT}A
To move the cursor DOWN: <ESC>[{COUNT}B
To move the cursor RIGHT: <ESC>[{COUNT}C
To move the cursor LEFT: <ESC>[{COUNT}D
You can test these yourself by typing them into the terminal. Just type the keys one after another. My terminal does not recognize the count argument, but will work successfully if I type <ESC>[X (for X in A,B,C,D).
If your terminal isn't in VT100 mode, look up the escape sequences for whichever mode it's in. You might realize that depending too much on terminal specific escape codes restricts your program to one specific terminal type.

Mapping numeric keypad keys in vim

I'm unable to get this mapping to work in vim inside an xterm terminal.
:map <k0> :echo 'Hello'<CR>
I can get the same mapping to work fine in gvim. If I issue the above command in vim on a terminal, it accepts it, and it shows up correctly when I type :map. But in normal mode, if I press the 0 keypad key, a "0" shows up on the status line, and then disappears with the next keypress.
I'm using the vim that came with Fedora 14 if that matters, and a plain xterm. The keypad keys work fine in insert mode, both with numlock on and off.
What am I missing?
Try to add this line to your ~/.Xdefaults:
xterm*appkeypadDefault: false
and relaunch xterm.
The "Application Keypad Mode" is likely the reason of your troubles.
But I don't think you should do what you are doing. In --NORMAL-- mode, numeric input is used to indicate "count" like in 4dd. Mapping numbers to other commands is going to get you into troubles fast.
You should add a xterm tag to your question.
There's some ambiguity in the question, which may indicate the actual problem. vim accepts that binding for k0 supposing that it is a function key.
Most keyboards that you'll see number function-keys starting at 1, and a few terminal descriptions equate function-key 1 to k0, a few equate it k0 to function-key 10. It's also possible that someone assumes that is part of the numeric keypad, but unlikely (since the keypad uses different character sequences than the function keys).
That's assuming you used a terminal description that knows about the function keys. The vt100 terminal description doesn't do that, since vt100's had no function keys (other than PF1 through PF4 which are or aren't depending who you talk to). But if you had TERM=vt100, then some of the numeric keypad could be recognized on the basis of the terminal description (see for instance the lengthy comment above the vt100+fnkeys description).
It's not in TERM=xterm, however.
What you're overlooking is that vim (helpfully perhaps) amends the terminal description using its built-in termcaps. It recognizes PF1, etc. using table entries like this:
{K_XF1, IF_EB("\033O*P", ESC_STR "O*P")},
{K_XF2, IF_EB("\033O*Q", ESC_STR "O*Q")},
{K_XF3, IF_EB("\033O*R", ESC_STR "O*R")},
{K_XF4, IF_EB("\033O*S", ESC_STR "O*S")},
But there are no entries for the numbered keys; there's no "\033O*p" for the 0 key.
If vim has (in the terminal description) the k0, and you haven't mapped it to anything, vim will treat it as a literal 0. The same happens with k1, etc., in effect treating the function-keys and numeric keypad as the same thing.
For what it's worth, GNU screen does the same thing, but also for the numbered keys. If I run vim inside screen, vim will see only the 0's. A literal 0 in vim doesn't do much in command-mode.

Linux bash read output explanation

I would like to understand the values I get from read command in console. Are these outputs combinations of some keys?
F2 ^[OQ
F3 ^[OR
F4 ^[OS
ESC ^[
My problem is that I use special 128 keys keyboard that is programmed for specific software. I need to send these "keys" into this software using code below. Don't be confused about the fact that I'm using Linux to read keyboard and in code below I use Win. I'm just trying to figure it out first on my Linux box.
import win32api, win32com.client
shell = win32com.client.Dispatch('WScript.Shell')
shell.AppActivate('Some Application Title')
shell.SendKeys('%fo') # Alt+F, O
win32api.Sleep(100)
Thnx guys
These are standard ANSI Escape Sequences. The "^[" is an CSI = Control Sequence Introducer (not a TV series).
See http://en.wikipedia.org/wiki/ANSI_escape_sequences or a similar source on ANSI Escape Sequences.
If you send such sequences to something that recognises them, you can cause the effect they imply, like clear screen, change colour or, indeed, act as if the F1 key was pressed. However, you do need a recipient which does handle them! (like an linux console) A simple File stream would just write the characters.

Resources