Intro to Vi - RobotGirls/FTC-Team-25 GitHub Wiki

vi is a text editor. Although most of your FTC code will be edited in Android Studio, it's helpful to know how to make quick edits in vi.

There are two modes to vi

  • Command mode
    • Command mode is used to move through text, search for words, save a file, etc. This mode covers everything except inserting code
    • Press Esc to go from Insert mode to Command mode
  • Insert mode
    • Insert is only for inserting text into a file.
    • Using i, I, a, A, cw, o, O, or R will place you into insert mode from command mode.
  • The easiest thing to do if you get confused in Vi is to press ESC a couple of times and start over with what you were doing

For common vi commands, see Vi Quick Reference

Create a file using vi

Open a terminal and and the command prompt, type:

$ cd
$ vi vi_practice
  • vi will create the file “vi_practice” for you.

  • Press i to switch to input mode

  • Type something like, "VI is great! I think I'll be using vi from now on instead of Word”

  • Press to add lines

  • Type some more text

To save the file that you are in:

  • Press ESC to switch into command mode
  • Type :wq to write (save) and quit the file
    • Notice the : before the wq
  • Press ENTER

Copy a large file to your home directory so that you can play around with some more vi commands. We'll copy over the vi_playground from ~/Desktop/FIRST/software/vi_playground file for this exercise. To do this:

$ cd 
$ cp ~/Desktop/FIRST/software/vi_playground my_playground

Navigation, copy/paste and editing in vi

Edit the file, but start at the bottom of the file:

$ vi + my_playground 
  • Opening a file with + opens the file and places the cursor at the end of the file. This is handy for long config files

Go to the first line of the file:

  • :1
    • Note the colon : before the 1

Go to line 10, add a new line, and add in some text:

  • :10
  • Press the o key

Add the following text:

##
## A sample comment
##

Delete the three lines you just created:

  • Move to the first line of new text
  • Press ESC
  • Press dd to delete a line
  • Repeat until the text is gone

Save the file, but don’t exit:

  • :w
  • Press ENTER

Copying and pasting text

Go to line 12, copy 3 lines of text, go to the bottom of the file, place the text there:

  • Press ESC (go to command mode)
  • Type :12 (go to line 12 of the file)
  • Type 3yy (“yank” 3 lines of text and place in copy buffer)
  • Type G (go to the end of the file)
  • Type p (place the contents of the copy buffer here)

If want to undo this you would type (in command mode):

  • u

Go to the top of the file, replace all occurrences of “directory” with “folder”, but prompt for each change:

  • ESC
  • :1
  • :%s/directory/folder/gc

Type y or n to a few prompts. Exit this mode by pressing Ctrl+C and ENTER.

Go to line 1, search for “blue”, move to the end of the line, add some text:

  • ESC
  • :1
  • /blue
  • Type A
  • “text here”
  • ESC

Now let’s exit from the file and not save the few changes we’ve made.

  • :q!
    • Exercise question: why did we add !?

Additional exercises

vi is extremely powerful as an editor, but not necessarily intuitive. The best way to get good at using vi is to practice.

Let's do the following:

$ cd 
$ vi my_playground

Exercises

In this file practice some of the following items:

  • Moving around:
    • By word (w or b)
    • End of line (A)
    • Start of line
    • Top of file (1G)
    • Bottom of file (G)
    • To an absolute line number (:n)
  • Copying and pasting multiple lines (use vi commands, nyy)
    • Copying and pasting single lines (use vi commands, yy & P or p)
    • Copying and pasting multiple lines (use your mouse buffer)
    • Copying and pasting single lines (use your mouse buffer)
    • Search for items backwards and forwards (?string or /string)
    • Replacing text (:%s/pattern /string /flags)

Additional exercises

⚠️ **GitHub.com Fallback** ⚠️