Vi: Feature-Rich or Over-Complicated?

As a die-hard nano fan this was a tough article to write however I think it is valuable information nonetheless. By default most, if not all, Linux distributions come with vi pre-installed, but the same cannot be said about nano. Sad I know.
That being said, I think it’s about time to learn the ways of the grown-up text editors such as Vi and even Vim. Vi is a popular command-line text editor found on Unix and Unix-like systems. Known for its complicated navigation, people often comment about “getting stuck in vi” as the escape commands are not as intuitive as something like nano or gedit. Regardless if you think the text-editor is needlessly complicated, it does have some pretty great utilities especially for a built-in tool.
Here are the key concepts and commands you need to know:
There are Two Modes:
  • Command Mode: This is the default mode when you open vi. In this mode, you can navigate through the document and issue commands.
  • Insert Mode: In this mode, you can type and edit text.
Navigation:
  • Moving the Cursor: Use the arrow keys (up, down, left, right) to move the cursor in command mode. (No arrow keys on your keyboard? H,J,K,L can be used to move the cursor in navigation mode)
  • Jumping:
    • 0 (zero) moves the cursor to the beginning of the current line.
    • $ moves the cursor to the end of the current line.
    • gg moves to the first line of the document, and G moves to the last line.
Editing:
  • Inserting Text: Press i to enter insert mode at the current cursor position.
  • Appending Text: Press a to enter insert mode after the current cursor position.
  • Deleting Text:
    • x: Deletes the character under the cursor.
    • dd: Deletes the entire line.
  • Copying and Pasting:
    • yy or Y: Copies the current line.
    • p: Pastes the copied text below the current line.
  • Undo and Redo:
    • u: Undoes the last change.
    • Ctrl + r: Redoes the undone change.
Saving and Quitting:
  • Saving:
    • :w: Saves the changes.
    • :wq: Saves the changes and quits vi.
    • :x: Alternative means to Save and quit.
  • Quitting:
    • :q: Quits vi (only if there are no unsaved changes).
    • :q!: Quits vi without saving changes.
Searching:
  • /search_term: Searches forward for “search_term”.
  • ?search_term: Searches backward for “search_term”.
  • Press n to find the next occurrence of the search term.
Replacing Text:
  • :s/old_text/new_text: Replaces the first occurrence of “old_text” with “new_text” in the current line.
  • :%s/old_text/new_text/g: Replaces all occurrences of “old_text” with “new_text” in the entire document.
Other Useful Commands:
  • :set number: Displays line numbers.
  • :set nonumber: Hides line numbers.
  • :help: Opens vi’s help system.
Vi is a powerful text editor with a steep learning curve but offers great efficiency once you get familiar with its commands and shortcuts. The only way to improve your skills with vi are practice, practice, practice. Try it out and feel free to reach out if you have thoughts.

Leave a comment