Enter key is different from Carriage Return (CR)

26,683

Solution 1

Your terminal sends carriage return when you press Enter, and on Unix-like systems, the terminal driver translates that into line-feed ("newline").

That's the icrnl feature shown by stty -a, e.g.,

$ stty -a
speed 38400 baud; rows 40; columns 80; line = 0;
intr = ^C; quit = ^\; erase = ^H; kill = ^U; eof = ^D; eol = <undef>;
eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R;
werase = ^W; lnext = ^V; flush = ^O; min = 1; time = 0;
-parenb -parodd cs8 -hupcl -cstopb cread -clocal -crtscts
-ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon -ixoff
-iuclc -ixany -imaxbel -iutf8
opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt
echoctl echoke

Programs (even shell scripts) can turn that off to read the actual carriage return character to distinguish it from ControlJ (line feed).

Solution 2

The Enter key does send a CR character (carriage return, Ctrl+M, numerical value 13 = 0x0d = 015). You can see that at a shell prompt or in Vi insert mode by pressing Ctrl+V then Enter: Ctrl+V is a keyboard shortcut to enter the next character literally in the kernel's built-in terminal driver as well as in many terminal-based programs including typical shell and Vi(m).

In Vi(m) insert mode, the character Ctrl+M is bound to the command “insert line break”. Vi reacts to most input characters by inserting that characters, but there are a few exceptions, most obviously the character Ctrl+[ which is what the Esc key sends.

In a text file, a line break is represented by a LF character (line feed, Ctrl+J, numerical value 10 = 0x0a = 013).

Pressing Ctrl+J in Vi would actually have the same effect, but you could bind the two keystrokes to separate commands if you wanted. You can observe a difference between Ctrl+J and Ctrl+M in command mode: Ctrl+J simply moves the cursor down to the next line whereas Ctrl+M moves the cursor down to the next line and also moves it to the first non-blank character.

Share:
26,683

Related videos on Youtube

showkey
Author by

showkey

contact me at [email protected]

Updated on September 18, 2022

Comments

  • showkey
    showkey over 1 year

    Many webpages said that ASCII code for enter key is 13(0d).
    Enter key is considered as Carriage Return (CR).
    Now let's make an experiment.
    To open vim and just click enter key for three times ,do nothing more,then save the file as test.csv.

    xxd  test.csv
    0000000: 0a0a 0a 
    

    My conclusions got.
    The enter key's ascii value is 0a ,meaning newline ,it is different from Carriage Return (13 or 0d in ascii).
    Is right or not?

  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' about 7 years
    Yes but that's irrelevant. In Vim, icrnl is off.
  • Marius
    Marius about 7 years
    Try reading the last sentence again, to see if you understand it.
  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' about 7 years
    The fact that “programs can” in the last sentence can be instantiated with “Vim does” to make a true sentence implies that everything that you explained previously is irrelevant. Which leaves your post not answering the question at all.