Text Editor's Cursor Movement Behavior (emacs, vi, Notepad++)

Perm url with updates: http://xahlee.org/emacs/text_editor_cursor_behavior.html

Text Editor's Cursor Movement Behavior (emacs, vi, Notepad++)

Xah Lee, 2010-06-17

This article discusses some differences of cursor movement behavior among editors. That is, when you press “Ctrl+”, on a line of programing language code with lots of different sequence of symbols, where exactly does the cursor stop at?

Always End at Beginning of Word?

Type the following in your favorite text editor.

something in the water does not compute

Now, you can try the word movement in different editors.

I tested this on Notepad, Notepad++, vim, emacs, Mac's TextEdit.

In Notepad, Notepad++, vim, the cursor always ends at the beginning of each word.

In emacs and TextEdit, they end in the beginning of the word if you are moving backward, but ends at the end of the word if you are moving forward.

That's the first major difference.

Does Movement Depends on the Language Mode?

Now, try this line:

something !! in @@ the ## water $$ does %% not ^^ compute

Now, vim and Notepad++ 's behavior are identical. Their behavior is pretty simple and like before. They simply put the cursor at the beginning of each string sequence, doesn't matter what the characters are. Notepad is similar, except that it will move into between %%.

Emacs and TextEdit behaved similarly. Emacs will skip the symbol clusters !!, @@, ##, ^^ entirely, while stopping at boundaries of $$ and %%. (when emacs is in text-mode) TextEdit will stop in middle of $$ and ^^, but skip the other symbol clusters entirely.

I don't know about other editors, but i understand the behavior of emacs well. Emacs has a syntax table concept. Each and every character is classified into one of “whitespace”, “word”, “symbol”, “punctuation”, and others. When you use backward-word, it simply move untill it reaches a char that's not in the “word” group.

Each major mode's value of syntax table are usually different. So, depending on which mode you are in, it'll either skip a character sequence of identical chars entirely, or stop at their boundary.

(info "(elisp) Syntax Tables")

The question is whether other editor's word movement behavior changes depending on the what language mode it is currently in. And if so, how the behavior changes? do they use a concept similar to emacs's syntax table?

In Notepad++, cursor word-motion behavior does not change with respect to what language mode you are in. Some 5 min test shows nor for vim.

More Test

Now, create a file of this content for more test.

something in the water does not compute
something !! in @@ the ## water $$ does %% not ^^ compute
(defun insert-p-tag () "Insert <p></p> at cursor point."
  (interactive) (insert "<p></p>") (backward-char 4))
for (my $i = 0; $i < 9; $i++) { print "done!";}
<a><b>a b c</b> d e</a>

Answer this:

  • Does the positions the cursor stop depends on whether you are moving left or right?
  • Does the word motion behavior change depending on what language mode you are in?
  • What is your editor? on what OS?

Which is More Efficient?

Now, the interesting question is which model is more efficient for general everyday coding of different languages.

First question is: is it more efficient in general for left/right word motions to always land in the left boundary the word as in vim, Notepad, Notepad++ ?

Certainly i think it is more intuitive that way. But otherwise i don't know.

The second question is: whether it is good to have the movement change depending on the language mode.

I don't know. But again it seems more intuitive that way, because users have good expectation where the cursor will stop regardless what language he's coding. Though, of course it MAY be less efficient, because logically one'd think that it might be better to have word motion behavior adopt to different language. But am not sure about this in real world situations.

Though, i do find emacs syntax table annoying from my experience of working with it a bit in the past few years... from the little i know, i felt that it doesn't do much, its power to model syntax is quite weak, and very complicated to use... but i don't know for sure.

This article is inspired from Paul Drummond question in gnu.emacs.help

Popular posts from this blog

11 Years of Writing About Emacs

does md5 creates more randomness?

Google Code shutting down, future of ErgoEmacs