Linux Vi Commands: Complete Guide

Vi is a text editor widely used in Linux systems. It utilizes a command-line interface that can be directly accessed within the terminal. Vi offers a variety of commands and functions, and below is a detailed explanation of Vi commands.

  1. Access the Vi editor:
  2. Open a file: vi filename.
  3. Create a new file: vi newfilename
  4. Switch mode:
  5. Command mode is the default mode in Vi after entering, where you can save and exit files. Pressing the Esc key allows you to enter command mode.
  6. Insert mode: Press the keys i, a, or o in command mode to enter insert mode, where you can insert, delete, or modify text.
  7. Ex mode, accessed by pressing the colon key in command mode, allows for executing complex commands and operations.
  8. Cursor moving:
  9. Move one character to the left.
  10. j: Move down one line.
  11. k: move up one line.
  12. Move one character to the right.
  13. Move to the beginning of the line.
  14. $:Move to the end of the line.
  15. gg:Go to the beginning of the file
  16. G: Scroll to the end of the file.
  17. Press Ctrl + F to scroll down.
  18. Ctrl + B: Scroll up page.
  19. Editing operation:
  20. i: Inserting text in front of the cursor.
  21. Insert text after the cursor.
  22. o: Insert a new line below the current line and enter insert mode.
  23. Insert a new line above the current line and enter insert mode.
  24. x: Delete the current character.
  25. dd: delete the current line
  26. yy: Duplicate the current line.
  27. p: Content copied and pasted
  28. Search and replace:
  29. Keyword: Search for keyword downwards.
  30. Key term: search key term upwards
  31. n: navigate to the next matching item
  32. N: locate the previous matching item
  33. Replace the first occurrence of the old content with the new content on the current line.
  34. Replace all matching items in the current line with the new content.
  35. Replace all occurrences of “old content” with “new content” in the entire file.
  36. Save and exit
  37. :w Save the file without exiting Vi.
  38. :q: Quit Vim
  39. :wq: Save the file and exit Vi.
  40. Do not save the file and exit Vi forcefully.

These are just a few of the Vi commands; Vi also has many other advanced features and commands. Hopefully these basic commands will help you get started with using Vi editor.

bannerAds