100

I'm learning Vim and can't wrap my head around the difference between word and WORD.

I got the following from the Vim manual.

A word consists of a sequence of letters, digits and underscores, or a sequence of other non-blank characters, separated with white space (spaces, tabs, ). This can be changed with the 'iskeyword' option. An empty line is also considered to be a word.

A WORD consists of a sequence of non-blank characters, separated with white space. An empty line is also considered to be a WORD.

I feel word and WORD are just the same thing. They are both a sequence of non-blank chars separated with white spaces. An empty line can be considered as both word and WORD.

Question:
What's the difference between them?
And why/when would someone use WORD over word?

I've already done Google and SO search, but their search-engine interpret WORD as just word so it's like I'm searching for Vim word vs word and of course won't find anything useful.

octref
  • 6,521
  • 7
  • 28
  • 44
  • 4
    This is explained and even illustrated at `:h 03.1`. Please check the user manual. – glts Apr 08 '14 at 11:31
  • 1
    @Al.G. Glad to know. Thanks! – octref Nov 03 '15 at 16:52
  • 2
    There's ambiguity in English 'or'. What they mean to say is: *A word consists of a sequence of (letters OR digits OR underscores), XOR (a sequence of other non-blank characters), separated with white space (spaces, tabs, ).* – user1433150 Sep 06 '18 at 06:28
  • @user1433150 that was helpful thanks. But even with that clarification there is still something confusing. They say: "or a sequence of other non-blank characters, separated with white space (spaces, tabs, )" but if that is true then it simply means **any** word. So it seems to me that they say "its a smaller set XOR everything". Is that correct? – Charlie Parker Jul 12 '20 at 16:11
  • The thing that clicked for me is one line from `:h 03.1`. It says, `A word ends at a non-word character, such as a ".", "-" or ")"`. But it is not the complete list. In fact anything not in the list returned by `set iskeyword?` will separated a word (This command returns ascii values range, `iskeyword=@,48-57,_,192-255`; @ mean all characters). These ".", "-",")" etc will separate word because they are not included in the specified ASCII value range. "_" do not separate word because it is in the specified value range. – Quazi Irfan Aug 11 '22 at 06:31

6 Answers6

132
  • A WORD is always delimited by whitespace.
  • A word is delimited by non-keyword characters, which are configurable. Whitespace characters aren't keywords, and usually other characters (like ()[],-) aren't, neither. Therefore, a word usually is smaller than a WORD; the word-navigation is more fine-grained.

Example

This "stuff" is not-so difficult!
wwww  wwwww  ww www ww wwwwwwwww    " (key)words, delimiters are non-keywords: "-! and whitespace
WWWW WWWWWWW WW WWWWWW WWWWWWWWWW   " WORDS, delimiters are whitespace only
Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
  • 4
    Note that you can use the :set iskeyword to change what 'W' does. See :help 'iskeyword' for more information. – Jessie Jun 17 '18 at 16:01
  • Example above is wrong. Double quotes are words too (or part of the one word depending on the 'iskeyword' option). Thus it should be marked with 'w' too. As well as dash. – Flashrunner Jul 14 '19 at 23:06
  • @Flashrunner That depends on the `'iskeyword' ` option, as mentioned by Jared Ross. By default, it works as I've written. – Ingo Karkat Jul 15 '19 at 04:29
  • `:set iskeyword=a-z,A-Z,\",- |This |"stuff" |is |not-so |difficult|! :set iskeyword=a-z,A-Z |This |"|stuff|" |is |not|-|so |difficult|!` `|` - is where 'w' stops. As you can see it is always a word. – Flashrunner Jul 15 '19 at 10:46
  • Jared Ross's comment is wrong too. iskeyword changes what 'w' does, not 'W'. – Flashrunner Jul 15 '19 at 10:53
  • @Flashrunner I think Jared's comment got you off track. The question (and my answer) are about what characters _belong to_ a word, not where motions like `w` (and yes this should be lowercase-w) stop. It's related, but not the same, because (as you've pointed out) the motion stops at both whitespace-keyword and whitespace-nonkeyword boundaries. – Ingo Karkat Jul 15 '19 at 15:11
  • "A word consists of a sequence of letters, digits and underscores, **or a sequence of other non-blank characters**, separated with white space". `", -, !` are words too, no matter whar `iskeyword' option is. There is no whitespace-[non]keyword notion in the help (at least at 'word' section). – Flashrunner Jul 15 '19 at 15:32
  • In my `:help word`, that paragraph continues: _This can be changed with the 'iskeyword' option._ And yes, the boundaries are an added detail (apparently not documented, at least I could not find it) of the `w` motion. – Ingo Karkat Jul 15 '19 at 16:05
47

To supplement the previous answers... I visualise it like this; WORD is bigger than word, it encompasses more... enter image description here

run_the_race
  • 1,344
  • 2
  • 36
  • 62
23

If I do viw ("select inner word") while my cursor is on app in the following line, it selects app:

app/views/layouts/admin.blade.php

If I do viW (WORD) while my cursor is at the same place, it selects the whole sequence of characters. A WORD includes characters that words, which are like English words, do not, such as asterisks, slashes, parentheses, brackets, etc.

  • Last paragraph of the explanation above is unclear. It's missing some crucial words which would have made the difference clear. – Praym Aug 24 '16 at 05:04
17

According to Vim documentation ( :h 03.1 )

  • A word ends at a non-word character, such as a ".", "-" or ")".

  • A WORD ends strictly with a white-space. This may not be a word in normal sense, hence the uppercase.

eg.

           ge      b          w                             e
           <-     <-         --->                          --->
    This is-a line, with special/separated/words (and some more). ~
       <----- <-----         -------------------->         ----->
         gE      B                   W                       E

If your cursor is at m (of more above)

  • a word would mean 'more' (i.e delimited by ')' non-word character)

  • whereas a WORD would mean 'more).' (i.e. delimited by white-space only)

similarly, If your cursor is at p (of special)

  • a word would mean 'special'
  • whereas a WORD would mean 'special/separated/words'
Praym
  • 2,038
  • 23
  • 21
1

That's a grammar problem while understanding the definition of "word".

I get stuck at first in Chinese version of this definition (could be miss-translation).


The definition is definitely correct, but it should be read like that:

A word consists of:
   [(a sequence of letters,digits and underscores),
or (a sequence of other non-blank characters)], 
separated with white space (spaces, tabs, <EOL>).

Whitespace characters were only needed when delimiting two same types of 'word'


More examples in brackets as follow:

(example^&$%^Example) three "word" :(example), (^&$%^) and (Example)

(^&^&^^ &&^&^) two "word" : (^&^&^^) and (&&^&^)

(we're in stackoverflow) five "word" :(we), ('), (re), (in) and (stackoverflow)

0

Another way to say it. If ur coding, and want to move thru the line stopping at delimiters and things line that "() . [] , :" use w.

if you want to bypass those and just jump to words lets say like a novel or short story has, use W.

For coding the small w is probably the one used most often. Depends where you are in the code.