Wednesday, March 12, 2008

VIM-Text Editor

Vim's manual:
http://vimdoc.sourceforge.net/htmldoc/usr_toc.html
Vim Mode
http://vimdoc.sourceforge.net/htmldoc/intro.html#vim-modes-intro
Vim quick reference Card
http://tnerual.eriogerg.free.fr/vimqrc.html
Linux Gym 2
Q1 :Insert text
Within your "ch2-vim" directory, create a file called "hw.txt", with only one line of text saying: "hello world" (without the quotes) and save the file.

1. Starting Vim

From the command line you can start editing a file called "test.txt" (whether the file "test.txt" exists or not) with the command:
vim test.txt

2. Switching Modes

There are eleven types of mode when running Vim, but they can be categorised as two types: normal modes and insert modes.

Normal mode: When you start Vim you will be in normal mode, and at any time you can return to the normal mode by pressing the key at the top left-hand side of your keyboard.

Insert mode: There are many ways to enter the insert mode, but the simplest is to type i. After typing i all the characters you type will be inserted into the docuement.

3. Exiting Vim

To exit Vim and return to the command-line:

  1. Go to normal mode by typing
  2. Save and Exit: type :wq which stands for "write and quit".
  3. Exit without saving: typing :q! returns to the command-line without saving the changes you have made to the file
Q2:Join lines
Copy the file "/usr/local/linuxgym-data/vimdata/wordlist.txt" into your "ch2-vim" directory. Edit the file so that all of the text is on a single line with only a space (not a newline) between the words.

A: cp /usr/local/linuxgym-data/vimdata/wordlist.txt .
or
cp /usr/local/linuxgym-data/vimdata/wordlist.txt ./

Don't forget the dot
. at the end. Remember, in UNIX, the dot means the current directory.
Q4:
Deletion by line numbers
Copy the file "/usr/local/linuxgym-data/gutenberg/0ws3110.txt" into your "ch2-vim" directory. Starting at line 50 delete 4000 lines (including line 50) - and save the file. Make absolutely no other changes.
A :
Move to 50 line
then press "4000dd"
Q5:
Cut and paste by line numbers
Copy the file "/usr/local/linuxgym-data/teeny/3mwsm10.txt" into your "ch2-vim" directory. Delete the first 10 lines and append them to the end of the file. Save the file and make no other changes.
A:
delete first 10 lines --> press : 10dd
move to the end of the text ,press : p
Q:Search and replace strings
In the next two questions we have to distinguish between words and strings. A string is a sequence of characters, while a word is a sequence of letters within a string, surrounded by non-word characters. For example, the string "This is a testing time" contains the words "This", "is", "a", "testing" and "time". It does not contain the word "test", but it does contain the string "test".

Copy the file "/usr/local/linuxgym-data/vimdata/hermit.txt" into your "ch2-vim" directory. Replace every occurence of the string "and" with the string "OR". Ensure you make

A
Press
:%s/and/OR/gc -->Confirm every substitution.

Q9:Cut and paste by markers
Copy the file "/usr/local/linuxgym-data/teeny/4mwsm10.txt" into your "ch2-vim" directory. Cut (remove) the lines between (and containing) those with "START HERE" and "UPTO HERE" and insert them at the marker "PASTE HERE". Save the file and make no other changes
A:
Type ':11,64d'

Then 'Paste'--->Shift P



:e![cr] says "I made a right mess of that - abandon my changes and re-read the input file

No comments: