Archive

Archive for January, 2008

Vim Search

January 24th, 2008

I assume that most people that use Vim know how to use the basic search functionality, such as / and ?. I am wanting to elaborate on those a bit.

<CR> represents the actual enter key. If /<CR> is used then the search is executed again using the previous pattern, just hitting ‘n’ also does
the same thing. If you need to go in the opposite direction then use ‘N’. Another quick way to search is by using ‘*’. If your cursor is underneath the
word you want to find, then just hit ‘*’ to go to the next occurrence, this is also in a Vim tip, can’t remember which one however (:he * will get you there
also).

The pound sign, ‘#’, can also be used to search for a word closest to the cursor, however instead of searching forwards, it will search backwards.

When using ‘*’ and ‘#’ the search will only match words, an example is the word foo will not match foobar. If you precede those with a ‘g’ then this will also find matches that are not a word, so using ‘g*’ as foo as the word, then foobar will be a match.

Programmers will be interested in :he gd and also :he gD. I will not discuss these here (possibly will in the future).

I also recommend turning the increment search on in your vimrc file (:he incsearch).

While searching it is by default that your search will wrap. Meaning that once your search hits the end of your buffer, then the search will continue at the beginning of the buffer. In order to turn this feature off and to stop searching once the end of the buffer is found turn wrapscan off, :help wrapscan will tell you more about it or just do ‘:set nowrapscan’.

To find the carrot symbol ‘^’ you need to escape it, such as ‘/foo\^bar’. This will find ‘foo^bar’. The carrot symbol is also used to match at the beginning of a line (hence the need to escape it), so to find ‘foo^bar’ at the beginning of a line the search pattern will be ‘/^foo\^bar’.

Author: gorshing Categories: Vim Tags:

Vim Reformat

January 24th, 2008

While I am typing in Vim I do not let the text wrap, I put in returns manually. This causes problems when I came back to it later and add in some text within the middle of paragraph. The line will continue on to the right margin and the text will wrap, then when I have to adjust the line I have to fix all the following lines to be ended correctly on the right side.

Well, using ‘gq’ you can somewhat auto format your lines. This will use what ‘textwidth’ is set to, if it is not set then the width of the screen is used up to 79. In order to format the current line ‘gqq’, to format the paragraph ‘gqip’ (ip for inner paragraph).

For further reading try :he auto-format and :he text-objects.

Author: gorshing Categories: Vim Tags:

Vim Range

January 24th, 2008

There are numerous commands that can accept a range preceding the command as well as others that range can follow the command. As the name implies you can restrict a command to execute within a particular number of lines.

One such example is with the substitute command. The following command will replace see with saw only in lines 3, 4, and 5.

:3,5s/see/saw

Some people tend to believe that the second number is a line count, but this is incorrect. As you can see in the preceding example, the first number indicates which line to start at, and the second number specifies which line to stop at.

As I stated in the beginning it is also possible to have a range follow the command. But a note taken from the help:

“The commands that accept a count are the ones that use a range but do not have a file name argument (because a file name can also be a number).”

To copy yet again from the help is an example with the range following the command, this will substitute ‘x’ with ‘X’ in the current line and four following lines:

:s/x/X/g 5

Another neat way to specify ranges are used with marks (:help mark). If you have a mark on a line, mark name is ‘a’, then you can delete the line that the mark is on by doing

:'ad&lt;enter&gt;

You can also use two marks as a range, if you had a mark ‘a’ and a mark ‘c’ then you can delete those marks and any lines in between by :’a,’cd<enter>

As I mentioned previously with line numbers, if the mark ‘a’ is located at a line that is following mark ‘c’, you will be shown an error message saying that the range is backwards.

Another special character for ranges is ‘%’, this translates to 1,$ (the dollar sign signifies the last line in the file). Using this you can easily delete all lines by:

:%d&lt;enter&gt;

Author: gorshing Categories: Vim Tags:

Vim Substitute Command

January 24th, 2008

The following command is the substitute command, you can learn more about this command by reading :help substitute (or if you prefer the shorthand version :he su). If you did the preceding you will notice that the help shows the substitute command as :[range]s[ubstitute]/{pattern}/{string}/ (the help also shows all of the possible flags, which I omitted here on purpose). Just in case you didn’t know, since range is enclosed in square brackets that means it is optional. Also you will notice that the :s can also be spelled out as :substitute, I am sure that most people will prefer the :s syntax.

To replace the first occurrence of aspirin with foo in the current line:

:s/aspirin/foo

To replace the first occurrence of aspirin with foo in every line:

:%s/aspirin/foo

You will notice the only difference in the two preceding lines is the % sign. The % sign specifies a range, basically saying every line in the file, but the help (:he range) says that it is equal to 1,$. You are probably confused with the dollar sign also which you probably think of as the last character on the line but $ is range command meaning last line in file. I will not continue more with ranges (to read more about ranges, see my range tutorial), I just wanted to show what all is possible and where to look.

You will notice that both of the operations above will only work once for each line. So if you have aspirin in a line more than once, then only the first instance will be changed.

In order to change every occurrence of aspirin to foo:

:%s/aspirin/foo/g

One problem that many users face is editing DOS/Windows files on Linux, when viewed with Vim you will see ^M at the end of the lines. One way to fix this problem is the following:

:%s/\r//g

The \r is the extra character that Windows adds for the end of line marker, there is an empty string to replace it with as noted with //

Some of the special characters can easily be handled by escaping them, just use a preceding \ in these cases. There are a few others which need an extra step, such is the case for the ? character. In order to replace this character with a string (such as foo) then one would use the following command:

:s/\v\?/foo/

Author: gorshing Categories: Vim Tags:

Being a Computer Scientist

January 23rd, 2008

At one of my previous places of employment, I made a comment which made numerous of my co-workers very angry at me. I felt as though I was completely justified in saying it, but I soon found out how much I upset them.

I believe I was talking about Bill Joy, I honesty can’t recall off the top of my head who I was talking about, but a co-worker asked who that was. I responded with a rhetorical question in awe that they didn’t know who I was talking about. I rattled off a few other (in my opinion) popular/famous Computer Scientists, as a line of questioning to see if they knew any others … which they did not. FYI, I believe I mentioned Alan Turing, Larry Ellison, Donald Knuth, and Steve Wozniak among others.

I then made a comment which made a few of them mad at me “I don’t understand why somebody being a programmer, a computer scientist, and not knowing the pioneers in our field”. The conversation continued for a short while, but it became readily apparent I was in the minority.

The Senior Lead at that time was Paul Carter, he agreed with me during the whole conversation but I was the one who took all the heat. I have kept in touch with him since leaving and received an email from him just the other day.

His email had a link to a blog post about Donald Knuth, and thought about that conversation. I am glad to see someone else who has a very similar stand as I do:

If you don't know who Knuth is, then you're not a programmer. If you're a programmer and you don't know who Knuth is, well... I have no idea what rock you've been hiding under, but you should probably be fired.
Author: gorshing Categories: Programming Tags:

PHP Equality and Identity Operators

January 1st, 2008

I am working on learning the Zend Framework, and along with that PHP as well (of course). I stumbled upon a code snippet on the internet which I didn’t realize would work. I have seen the use of the identify operator in PHP before but I soon realized I didn’t know all that I should. So I dived into this topic a little bit more and soon found out that more seasoned PHP programmers aren’t too fluent in the differences between the two operators and what is (and not) possible.

Most programmers are accustomed to the equality operator as noted by ‘==’. This operator performs a check on the operands and if they are equal (you will soon learn equal has a very broad definition) then it will return true. What I mean by a broad definition is that the PHP interpreter will perform a conversion based upon what the operand contains. In an attempt to keep my examples more simple I will be talking about numerics and strings.

If both operands are a string value, but both contain a numeric expression the interpreter will attempt to convert both expressions to a numeric value then perform the numeric comparison. If one of the operands is a numeric value and you compare it to a string type, the string is converted to a numeric value as well. If the string conversion fails, then a 0 is returned. As an example, if you run the following code, it will print out ‘1′.

<?php
  // This prints 1
  print ("000" == "0" && "0" == 0 && 0 == "cat");
?>

The following will result in a string comparison instead of a numeric comparison. The PHP interpreter recognizes “0″ as a numeric expression, but since “cat” is not a numeric expression then they are both left alone and compared as strings.

<?php
  // Does nothing
  print ("0" == "cat");
?>

If you are more comfortable with a strongly typed language such as Java or C++ then the identity operator is going to be what you would be more accustomed to. In PHP this is denoted as ‘===’, this performs tests as you are more familiar with. The identity operator will return true if both operands are equal and of the same type. The above two examples, if used with the identity operator would return false in all cases.

In the following example since $a is not of a valid numeric expression no conversion takes place and a string comparison is performed. Conversely $c is a valid numeric expression (see Scientific Notation) this is converted and evaluated.

<?php
  $a = "0f0"; // This is not hex, as it does not start with 0x, ex. "0xf0"
  $b = "000";
 
  // This fails
  print ($a == $b); // "0f0" == "000"
 
  $c = "0e0";
 
  // This is successful
  print ($c == $b); // "0e0" == "000"
?>

Just one more last example, here both variables are valid numeric expressions. They both evaluate to 0 using the scientific notation.

<?php
  $a = "0e2";
  $b = "0e3";
 
  // This is successful
  print ($a == $b);
?>

I hope this has shed some light on the subject. The following are some references which helped me understand this a little better.

BlueShoes PHP Syntax Exam

PHP Docs for Comparison Operators – And a comment which started me on this journey

Author: gorshing Categories: PHP Tags: