Linux text editor – Vim

 

Vim is the editor of choice for many developers and power users. It's a "modal" text editor based on the vi editor written by Bill Joy in the 1970s for a version of UNIX. It inherits the key bindings of vi, but also adds a great deal of functionality and extensibility that are missing from the original vi.

When you run vim filename to edit a file, Vim starts out in command mode. This means that all the alphanumeric keys are bound to commands, rather than inserting those characters. Typing j won't insert the character "j"--it will move the cursor down one line. Typing dd will delete an entire line, rather than inserting "dd."

To enter the insert mode, type i (for "insert") and now the keys will behave as you'd expect. You can type normally until you want to make a correction, save the file, or perform another operation that's reserved for command mode or last-line mode. To get out of insert mode, hit the Escape key.

Once you press Escape, you're in command mode again. What if you'd like to save your file or search through your document? No problem, press : and Vim will switch to last-line mode. Vim is now waiting for you to enter a command like :w to write the file or :q to exit the editor.

 

The Basics of Moving in Vim

  • h moves the cursor one character to the left.
  • j moves the cursor down one line.
  • k moves the cursor up one line.
  • l moves the cursor one character to the right.
  • 0 moves the cursor to the beginning of the line.
  • $ moves the cursor to the end of the line.
  • w move forward one word.
  • b move backward one word.
  • G move to the end of the file.
  • gg move to the beginning of the file.
  • `. move to the last edit.

Editing Vim Style

  • d starts the delete operation.
  • dw will delete a word.
  • d0 will delete to the beginning of a line.
  • d$ will delete to the end of a line.
  • dgg will delete to the beginning of the file.
  • dG will delete to the end of the file.
  • u will undo the last operation.
  • Ctrl-r will redo the last undo.

Copying and Pasting

  • v highlight one character at a time.
  • V highlight one line at a time.
  • Ctrl-v highlight by columns.
  • p paste text after the current line.
  • P paste text on the current line.
  • y yank text into the copy buffer.