Edit files on Linux terminal using vi or vim

Linux editors

The early Unix editors were vi and emacs.  The Enhanced version of vi is called vim.  More recently the more friendly editor nano is sometimes available on Linux.

One way to learn vim is vimtutor a program that usually gets installed when you install the normal vim/gvim package. You should be able to just run "vimtutor" on the command line.

vi basics

To move the cursor, press the h,j,k,l keys as indicated.
             ^
             k              Hint:  The h key is at the left and moves left.
     < h       l >                 The l key is at the right and moves right.
                                   The j key looks like a down arrow.
             v

Note you can often also use the arrow keys to move around.

1. The cursor is moved using either the arrow keys or the hjkl keys.
         h (left)       j (down)       k (up)       l (right)

2. To start Vim from the shell prompt type:  vim FILENAME <ENTER>

3. To exit Vim type:     <ESC>   :q!   <ENTER>  to trash all changes.
                   or type:     <ESC>   :wq   <ENTER>  to save the changes.

4. To delete the character at the cursor type:  x

5. To insert or append text type:
         i   type inserted text   <ESC>         insert before the cursor
         A   type appended text   <ESC>         append after the line

6. To delete from the cursor up to the next word type:    dw
7. To delete from the cursor to the end of a line type:    d$
8. To delete a whole line type:    dd

9. To repeat a motion prepend it with a number:   2w
10. The format for a change command is:
               operator   [number]   motion
     where:
       operator - is what to do, such as  d  for delete
       [number] - is an optional count to repeat the motion
       motion   - moves over the text to operate on, such as  w (word),
                  $ (to the end of line), etc.

11. To move to the start of the line use a zero:  0

12. To undo previous actions, type:           u  (lowercase u)
     To undo all the changes on a line, type:  U  (capital U)
     To undo the undo's, type:                 <CTRL>R

13. To put back text that has just been deleted, type   p .  This puts the
     deleted text AFTER the cursor (if a line was deleted it will go on the
     line below the cursor).

14. To replace the character under the cursor, type   r   and then the
     character you want to have there.

15. The change operator allows you to change from the cursor to where the
     motion takes you.  eg. Type  ce  to change from the cursor to the end of
     the word,  c$  to change to the end of a line.

16. CTRL-G  displays your location in the file and the file status.
             G  moves to the end of the file.
     number  G  moves to that line number.
            gg  moves to the first line.

17. To search typing /  followed by a phrase searches FORWARD for the phrase.
     Typing  ?  followed by a phrase searches BACKWARD for the phrase.
     After a search type  n  to find the next occurrence in the same direction
     or  N  to search in the opposite direction.
     CTRL-O takes you back to older positions, CTRL-I to newer positions.

18. Typing  %  while the cursor is on a (,),[,],{, or } goes to its match.

19. To substitute (search and replace) new for the first old in a line type    :s/old/new

20. Type  o  to open a line BELOW the cursor and start Insert mode.
     Type  O  to open a line ABOVE the cursor.

21. Type  a  to append or insert text AFTER the cursor.
     Type  A  to insert text after the end of the line.

22. The  e  command moves to the end of a word.

23. The  y  operator yanks (copies) text,  p  puts (pastes) it.

24. Typing a capital  R  enters Replace mode until  <ESC>  is pressed.

25. Typing ":set xxx" sets the option "xxx".  Some options are:
        'ic' 'ignorecase'       ignore upper/lower case when searching
        'is' 'incsearch'        show partial matches for a search phrase
        'hls' 'hlsearch'        highlight all matching phrases
     You can either use the long or the short option name.

Vim has many more features than Vi, but most of them are disabled by
  default.  To start using more features you have to create a "vimrc" file.

26. Start editing the "vimrc" file.  This depends on your system:
        :e ~/.vimrc             for Unix
        :e $VIM/_vimrc          for MS-Windows

27. Now read the example "vimrc" file contents:
        :r $VIMRUNTIME/vimrc_example.vim

28. Write the file with:
        :w

  The next time you start Vim it will use syntax highlighting.