Daily Linux Command: Vim

<span>vim</span> is a very powerful and commonly used text editor in Linux, and it is an enhanced version of the <span>vi</span> editor. Although beginners may find it difficult to get started, once familiar, its efficiency and powerful features will greatly improve your editing productivity.

๐Ÿงพ Basic Command Format:

vim [filename]

For example:

vim myfile.txt

๐Ÿง  Different Modes in Vim:

  1. Normal Mode is the default mode when Vim starts, used for executing commands such as copy, paste, delete, etc.

  2. Insert Mode is used for entering text. To enter this mode, press <span>i</span> in Normal Mode.

  3. Visual Mode is used for selecting text. Enter it by pressing <span>v</span> (character selection), <span>V</span> (whole line selection), or <span>Ctrl+v</span> (block selection).

  4. Command-line Mode is used for executing commands like save, exit, find, etc. Enter it by pressing <span>:</span>.

โœ… Common Vim Operations

๐Ÿ“ Inserting and Editing Text

  • <span>i</span> โ€” Insert before the current cursor position
  • <span>I</span> โ€” Insert at the beginning of the current line
  • <span>a</span> โ€” Insert after the current cursor position
  • <span>A</span> โ€” Insert at the end of the current line
  • <span>o</span> โ€” Create a new line below the current line for insertion
  • <span>O</span> โ€” Create a new line above the current line for insertion

๐Ÿ“‚ Saving and Exiting

  • <span>:w</span> โ€” Save
  • <span>:q</span> โ€” Exit
  • <span>:wq</span> โ€” Save and exit
  • <span>:q!</span> โ€” Force exit without saving changes
  • <span>:x</span> โ€” Similar to <span>:wq</span>, but only saves if there are changes
  • <span>:w filename</span> โ€” Save as <span>filename</span>

๐Ÿ” Finding and Replacing

  • <span>/keyword</span> โ€” Search downwards (press <span>n</span> for next, <span>N</span> for previous)
  • <span>?keyword</span> โ€” Search upwards
  • <span>:s/old/new/</span> โ€” Replace the first occurrence in the current line
  • <span>:s/old/new/g</span> โ€” Replace all occurrences in the current line
  • <span>:%s/old/new/g</span> โ€” Replace all occurrences in the entire file
  • <span>:%s/old/new/gc</span> โ€” Replace with confirmation

๐Ÿ“‹ Deleting, Copying, and Pasting

  • <span>x</span> โ€” Delete the current character
  • <span>dd</span> โ€” Delete the current line
  • <span>dw</span> โ€” Delete a word
  • <span>d$</span> โ€” Delete from the cursor to the end of the line
  • <span>yy</span> โ€” Copy the current line
  • <span>yw</span> โ€” Copy a word
  • <span>y$</span> โ€” Copy from the cursor to the end of the line
  • <span>p</span> โ€” Paste after the cursor
  • <span>P</span> โ€” Paste before the cursor

๐Ÿงญ Moving the Cursor

  • <span>h</span> / <span>j</span> / <span>k</span> / <span>l</span> โ€” Left / Down / Up / Right
  • <span>w</span> โ€” Jump to the beginning of the next word
  • <span>b</span> โ€” Jump to the beginning of the previous word
  • <span>0</span> โ€” Move to the beginning of the line
  • <span>$</span> โ€” Move to the end of the line
  • <span>gg</span> โ€” Go to the top of the file
  • <span>G</span> โ€” Go to the bottom of the file
  • <span>:line_number</span> โ€” Jump to a specific line (e.g., <span>:50</span>)

๐Ÿ’ก Tips

  • <span>u</span> โ€” Undo the last operation
  • <span>Ctrl + r</span> โ€” Redo the last undo
  • <span>v</span> + move cursor + <span>d</span> โ€” Delete selected text in Visual Mode
  • <span>v</span> + move cursor + <span>y</span> โ€” Copy selected text in Visual Mode
  • <span>:set number</span> or <span>:set nu</span> โ€” Show line numbers
  • <span>:set nonumber</span> or <span>:set nu!</span> โ€” Hide line numbers
  • <span>:help</span> โ€” View help documentation

๐Ÿ“š Example: Creating and Saving a File

vim example.txt
  1. Press <span>i</span> to enter Insert Mode and type the content:

    Hello, this is a test file.
    
  2. Press <span>Esc</span> to return to Normal Mode.

  3. Type <span>:wq</span> and press Enter to save and exit.

Leave a Comment