vi keyboard shortcuts | ||
| Cursor control and position | ||
| Editing | ||
| File Handling | ||
| . | Cursor control and position | |
| Left | h | |
| Down | j | |
| Up | k | |
| Right | l (or spacebar) | |
| Forward one word | w | |
| Back one word | b | |
| End of word | e | |
| 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 line | 0 | |
| 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 line | n | | |
| Top line of current screen | H | |
| Middle line of current screen | M | |
| Last line of current screen | L | |
| n lines after top line of current screen | nH | |
| n lines before last line of current screen | nL | |
| Forward one screen | Ctrl-F | |
| Back one screen | Ctrl-B | |
| Down half a screen | Ctrl-D | |
| Up half a screen | Ctrl-U | |
| Display another line at bottom of screen | Ctrl-E | |
| Display another line at top of screen | Ctrl-Y | |
| Redraw screen with cursor at top | z RETURN | |
| Redraw screen with cursor in middle | z . | |
| Redraw screen with cursor at bottom | z - | |
| Redraw screen without re-positioning | Ctrl-L | |
| Redraw screen without re-positioning | Ctrl-R | |
| Search for text (forwards) | /text | |
| Repeat forward search | / | |
| Search for text (backwards) | ?text | |
| Repeat previous search backwards | ? | |
| Repeat previous search | n | |
| Repeat previous search, but it opposite direction | N | |
| 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 cursor | Ctrl-G | |
| Move cursor to line number n | nG | |
| Move cursor to line number n | :n | |
| Move to last line in file | G | |
Editing | ||
| Append to end of current line | A | |
| Insert before cursor | i | |
| Insert at beginning of line | I | |
| Open line above cursor | o | |
| Open line below cursor | O | |
| End of insert mode | ESC | |
| Insert a tab | Ctrl-I | |
| Move to next tab position | Ctrl-T | |
| Move back one character | Backspace | |
| Delete current line | Ctrl-U | |
| Quote next character | Ctrl-V | |
| Move back one word | Ctrl-W | |
| Change word | cw | |
| Change line | cc | |
| Change from current position to end of line | C | |
| Delete current line | dd | |
| Delete n lines | ndd | |
| Delete remainer of line | D | |
| Delete word | dw | |
| Delete rest of paragraph | d} | |
| Delete back to start of line | d^ | |
| Delete up to first occurance of pattern | c/pat | |
| Delete up to next occurance of pattern | dn | |
| Delete up to and including a on current line | dfa | |
| Delete up to, but not including, a on current line | dta | |
| Delete up to last line on screen | dL | |
| Delete to end of file | dG | |
| Join two lines | J | |
| Insert buffer after cursor | p | |
| Insert buffer before cursor | P | |
| Replace character with x | rx | |
| Replace text beginning at cursor | Rtext | |
| Substitute character | s | |
| Substitute n characters | ns | |
| Substitute entire line | S | |
| Undo last change | u | |
| Restore current line | U | |
| Delete current cursor position | x | |
| Delete back one character | X | |
| Delete previous n characters | nX | |
| Repeat last change | . | |
| Reverse case | ~ | |
| Copy current line to new buffer | y | |
| Copy current line | yy | |
| 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 heading | y]] | |
| Copy to end of word | ye | |
File Handling | ||
| :w | Write file | |
| :w! | Write file (ignoring warnings) | |
| :w! file | Overwrite file (ignoring warnings) | |
| :wq | Write file and quit | |
| :q | Quit | |
| :q! | Quit (even if changes not saved) | |
| :w file | Write file as file, leaving original untouched | |
| ZZ | Quit, only writing file if changed | |
| :x | Quit, only writing file if changed | |
| :n1,n2w file | Write lines n1 to n2 to file | |
| :n1,n2w >> file | Append lines n1 to n2 to file | |
| :e file2 | Edit 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 | |
| :n | Edit next file | |
| :n! | Edit next file (ignoring warnings) | |
| :n files | Specify new list of files | |
| :r file | Insert file after cursor | |
| :r !command | Run command, and insert output after current line | |
Sunday, March 29, 2009
vi keyboard shortcuts
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.
n | Matches a new line. |
f | Matches a form feed. |
r | Matches a carriage return. |
t | Matches horizontal tab. |
v | 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.