18

I saw somewhere on the web someone using Ctrl-J and as I didn't know this mapping I looked up in the relevant doc and found the following:

j           or
<Down>      or
CTRL-J      or
<NL>        or
CTRL-N          [count] lines downward linewise.

Which leads me to several questions:

  • What is <NL>: I would see it as an equivalent of <CR> since pressing Enter will go down one line in normal mode by default but why is it <NL> here and not <CR>?
  • What is the difference between these mappings: Do all of these 5 options go one line down in the same way? According to my tests I would answer yes but that would lead to my next question.
  • Why are there 5 mappings to do the exact same thing: I can understand that j and <down> are kept for users who are not used to vim mappings, but why do the other mappings exist?
  • When is it more interesting to use one more than the other: That is a continuation of the previous question: if there is so many possibilities I guess that they have different advantages or are better to use in specific use cases. What are those use cases?

I find the redundancy of these commands even more strange when I look at :h k: there are only 3 ways to go up: k, <UP> and ctrl-p. So the bonus question is: Why are there 5 ways to go down and only 3 to go up?

5
  • Are you sure they were using the default Ctrl-J binding? It seems to be common, at least among those I know, to map Ctrl-J/K to "down/up until the next line with the same indentation level as the starting line". Aug 10, 2015 at 17:31
  • 4
    I'd like to answer to " Why are there 5 ways to go down and only 3 to go up?" Well - it's because, in life, things go south way easier! :)
    – VanLaser
    Aug 10, 2015 at 19:30
  • @VanLaser really great answer, they should consider adding this hint in the doc! ;-)
    – statox
    Aug 11, 2015 at 8:03
  • @VanLaser Too bad isn't it. At that rate all the humanity will descend into nothingness in no time!
    – xji
    Aug 15, 2015 at 3:01
  • "Abandon hope, all ye who <CR> here"
    – VanLaser
    Aug 15, 2015 at 9:40

2 Answers 2

17

Interesting that you mentioned <CR>. <CR> or carriage return, technically used to mean go to the start of the line. <NL>, or newline aka line feed (usually called LF), was used to mean go to the next line. Over time, the distinction was lost in most applications.1 Pressing Enter actually yields a carriage return, usually (try pressing CtrlVEnter - you should see ^M in Unix terminals). <NL>'s control code is CtrlJ.

CtrlN surprised me. According to man ascii2, it is a shift out character:

Oct   Dec   Hex   Char                        Oct   Dec   Hex   Char
────────────────────────────────────────────────────────────────────────
016   14    0E    SO  (shift out)             116   78    4E    N

The shift in and shift out characters, according to Wikipedia:

... provided a way to shift, hence the nomenclature, a coloured ribbon, split longitudinally usually with red and black, up and down to the other colour in an electro-mechanical typewriter or teleprinter ...

How it ended up meaning go down is a mystery to me.


Why are there 5 ways to go down and only 3 to go up?

Well, the three ways to go up all have corresponding ways to go down. (CtrlP presumably is the complement of CtrlN, implying that the shift in/out is not relevant here). The extra two ways down (<NL> or CtrlJ) are actually two representations of the same way. <NL> is an inheritance from the typewriter days - and in those days, I don't think there was a key to go up - only to go down.

See also:

1 It still matters in some cases (printing \r (carriage return) and \n (line feed) using most formatted printing functions/tools still retain the old meaning.).

2 This table is arranged in such a manner that the alphabet corresponds to the control code of the character on the same line.

6
  • 3
    I always thought Ctrl-N was for next, and Ctrl-P was for previous. That's what they stand for in Emacs, anyway, in which they are bound to next-line and previous-line. Also, Ctrl-P is not shift in; it is data link escape. Shift in is Ctrl-O.
    – Lithis
    Aug 10, 2015 at 17:24
  • @Lithis I stand corrected.
    – muru
    Aug 10, 2015 at 17:25
  • 2
    @Lithis Honestly, the only thing I know about Emacs is c-x c-e.
    – muru
    Aug 10, 2015 at 17:28
  • Historical and clear answer, thanks a lot Muru! I should have thought of the ascii code of the characters the answer often lies in there.
    – statox
    Aug 11, 2015 at 7:59
  • @muru That's too bad since Emacs actually has a lot to offer, especially its org-mode which has been massively helpful to me. I recommend you check out the project Spacemacs github.com/syl20bnr/spacemacs , which focuses heavily on Vim emulation so that you don't need to know any traditional Emacs command to use. Maybe that'll change your perception a bit.
    – xji
    Aug 15, 2015 at 3:10
8

See :help key-notation.

  1. <NL> means "linefeed" and <CR> means "carriage return".

    Those are two different ways to break the current line. They are historically bound to different control characters (ctrl-j and ctrl-m) but are usually inserted with the same key, <CR>. I agree that using <NL> in this context seems counterintuitive.

  2. All those "mappings" (they are not actually mappings) behave exactly the same way.

  3. History, mostly, also cross-platform constraints.

    There were no dedicated motion keys Bill Joy's terminal but the arrows decals were on the hjkl keys. Since modal editors were rare at the time, pressing those keys only inserted the associated character in whatever you were doing. You had to press <C-h>, <C-j>, <C-k> and <C-l> to move the cursor on the screen.

    As you can see, there's quite an overlap between j, <C-j>, <NL> and <Down>. <C-n> generally meaning "next" in Vim, it's easy to see how it got lumped together with its comrades.

  4. Since they are all synonymous, you are free to use the one that works best for you.

    <CR>, for example, is an interesting key to use for your own mappings.

7
  • Thanks for the enlighting! I greatly agree on the <CR> mapping. Also one more question: I know that those "key combinations" are not actually called mappings but I'm not sure how I should call them?
    – statox
    Aug 11, 2015 at 8:01
  • That's an interesting question, I tend to call them "commands".
    – romainl
    Aug 11, 2015 at 8:17
  • I may be wrong but isn't "command" referring to the ex mode commands like :ls, :substitute, new, etc...?
    – statox
    Aug 11, 2015 at 8:41
  • x is also a "command".
    – romainl
    Aug 11, 2015 at 9:06
  • Indeed I just saw that the doc calls x, dd, c, etc "commands". So you must be right, I guess that j or ^J are also commands.
    – statox
    Aug 11, 2015 at 9:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.