Why some commands in vim require a colon while some don't?

前端 未结 4 676
栀梦
栀梦 2021-02-03 19:58

Some of the commands in vim are given by first typing a colon (:) eg . :wq for saving a file and quitting . While some of the commands don\'t require a colon for example the Rep

4条回答
  •  礼貌的吻别
    2021-02-03 21:00

    The most important difference (IMHO) is that the colon commands are treated as words, not individual letters, and they aren't interpreted until you hit enter.

    Commands in normal mode are treated as individual letters, each letter has a defined meaning, and they are executed as soon as vim knows what to do with them.

    For instance, typing 'de' in normal mode, will delete everything up until the end of the word. But 'de' isn't really a command; it's two commands. "e" moves you to the end of a word, and "d" means "delete something; wait for the next key press to tell you how much to delete." When you type 'd', it waits for you to complete the instruction. As soon as you press 'e', the command executes (without waiting to see if you want to type anything more).

    That means in normal mode, you can't have an instruction like "define" or "deliver" or "describe". You'd get as far as typing 'de', and vim would execute the 'de' instruction. By the time you got to the 'f' in define, you'd be starting a new instruction.

    But in command line (colon) mode, the commands are treated as words. There could be a command called 'define' and another command called 'describe'. (There isn't, but there could be).

    'w' could have been created as a normal mode command, if all it did was save the current file under the current filename. But ':w' does more than that. You can also do ":w new_filename", and you get a 'save as' function. That wouldn't work in normal mode. In normal mode "w new_filename" would save the file ('w'), do nothing (space), try to find the next search term ('n'), move to the next end-of-word position ('e')... and so on.

提交回复
热议问题