Vim Text Editor Guide

2 minute read

Updated:

Introduction

Vim is a free and open-source, screen-based text editor program. In many Linux distributions is the default text editor. In this blog, I’m showing you the vim text editor commands to be more productive using vim as a beginner, because vim is a great choice among Linux users but is difficult to use as a newbie.

Vim Mode

There are three modes in vim, that is the command, insert, and visual mode.

Vim Commands

  1. Open a shell(bash) and write vim, it will open the text editor, in command mode.
  2. To insert text in vim press i, now you are in insert mode, and enter some text.
  3. To save your text, exit from insert mode by pressing the Esc key, which will change the vim to command mode.
  4. Now save your text with :w followed by a filename.
  5. Now you can quit the text editor using :q will save your text.
  6. Using shift+A will enter in insert mode but at the end of the line at the cursor position.
  7. To save and quit at a same time use :wq
  8. To use the previous commands enter : and use up or down arrow keys.
  9. To undo, press u.
  10. To delete a text character at the cursor’s current position, press x.
  11. To delete a whole line at cursor position, press dd.
  12. To copy content (text) from another file, use :r filename will copy all content to your file. If both files are not in the same directory use the full path of the file.
  13. To move up, down, left, or right use h, j, k, l in command mode.
  14. To move up, down, left, or right in insert mode use arrow keys.
  15. To suspend the vim to the background, pressing ctrl+z in command mode will suspend the text editor and you will be shifted to your shell. To activate your vim after doing some jobs in your shell press fg which means foreground, now your Linux shell will open vim as it is.
  16. To use the Linux command while your text editor is also open, press :! followed by Linux command.
  17. To navigate multiple files at a time use :e and filename, which will open another file. use :bp or :bn to switch between files and use :bd to close the current file.
  18. To move at beginning of the line at cursor position press 0 and press $ to move at the end of the line.
  19. To copy and paste text, press v to enter in visual mode and highlight the text with arrow keys and press y to copy, press p to paste where you want.
  20. To replace a word in a file at all locations in command mode, press :%s/word/word/g the first word you want to change followed by a word to insert.
  21. To split two files at a time open the first file then the second file with :split and filename will open the file horizontally, to switch between files use ctrl+ww.
  22. To open the file vertically open the first file and then the second file with :vsplit filename will open a file vertically and switch between open files use ctrl+ww.
  23. To set line number use :set number and use :set nonumber will remove the line numbers.
  24. To edit two files at the same use vim -o file1 file2 will open both files horizontally.
  25. To edit two files at the same time vertically use vim -O file1 file2.

Tags:

Categories:

Updated:

Comments