Home > Articles > Certification > Other IT

Basic vi Skills for the Linux LPIC Exams

  • PrintPrint
  • Share ThisShare This
  • DiscussDiscuss

Advanced vi

Several tasks are part of vi that don't fit in any other section. Most of these are quite advanced, such as running external commands, joining lines, and splitting windows. This section covers these in detail.

Running External Commands in vi

A frequent question on the exams is how to run an external command inside vi, such as seeing an ls –l listing of the current directory so you can remember a filename:

:! ls –l

In this, the command ls –l executes, with the command's output displaying onscreen, and you need only press Enter or enter a command to return to the vi session. If the output scrolls more than one screen, it's piped to the more command and all the normal movement keystrokes will apply.

Joining Lines

It's quite irritating in vi to be at the front of a line and want to use the Backspace key to move that line to the end of the previous line. The Backspace key works only on the current line. If you need a line to be joined to the previous line, you can position the cursor in either line and press Shift+J to cause the second to be appended to the end of the first line.

Say you have a file named file1 that contains the following text:

This is line 1
This is a longer line 2
This is an even longer line 3

You want to join line 1 and line 2, so you position your cursor somewhere on line 1 and press the J key. The file then looks like the following:

This is line 1 This is a longer line 2
This is an even longer line 3

Putting the cursor on line 2 and pressing J joins line 2 and line 3 in a similar fashion.

Split Windows

Last, but not least, is splitting windows in vi, specifically the vim version of vi. When you're editing a particular file and want to see either another section of that same file or even another file altogether, you can use the following:

  • :split—This splits the window horizontally, with the same file and lines shown in both windows.

  • :vsplit—This splits the window on the same file vertically, with the same document shown in both the left and right panes.

Moving between the panes is somewhat counterintuitive because you must press Ctrl+W twice to move between the windows.

To edit a completely different file, you should edit the first one in vi; then to split the screen horizontally with the other file loaded in the second pane, you can enter

:split file2

To set the height of the newly split window from the split, you could enter the following:

:10split /etc/fstab

This command splits the top 10 lines of the screen and displays the contents of the /etc/fstab file therein.

If you wanted to close the split window, you would focus on it by pressing Ctrl+W and then entering

:close

Better yet, after comparing something or getting a filename straight, you can close all the other split windows and just have the current window open by entering the following:

:only

NOTE

Many times I've opened a couple of split windows to see the difference between two files or used the diff command. The easiest way to compare two files and make edits to them is to use the vimdiff command, such as

:vimdiff file1 file2

This loads the two files into a vertically split vim session and uses color and other symbols to show you what is similar or different between the two files. This is useful for comparing a log file before and after a problem or two configuration files to see why one doesn't work.

  • Share ThisShare This
  • Your Account

Discussions

Make a New Comment

You must log in in order to post a comment.

Related Resources

Jennifer  BortelWin FREE iPhone Developer Books and Videos- Introducing @InformIT Giveaways
By Jennifer Bortel on February 5, 2010 No Comments

Apples’s recent iPad announcement made our hearts flutter so we couldn’t resist making an announcement of our own!

Today marks the first ever @InformIT Giveaway!

We’ll regularly post a video like this one profiling spectacular prizes we’re giving away—from books and videos to T-shirts and other exciting stuff. Check out the video below to see the giveaways for today, and then scroll down for more prize details and instructions on how to win them!

So Far So Good
By John Traenkenschuh on February 2, 2010 No Comments

So far, Win 7 is making a thoroughbred of what has been a plough mule laptop

Dustin Sullivan"Every OSX developer should have this book on their desk."
By Dustin Sullivan on February 1, 2010 No Comments

That was the sentence Mike Riley ended his recent Dr Dobb's CodeTalk review of Cocoa Programming Developer's Handbook with.

See All Related Blogs

Informit Network