"

Level 2 Linux Exercises

Vim Quick Reference

Make sure to save your work frequently!
  1. To begin editing a file: vim file_name.extension
    1. For example: vim prog1.cpp
  2. Command Mode
    To navigate in the document to prepare to edit here are a few of the choices available:

    1. Go to the first line 1G
    2. Go to the first line H
    3. Go to the last line L
    4. Move the cursor down one line j
    5. Move the cursor up one line k
    6. Move the cursor right one character l (lower case L)
    7. Move the cursor left one character h
    8. Go to the beginning of a line 0 (a zero)
    9. Go to the first non-black character ^
    10. Go to the end of the line $
    11. Go forward one full screen control f
    12. Go backwards one full screen control b
    13. Go forward half a screen control d
    14. Go backwards half a screen control u
  3. Other command mode options are:
    1. Undo the last change u
    2. To delete an entire line dd
    3. To copy the current line yy or  Y
    4. To paste the copied line(s) p
  4. Insert mode
    To enter insert mode; once insert mode has been entered, you can type normally inserting text. When done, you will need to exit insert mode with the escape key or by holding down the control key and pressing c at the same time:

    1. Insert before the cursor i
    2. Append after the cursor a
    3. Insert at the beginning of the line I
    4. Append at the end of the line A
  5. To end insert mode, press escape key or control c
  6. Last line mode commands
    Options for saving your work and quitting:

    1. To save your work :w
    2. To save changes and exit (quit) :wq
    3. To quit without saving :q!  (**all changes will be lost!)
  7. Navigation options at last line mode include:
    1. Go to a particular line :42 go to line # 42
    2. Find some text in the file /text
    3. Find the next occurrence /
    4. Search and replace :%s /to_replace/replace_with/<flags>
      flags:g // replace all
      i // case insensitive
      c // confirm before doing each replacement
  8. Visual Mode
    To enter Visual mode: v
  9.  Once in Visual mode, we can cut, copy and paste:
    1. To delete text and copy it to the clipboard: d
    2. To copy (yank) text: yy
    3. To paste text that has been copied: p
  10. Or we can alter indentation:
    1. To add indentation to the right: >
    2. To reduce indentation (move to the left): <
  11. To end visual mode, press escape key or control c
  12. Advanced vim Commands:
    Split Screen: One of the really great features is working in split screen mode; this allows you to view multiple files simultaneously

    1. Split horizontally: vim  –o  file1.cpp  file2.cpp 
    2. Split vertically: vim  –O  file1.cpp  file2.cpp 
    3. Split all of your .cpp files: vim  –O  *.cpp
    4. Switch between screens: <cntrl> ww  
  13. Other Commands for multiple files
    1. Open a file next to the current code, vertically :vs filename.extension
    2. Open a file below the current code, horizontally :sp filename
    3. Save and close all open files :wqa
    4. Open a file in a new buffer :e filename.extension
    5. Cycle through available files tab

 

 

License

CS Linux & Vim Manual Copyright © by Karla Fant. All Rights Reserved.

Share This Book