Sunday, March 29, 2009

vi keyboard shortcuts



vi keyboard shortcuts
Cursor control and position
Editing
File Handling
.
Cursor control and position
Lefth
Downj
Upk
Rightl (or spacebar)
Forward one wordw
Back one wordb
End of worde
Beginning of current sentence(
Beginning of next sentence)
Beginning of current paragraph{
Beginning of next paragraph}
Beginning of current section[[
Beginning of next section]]
Start of current line0
End of current line$
First non-white character of current line^
First character of next line+ or RETURN
First character of previous line-
character n of current linen |
Top line of current screenH
Middle line of current screenM
Last line of current screenL
n lines after top line of current screennH
n lines before last line of current screennL
Forward one screenCtrl-F
Back one screenCtrl-B
Down half a screenCtrl-D
Up half a screenCtrl-U
Display another line at bottom of screenCtrl-E
Display another line at top of screenCtrl-Y
Redraw screen with cursor at topz RETURN
Redraw screen with cursor in middlez .
Redraw screen with cursor at bottomz -
Redraw screen without re-positioningCtrl-L
Redraw screen without re-positioningCtrl-R
Search for text (forwards)/text
Repeat forward search/
Search for text (backwards)?text
Repeat previous search backwards?
Repeat previous searchn
Repeat previous search, but it opposite directionN
Go to line n after text/text/+n
Go to line n before text?text?-n
Find match of current parenthesis, brace, or bracket.%
Display line number of cursorCtrl-G
Move cursor to line number nnG
Move cursor to line number n:n
Move to last line in file
G
Editing
Append to end of current lineA
Insert before cursori
Insert at beginning of lineI
Open line above cursoro
Open line below cursorO
End of insert modeESC
Insert a tabCtrl-I
Move to next tab positionCtrl-T
Move back one characterBackspace
Delete current lineCtrl-U
Quote next characterCtrl-V
Move back one wordCtrl-W
Change wordcw
Change linecc
Change from current position to end of lineC
Delete current linedd
Delete n linesndd
Delete remainer of lineD
Delete worddw
Delete rest of paragraphd}
Delete back to start of lined^
Delete up to first occurance of patternc/pat
Delete up to next occurance of patterndn
Delete up to and including a on current linedfa
Delete up to, but not including, a on current linedta
Delete up to last line on screendL
Delete to end of filedG
Join two linesJ
Insert buffer after cursorp
Insert buffer before cursorP
Replace character with xrx
Replace text beginning at cursorRtext
Substitute characters
Substitute n charactersns
Substitute entire lineS
Undo last changeu
Restore current lineU
Delete current cursor positionx
Delete back one characterX
Delete previous n charactersnX
Repeat last change.
Reverse case~
Copy current line to new buffery
Copy current lineyy
Copy current line into buffer x"xyy
Delete and append into buffer x"Xd
Put contents of buffer x"xp
Copy up to next section headingy]]
Copy to end of wordye
File Handling
:wWrite file
:w!Write file (ignoring warnings)
:w! fileOverwrite file (ignoring warnings)
:wqWrite file and quit
:qQuit
:q!Quit (even if changes not saved)
:w fileWrite file as file, leaving original untouched
ZZQuit, only writing file if changed
:xQuit, only writing file if changed
:n1,n2w fileWrite lines n1 to n2 to file
:n1,n2w >> fileAppend lines n1 to n2 to file
:e file2Edit file2 (current file becomes alternate file)
:e!Reload file from disk (revert to previous saved version)
:e#Edit alternate file
%Display current filename
#Display alternate filename
:nEdit next file
:n!Edit next file (ignoring warnings)
:n filesSpecify new list of files
:r fileInsert file after cursor
:r !commandRun command, and insert output after current line

Friday, March 13, 2009

RegularExpressionValidator control

How to use the RegularExpressionValidator control?

The purpose of the RegularExpressionValidator control is to filter out unwanted or invalid input. It can be the first line of defense against input that is not formatted the way you want to see it. Correctly formatted expressions get through with no fanfare. Exceptions, however, cause the control to display its error message and the postback process is halted.

To demonstrate how this control works, let's drop a few controls on a Web form. I'm using Visual Studio .NET with code-behind and my language is VB. I've set my form to Flow Control but all that isn't really necessary to understand the principles involved. VS.NET has declared my control automatically with the following statement: Protected WithEvents RegularExpressionValidator1 As System.Web.UI.WebControls.RegularExpressionValidator

The rest is as easy as 1,2,3.

1. First I drop a text box on my form. This is where I enter my test expression. I'll give the control an id of "txtText."

<asp:TextBox id="txtTest" runat="server"> </asp:TextBox>

2. Next I drop a RegularExpressionValidator control on the form. I give this control an ID of "RegularExpressionValidator1" since I'm not feeling very creative today. I set the "ControlToValidate" property to the ID of the control I want to validate (which is the textbox I just created). I set the "ErrorMessage" property to what I want to display if the expression is not valid. I set the "ValidExpression" property to a regular expression that I want to use to compare against the input. In this case, I've just set the expression to the letter "m."

<asp:RegularExpressionValidator id="RegularExpressionValidator1"

runat="server" ControlToValidate="txtTest" ErrorMessage="Sorry.

You are not a valid expression!" ValidationExpression="m"> </asp:RegularExpressionValidator>

3. Next I drop a button control on the form. This is just so we have a place for the cursor to go after we leave the text box.

<asp:Button id="Button1" runat="server" Text="Submit">

</asp:Button>

Now we are ready to test our control that should only allow the letter "m" (lower case) to get by. If we enter "m" and press TAB or click the SUBMIT button, everything looks good -- no error messages. If we leave the text box blank, there is no error message, either because the control only checks input that exists, not the existence of input. If, however, we enter any other character or digit the conrol's error message will be displayed. In this case, the error message is: "Sorry. You are not a valid expression!"

Now that we know how to use the control, what we really need is a primer on regular expressions. Then we can easily drop a RegularExpressionValidator control on our form, associate it with an entry in a text box and set the "ValidationExpression" property to what we want the input to look like.

Regular expressions

The subject of regular expressions is often confusing but we are going to take an approach that will give you a basic understanding upon which you can build. Becoming proficient with regular expressions takes a lot of practice, just like anything else.

Regular expressions let us to search for certain patterns. In the case of our validator control, if we find the pattern in the text box, the control is satisfied and let's the text through to the promise land. If we do NOT find that pattern, the control is not happy and displays its error message. (This is not all we can do with regular expressions. We can also "search and replace" and reformat text. Just be aware that you can use regular expressions to find all instances of some word or phrase and replace it with another or reformat it. You can use them to mine documents for e-mail addresses or URLs.

Character matching

The easiest kind of matching we can do is "character matching." That's what we did when we put the letter "m" in our validator control. We are simply asking if the text is the letter "m" and nothing else. So the lowercase letter "m" passes but the uppercase "M" does not. If you enter "mom," it does not pass because "mom" is not "m" -- it is something more than just "m."

Character matching is not limited to a single character. If you use the regular expression "mom," then the only expression that matches will be "mom." "MOM" will not work, "mommy" will not work and "I want my mom" will not work because none of these expressions are exactly equivalent to "mom." OK, I think we are clear on that point.

A period is a special character that matches any single character. So the regular expression "m.m" would match "mom" or "mam" or "m9m."

You can search for a string that has a single character from a group of predetermined characters. For example: "m[ao]m" would match up with "mam" or "mom" because the middle letter is in the group [ao], but "m9m" would not match because "9" is not an "a" or an "o."

You can search for a string that has a single character that is in a range. For example: "m[a-y]m" will match up with "mam" or "mbm" or "mcm" or any other letter in the middle so long as it is in the range [a-y]. The letter "z" is not in that range and all uppercase letters are not in that range.

Here is a list of the most common special characters that relate to character matching:

{x} 

Match exactly x occurrences of a regular expression. 

d{5}

Matches five digits such as 12345. 

{x,} 

Match x or more occurrences of a regular expression. 

s{2,} 

Matches at least two-space characters.  

{x,y} 

Matches x to y number of occurrences of a regular expression. 

d{2,3} 

Matches at least two but no more than three digits. 

Match zero or one occurrences. Equivalent to {0,1}.

as?b 

Matches "ab" or "a b".

Match zero or more occurrences. Equivalent to {0,}

Match one or more occurrences. Equivalent to {1,}

Repetition matching

The question mark is a special character that matches zero or one instances of the character that precedes it. So the regular expression "moms?" would match "mom" because the letter "s" appears zero times after "mom." It would match "moms", of course.

The asterisk is a special character that matches zero or more instances of the character that precedes it. So the regular expression "moms*" would match "mom" because the "s" appears zero times. It would match "moms" because the "s" appears once. It would also match "momsssssssss."

The plus sign is a special character that matches one or more instances of the character that precedes it. So the regular expression "moms+" would match "moms" or "momsssss" but not "mom" because the s has not occurred at least once.

If you wanted to find a match on n instances of the preceding character, you can use {n}. For instance, the regular expression "moms{3}" would only match "momsss" because there are exactly three instances of the letter "s". It would not match "moms" or "momss." If you wanted to match the word "moon," you could use the regular expression "moon" (the most direct method) or you could use "mo{2}n."

Here are some common examples of repetition matching:

{x} 

Match exactly x occurrences of a regular expression. 

d{5}

Matches five digits such as 12345. 

{x,} 

Match x or more occurrences of a regular expression. 

s{2,} 

Matches at least two-space characters.  

{x,y} 

Matches x to y number of occurrences of a regular expression. 

d{2,3} 

Matches at least two but no more than three digits. 

Match zero or one occurrences. Equivalent to {0,1}.

as?b 

Matches "ab" or "a b".

Match zero or more occurrences. Equivalent to {0,}

Match one or more occurrences. Equivalent to {1,}

Matching special characters

We've already introduced several "special characters." Special characters are those which have a special meaning. In the above discussion, the period, the asterisk and the plus sign are all "special characters." To match a special character, you have to precede the special character with a "" in the regular expression. Here is a list of the most common ways to match a character which would otherwise have special meaning.

Matches a new line. 

Matches a form feed.

Matches a carriage return.

Matches horizontal tab. 

Matches vertical tab. 

Matches ?

*

Matches *

+

Matches +

.

Matches .

  

Matches

Alternation and grouping

Alternation and grouping is used to develop more complex regular expressions. Grouping a clause to create a clause. May be nested. "(ab)?(c)" matches "abc" or "c."

Alternation combines clauses into one regular expression and then matches any of the individual clauses. "(ab)|(cd)|(ef)" matches "ab" or "cd" or "ef."

More examples and references

1. You can find many ready-made regular expression samples at http://www.regexlib.com/. These include regular expressions for dates, e-mail addresses, URLs, ZIP codes and things like that. Some of these are better than others, so make sure you test and understand the expression before you put it into production.

2. Microsoft has a fairly dense, if somewhat disorganized, coverage of the subject here.

Conclusion

The RegularExpressionValidator Control is powerful and useful if you have a basic understanding of regular expressions. There are benefits to using this control over other methods. (1) Depending on the browser being used, client-side code will be generated for the validation. This means expressions will be validated at the client without having to make the round trip to the server. (Up-level browsers (IE 4.0+) will have the validation rendered in JavaScript/DHTML to enable client-side validation, while down-level browsers will provide strictly server-side validation.) (2) Pattern matching may be faster than other methods. For instance, a date validator is much faster than testing for a valid date using the IsDate() function.