Ivor O’Connor

January 25, 2010

Navigating Log Files With VIM

Filed under: bash, cli, howto, Linux, tutorial, vi — ioconnor @ 6:40 pm

One of the main problems with VI, VIM and GVIM is the navigation between multiple files. The “:n” and “:rew” commands may get somebody through the basics. But seriously it’s not enough. The secret is writing little macros for the work being done. Often this is such a daunting task that most people haven’t got a clue. So hence this article. The idea presented here is to show how to make a navigation file so that once the cursor is over the file name in question, or information needed to identify the file, two keystrokes will take you to that file.

First there is the ~/.vimrc file or equivalent depending on the OS. Locate it. Add to this file the macros.

:map ** “zyy:@z<Enter>

This command maps the keystrokes “**”, the two asterisks hit in rapid succession, to the commands that follow it. The command that follows reads the line the cursor is on into the z buffer and then executes that buffer. This is very useful because now the commands you want to test can be written on a line and then executed by pressing “**”. It opens the world of macros to you.

So lets write a macro that will bounce the window between the current file and the last file you edited. Say you started gvim by typing gvim file1 file2 file3 and once in file1 you then typed :n and :n again. So you are now in file2. The goal of this macro is to let you move to file2 without doing a “:rew” and “:n”. So start a new line in file 3 and type in:

:map *; :e#<Enter>

Then press “**” over the line so this macro becomes available. Now if you press “*;” you’ll go to file2.

Now we are ready to write some macros to view log like files. I’ll start with the one I wrote yesterday. Websites require a file named “Sitemap.xml” which describes each file’s location, importance, and frequency they should be crawled by the search spiders. Each line has a format similar to this:

<url><loc>http://www.domain-name.com/index.html</loc><priority>0.50</priority><changefreq>weekly</changefreq></url&gt;

Three macros might be all that is needed to navigate a website via its Sitemap.xml. File navigation this way ensures the Sitemap.xml always stays relevant. So the first macro might be something like:

:map *S :e ~/path/Sitemap.xml<Enter>

This is simple enough to understand. When “*S” is typed it will take you to the Sitemap.xml file.

Now the following is all that is needed to edit the files listed in the Sitemap.xml by putting the cursor on the line containing the file and typing “*e”:

:map *e :w<Enter>$0fhf<mz0fml”ay`zo:e ~/path-to-website/<Esc>”ap<Esc>”zyyu:@z<Enter>

First the file is written out with this command “:w<Enter>”. Depending on your habits you may want to remove that.
Then the cursor is moved to the end of the line and then to the beginning with “$0”. There is history to this that is wasted on VIM/GVIM.
Then a search for the ‘h’ in “http” is done, to get to the start of the URL, with “fh” and then another search for the end of the URL with “f<”
Then that location is marked as location “z” with the command “mz”. Then we go back to the start of the file name which for my Sitemaps can be done for searching for the “m” in com. You may have to vary the search depending on your domain name. Then moving the cursor over to the “/”. So altogether it’s “0fml”.
Now yank the name of the file into the a buffer using the backwards tick “”ay`z”.
Now create a line with the command needed to edit the file just yanked “o:e ~/path-to-website/<Esc>”ap<Esc>”
Finally yank the newly created line into the z buffer, undo the line just created, and then execute the z buffer so the editor is now editing the file “”zyyu:@z<Enter>”

The next command sets the browser to the file in question:

:map *b :w<Enter>$0fhf<mz0fh”ay`zo:!gnome-open <Esc>”ap<Esc>”zyyu:@z<Enter>

These techniques can be used for almost anything to avoid typing names. In fact a list of all files you edit, in a centralized place, is all that’s needed to prevent ever repeatedly typing out a file name again. Once familiar with these techniques most of the files needing editing can be gotten to with two key strokes. This gives VI, VIM, and GVIM the ability to navigate files more easily than an IDE without losing all the power you surrender when using an IDE. And it’s all free!

Advertisements

1 Comment »

  1. thanks.. nice info

    Comment by filesearch.web.id — March 7, 2010 @ 10:58 pm


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: