Get the most out of your Centmin Mod LEMP stack
Become a Member

WebPerf Popular Vim Commands – a Comprehensive Vim Cheatsheet

Discussion in 'All Internet & Web Performance News' started by eva2000, Dec 22, 2016.

  1. eva2000

    eva2000 Administrator Staff Member

    53,811
    12,159
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +18,710
    Local Time:
    3:36 AM
    Nginx 1.27.x
    MariaDB 10.x/11.4+
    Vim was made available in 1991 and is a free, open source software. Available both as a command line interface and as a standalone program with a GUI, Vim is a text editor that is a modal version of the vi editor created for Unix in the 1970s; Vim stands for vi improved. While it was designed with Unix in mind, versions of it are available for most operating systems and Vim is also available for Android and iOS smartphones.


    While you may be familiar with the concept of a text editor, the modal part may throw you. A modal editor is one that allows you to edit text in different modes, and in the case of Vim, the mode determines what the alphanumeric keys on your keyboard do and how Vim editor commands work.

    For example, in insert mode, your keyboard behaves normally, so what you type in is what you see, just like with a standard text editor. However, if you switch to command mode, the letters on your keyboard will allow you use Vim commands to move within the text. If you play video games that use the left-hand keys on the keyboard to move your character around, this concept is probably familiar to you.

    To open a file using Vim you can use the following command (simply replace filename.css with your actual file name).

    vi filename.css

    The idea behind having a modal text editor is that it allows you to write and edit text, including code, without requiring your hands leave the keyboard. Vim isn’t for everyone, and it requires you to learn a variety of Vim editor commands to get the most out of it. That said, many people who have started using it and become comfortable with it won’t even consider another editor. The image below shows an exampe of what a css file looks like when viewed using Vim.

    [​IMG]

    This text editor is particularly well-suited for people who are programmers, coders, system administrators or individuals looking for a streamlined way to edit text. The editor allows you to edit text in multiple windows, which can be helpful to programmers and editors alike. If you’re interested in giving Vim a shot, the following is a basic explanation of Vim modes and a list of frequently used vim commands, along with a definition of what they do.

    Vim Modes


    There are some arguments as to how many modes that Vim has, but the modes you’re most likely to use are command mode and insert mode. These modes will allow you to do just about anything you need, including creating your document, saving your document and doing advanced editing, including taking advantage of search and replace functions.

    Command Mode


    This is the default mode that you’ll be in once you open Vim. If you’re in a different mode and want to go back to command mode, just hit the Escape key. This mode allows you to use vim commands and move through your document. From command mode, you can also use last-line commands, which generally start with the use of a colon. For example, :w saves your file and :q allows you to exit Vim.

    Insert Mode


    This mode allows you to enter text into your document. You can enter insert mode by pressing the i key. Keep in mind that to save your document, you’ll need to go back to command mode since only text input is allowed in this mode.

    Vim Commands


    The following is a list of frequently used commands and what they do. Many of the commands can be made to repeat by adding a number to the command. This is not an exhaustive list because more advanced commands, such as how to use multiple buffers, are not included. However, just about all of the basic commands for opening, editing and saving documents are included as well as commands that enable you to find and replace text and work with multiple documents.

    1. Basic Vim Commands


    The most simple commands allow you to open and close documents as well as saving them. As with most other text editors, there are protections in place to help you avoid exiting the editor without having saved what you’re working on.

    :help [keyword] – Performs a search of help documentation for whatever keyword you enter
    :eek: [file] – Opens a file, where [file] is the name of the file you want opened
    :w – Saves the file you are working on
    :w [filename] – Allows you to save your file with the name you’ve defined
    :wq – Save your file and close Vim
    :q! – Quit without first saving the file you were working on

    2. Vim Commands for Movement


    When using movement commands, you can put a number in front of them to make Vim complete a command multiple times. For example, 5h will move your cursor five spaces to the left, and 90j will put your cursor at the beginning of the ninth line down from where your cursor currently is.

    h – Moves the cursor to the left
    l – Moves the cursor to the right
    j – Moves the cursor down one line
    k – Moves the cursor up one line

    H – Puts the cursor at the top of the screen
    M – Puts the cursor in the middle of the screen
    L – Puts the cursor at the bottom of the screen

    w – Puts the cursor at the start of the next word
    b – Puts the cursor at the start of the previous word
    e – Puts the cursor at the end of a word


    0 – Places the cursor at the beginning of a line
    $ – Places the cursor at the end of a line

    ) – Takes you to the start of the next sentence
    ( – Takes you to the start of the previous sentence
    } – Takes you to the start of the next paragraph or block of text
    { – Takes you to the start of the previous paragraph or block of text

    Ctrl+f – Takes you one page forward
    Ctrl+b – Takes you one page back

    gg – Places the cursor at the start of the file
    G – Places the cursor at the end of the file
    :# – Where # is the number of a line, this command takes you to the line specified

    3. Vim Commands For Editing


    Those who use Vim tend to use the term “yank” where most people would use the terms copy and paste. Therefore, the command for copying a word is yw, which stands for yank word, and the command for pasting whatever has been copied is p, meaning put. If you look up additional commands in the future, it can be confusing if you don’t know what yank and put mean when using Vim.

    As with movement commands, putting a number in front of the command can increase the number of times a task is completed. For instance, putting a number in front of yy will increase the number of lines copied, so 5yy will copy five lines.

    You also have two options for how to select text. You can either use commands like dd, which deletes a single line, and yw, which copies a single line, or you can highlight text and then copy it to the clipboard. The paste commands work the same whether you’ve highlighted text or used a command to automatically copy it.

    yy – Copies a line
    yw – Copies a word
    y$ – Copies from where your cursor is to the end of a line

    v – Highlight one character at a time using arrow buttons or the h, k, j, l buttons
    V – Highlights one line, and movement keys can allow you to highlight additional lines
    p – Paste whatever has been copied to the clipboard

    d – Deletes highlighted text
    dd – Deletes a line of text
    dw – Deletes a word
    D – Deletes everything from where your cursor is to the end of the line
    d0 – Deletes everything from where your cursor is to the beginning of the line
    dgg – Deletes everything from where your cursor is to the beginning of the file
    dG – Deletes everything from where your cursor is to the end of the file
    x – Deletes a single character

    u – Undo the last operation; u# allows you to undo multiple actions
    Ctrl+r – Redo the last undo
    . – Repeats the last action

    4. Vim Commands for Searching Text


    Like many other text editors, Vim allows you to search your text and find and replace text within your document. If you opt to replace multiple instances of the same keyword or phrase, you can set Vim up to require or not require you to confirm each replacement depending on how you put in the command.

    /[keyword] – Searches for text in the document where keyword is whatever keyword, phrase or string of characters you’re looking for
    ?[keyword] – Searches previous text for your keyword, phrase or character string
    n – Searches your text again in whatever direction your last search was
    N – Searches your text again in the opposite direction

    :%s/[oldkeyword]/[newkeyword]/g – This replaces all instances of whatever your old keyword, phrase or string of characters is with new text
    :%s/[oldkeyword]/[newkeyword]/gc – Makes replacements without requiring a confirmation for each replacement

    5. Vim Commands For Working With Multiple Files


    You can also edit more than one text file at a time. Vim gives you the ability to either split your screen to show more than one file at a time or you can switch back and forth between documents. As with other functions, commands make going between documents or buffers, as they’re referred to with Vim, as simple as a few keystrokes.

    :e file – Opens an additional file in a new buffer
    :bn – Switch to next buffer
    :bp – Switch to previous buffer
    :bd – Close a buffer
    :sp [filename] – Opens a new file and splits your screen horizontally to show more than one buffer
    :vsp [filename] – Opens a new file and splits your screen vertically to show more than one buffer
    :ls – Lists all open buffers

    ctrl+ws – Split windows horizontally
    ctrl+wv – Split windows vertically
    ctrl+ww – Switch between windows
    ctrl+wq – Quit a window

    Ctrl+wh – Moves your cursor to the window to the left
    Ctrl+wl – Moves your cursor to the window to the right
    Ctrl+wj – Moves your cursor to the window below the one you’re in
    Ctrl+wk – Moves your cursor to the window above the one you’re in

    Vim Commands – In Summary


    Vim is quite easy to use, it just involves memorizing Vim editor commands and remembering what mode you’re in. If you’re used to using keyboard shortcuts like Ctrl+C and CTRL+S, you shouldn’t have too much difficulty getting used to the way that Vim works. While there’s a bit of a breaking in period with the editor, you don’t have to worry too much about accidentally deleting large swathes of text without being able to recover them since you can use the undo command multiple times.

    While not for everyone, functionality like being able to work on more than one document at a time in windowed screens and the ability to do major editing without a mouse is what makes Vim so popular. You can download the editor for free, and there are a variety of plug-ins and extensions that can improve its functionality and add additional vim commands.

    The post Popular Vim Commands – a Comprehensive Vim Cheatsheet appeared first on KeyCDN Blog.

    Continue reading...
     
    Last edited: Dec 23, 2016