Team with the best
Db2® LUW Performance Tools
company in the World

DB2 LUW Performance: Using the vi (Very Intuitive) editor

December 25, 2010, 10:09 am
Posted by Scott in DB2 Performance How-To
I recently spoke with a customer that was rehosting a mainframe DB2/CICS/COBOL application to pSeries AIX. I did this in the mid-90's. When you find yourself in the UNIX or Linux world, you need to know how to use the VI editor. Since I'm now stranded in the ATL airport on Christmas day, this mini VI class is my "gift" to the DB2 LUW community. icon_smile


From your command prompt, to begin using vi, simply type vi followed by the name of the file you want to edit:
$ vi myfilename.txt

vi has two basic modes of operation: command mode and input mode.

When you first start vi, you are in command mode and vi waits for your instructions. Many useful commands are preceded by the colon ":" character.

Some of the more useful commands include:

  • :set nu (this turns on line numbers)
  • :set nonu (this turns off line numbers)
  • :set showmode (now vi will tell you if it is in command mode or input mode)
  • :q (quit - to exit an edit session)
  • :q! (quit - to exit an edit session and abandon any changes you may have made)
  • :w (write aka save the file)
  • :wq (save the vile and exit)
If you are in INPUT mode and want to return to COMMAND mode, hit your ESC key. You will find that you will hit the ESC key quite a bit.

INPUT MODE

While in COMMAND mode, you switch to INPUT mode by hitting particular characters:
  • i - the letter i places you into INSERT mode. Characters you type will now be added to the file.
  • a - the letter a places you into INSERT mode with the APPEND option; characters you type are now added to the file after the current cursor position
  • R - the letter R places you into REPLACE or overstrike mode; now whatever you type replaces any characters. And yes, case is IMPORTANT in vi.
  • r - the lowercase letter r allows you to replace just ONE character and then it returns to command mode afterwards.
  • o - the lowercase letter o will Open a new line beneath the line of your current cursor location. You'll be in INSERT mode on a new line.
  • O - the uppercase letter O will Open a new line OVER the line of your current cursor location. You'll be in INSERT mode on a new line.
So, that's the basics. You can now start vi, use one of the INPUT mode commands to type some characters, then hit ESC (return to COMMAND mode), then type :wq to save your work and exit.

IMPORTANT COMMANDS

You'll probably need or want to navigate around within your file, so let's look at commands to help you get around next:
  • j - the lowercase j key will scroll down one line at a time for each time that you hit the key.
  • k - the lowercase k key will scroll up one line at a time for each time that you hit the key.
  • w - the lowercase w key will cause your cursor to jump forward one word at a time. I use "w" a lot.
  • If you turned on line numbers with the :set nu command, then you can jump to a specific line number by preceding the letter G with the line number you want to go to. For example, 100G will Go to line number 100.

Repeating commands

If you want to repeat a particular command some number of times, type the number of times to repeat followed by the command that you want to repeat. This is incredibly powerful and useful. Some examples:
  • 300j will scroll down 300 lines
  • 6w will advance forward by 6 words
  • 500k will scroll up 500 lines
Probably the most commonly used repeat command is the dot or period key. After executing a command, just hit the "." key to repeat it.

Deleting stuff

There will likely come a time when you want to get rid of something in a file. Here are your important commands:
  • x - the lowercase x character will delete one character. 30x will delete 30 characters. Be careful when you repeat a command that deletes data!
  • dw - the lowercase characters d and w together will delete the current word that the cursor is positioned on. 3dw will delete 3 words!
  • dd - type the lowercase d twice to delete the current line. 300dd will delete 300 lines! Careful!

Finding Stuff

vi will help you search forwards or backwards:
  • / - the slash character means you want to do a foward search for a string. Follow the / with the string you want to search for. For example, /ERROR. vi will take you forward to the first occurrence of ERROR that it finds. To find the next occurrence, type a lowercase n character.
  • ? - the question mark character will do a reverse or backwards search; that is, the contents of the file above the current cursor location will be searched. You can use "n" again to find "n"ext.

Find and Replace

Often times we have the need to replace strings in a file. This is tricky, but very powerful.

Type a colon ":" followed by the lowercase letter "s" (for substitute).
Follow the letter "s" with a slash "/"
Type the old string you want to get rid of
Type another slash "/"
Type the new string that you want to replace the old one with
Type another slash "/" followed by a lowercase letter g and hit enter!
Here's and example: :s/ERROR MESSAGE/SUCCESS Message/g

Well then, my time is up as my flight will soon be boarding. I hope this quick primer on the very intuitive (vi) editor will be helpful to many of you. Even experienced folks might find a few useful tricks in this.

Happy Holidays,
Scott

PS - Please check out The DB2Night Show™ Edutainment Webinar series at www.DB2NightShow.com

Printer friendly