Archive

Archive for the ‘Vim’ Category

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: