Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can VIM tell the difference between `Ctrl-J` and `LF`?

I'm trying to create a little Python/curses app.

But as far as I can see there's no way to tell whether CTRL+J or Enter have been pressed. Now this may be caused by the fact that they both have the same ascii code (10):

http://en.wikipedia.org/wiki/Control_character#In_ASCII

But how can VIM tell the difference between these two?

like image 970
Dave Halter Avatar asked Jan 30 '13 23:01

Dave Halter


1 Answers

Enter is usually equivalent to C-m. But, if the icrnl flag is active for the tty (see stty -a), then an input C-m will automatically be translated to C-j (so that it is easy to type Unix-ly terminated lines by just pressing Enter).

In plain C you could use the termios functions tcgetattr(3) and tcsetattr(3) to unset the ICRNL flag in c_iflag so that C-m is not translated to C-j on input. If you want absolute control over the input and output, you would use a “raw” mode (disable all input and output processing). It looks like Python has these termios functions.

The curses library has some higher-level functions for dealing with tty modes: savetty(3), resetty(3), nonl(3), raw(3), cbreak(3), etc. It also looks like Python has these curses functions.

If you are using other bits of the curses library, then it is probably best to also use its functions to adjust the ICRNL flag (e.g. nonl(3)) to avoid breaking any assumptions that the library has made (i.e. it assumes the tty is set one way, but your termios-level calls change things and break that assumption).

like image 81
Chris Johnsen Avatar answered Oct 03 '22 14:10

Chris Johnsen