Skip to main content
Engineering LibreTexts

05-A.2.3: Vim - Command-Line Mode

  • Page ID
    32124
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    Command-Line Mode

    You may be asking, "Why does vim have so many modes, why is that necessary?" Well, you must remember that the original vi, the forerunner to vim, was created a LONG time ago, when keyboards did not have arrow keys, there were no graphical user interfaces, and everything was done on a character-based terminal.

    Vim uses one mode to insert text into the file; one mode to navigate around the file - although vim allows use of the arrow keys to move while in insert mode; one mode that allows us to select chunks of code to modify; and now we have the Command-Line mode where we can do a majority of the editing.

    In Command-Line we use certain keys or key sequences to edit the file and to move around within the file. Depending on where you look for information about vim, it may tell you that the Command-Line mode is only for the commands that start with ':' - but we are including other editing and search functionality as well.

    Remember - you need to make sure you are in Normal mode (which is actually the same as Command-Line mode) - if you are not sure what mode you are in you can simply hit the ESC key which is harmless if you are in Normal mode already.

    ex Commands

    There is an editor called ex, which is even older than vi, and it has some interesting syntax to its command. These commands are all entered by using a '':' and then the actual command. There are lots of vim resources that discuss the use of these very powerful commands, but the syntax is very different, especially if you are not used to operating from a command line.

    Some examples of ex commands (some of these we have already seen):

    Mode Name Purpose
    :wq Write file to disk and quit the editor
    :q! Quit - does NOT save file before quit
    :q Quit (a warning is printed if a modified file has not been saved)
    :w Writes the file back to the original file
    :w myFile Writes the current file to the file named myFile; it is created if it doesn't exist (if it does exist you will need to issue :w! myFile)
    :/[some pattern] Search forward in the file for the pattern - places cursor at the beginning of the line
    :?[some pattern] Search backward in the file for the pattern - places cursor at the beginning of the line
    :10,25 w newFile Writes lines 10 through 25 to a file named newFile (if that file exists you will need to issue 10,25 w! newFile)

    The example below is only showing the bottom of the terminal window, which is where the ex commands show up as soon as you enter the colon character. This image shows writing the current file to a file named myTest.

    it is possible to enter ex commands in vim - simply enter a colon followed by the command. In this case the command is w to write the file.
    Figure \(\PageIndex{1}\): Run ex commands in vi. ("vi using ex" by Patrick McClanahan is licensed under CC BY-SA 4.0)

    Cursor Movement

    To move the cursor you can use the arrow keys. There is also a set of commands to move the cursor to specific places within the file. These commands allow you to move quickly throughout the file. These keystrokes DO NOT show up anywhere on the terminal window.

    Key Purpose
    nG Cursor goes to the specified n (where n is some integer value) line
    ^F (CTRl F) Forward screenful
    ^B (CTRL B) Backward screenful
    ^f One page forward
    ^b One page backward
    ^U Up half screenful
    ^D Down half screenful
    $ Move cursor to the end of current line
    0 (zero) Move cursor to the beginning of current line
    w Forward one word
    b Backward one word

    Edit Commands

    There are keys/key sequences that edit the file. These keystrokes DO NOT show up anywhere on the terminal window.

    Key Purpose
    x Delete character
    dw Delete word from cursor on
    db Delete word backward
    dd Delete line
    d$ Delete to end of line
    d^ (d caret, not CTRL d) Delete to beginning of line
    yy Yank current line
    y$ Yank to end of current line from cursor
    yw Yank from cursor to end of current word
    5yy Yank, for example, 5 lines
    p Paste below/after cursor
    P Paste above/before cursor

    The two paste commands have two slightly different uses. If you yank an entire line from the file, then p (lower case) will paste that line below the current line and P (upper case) will paste it above the current line. If, however, you have yanked a word or even a part of the line, then p (lower case) will paste the text after the cursor position on the same line and P (upper case) will paste it before the cursor position on the current line.

    Adapted from:
    "Learning the vi Editor/Vim/Modes" by Multiple ContributorsWikiBooks is licensed under CC BY-SA 3.0


    05-A.2.3: Vim - Command-Line Mode is shared under a CC BY-SA 4.0 license and was authored, remixed, and/or curated by LibreTexts.

    • Was this article helpful?