Vi Tip of the Week: Open a file in read-only mode

This is part of a series of tips on using Vi.  To see the complete series, click here.

Sometimes I want to open up a file to look at it, but not change its contents.  I could use the commands “more” or “less”, but I prefer to use vi for longer files, mostly because I’m so used to using vi.  To do so, I can use the command:

vi -R filename

where “-R” tells vi to open the file in read-only mode.  Although, I never open it with the -R option; instead I use the command:

view filename

which also opens the file in read-only mode.

Sometimes, though, I’ll be looking at the file, and realize I actually do want to make a change to it.  The hard way would be to close the file, then open it again in the regular editing mode, but instead, I can use the command mode to force a write:

:w!

If I use “ZZ” or “:w” to try to write the file, I get an error message telling me that the “readonly” option is set, but if I use the exclamation mark “:w!”, I can force vi to write the file.

Vi Tip of the Week: Go to a specific line

This is part of a series of tips on using Vi.  To see the complete series, click here.

Sometimes when I open up a file in vi, I want to go to a specific line.  Perhaps I had a compile error on a certain line, and I want to take a look at that line.  It’s nice and easy to do: when in command mode, just type the line number, followed by an uppercase G.  So, for example, if I type:

35G

I’ll be taken to line 35.

Actually, in the situation I mentioned, there’s an even better way to do it — when I open the file, I can specify what line to go to with the +linenumber option on the command line.  For example, to open a file called dummy.txt and go directly to line 35, I can type:

vi +35 dummy.txt

and the cursor will be placed on line 35.

There are a couple variants that I use frequently: if I want to go to the first line, I enter:

1G

and if I want to go to the very last line of the file, I don’t put a number first, just the uppercase G

G

takes me to the last line.

Vi Tip of the Week: Transpose characters

This is part of a series of tips on using Vi.  To see the complete series, click here.

OK, this is my favourite command in vi, not because it’s especially useful, just because I think it’s neat.  If you make a typing mistake and get two characters switched around, it’s easy to fix.  For example, if I want to type “Linux”, but I mistakenly type “Liunx”, all I have to do is place my cursor on the “u”, use the “xp” (transpose) command, and it swaps the “u” and the “n” around, making “Linux” as I originally wanted.

I sort of lied; this isn’t a single command — it’s two commands that result in transposing the characters.  The first command, x, deletes the character under the cursor and saves it in vi’s “unnamed” buffer.  Then, the second command, p, spits out what’s in that buffer, placing it after the cursor.  So, the result is that the two characters are transposed (fancy word for switched around).  What I think is cool is that these two separate commands, x and p, when combined as xp, perform an operation that is easy to remember: xp = transpose!  Cool!!!

Vi Tip of the Week: Read the manual!

When I teach a class that is using Linux, I always encourage the students to use the Vi editor, rather than something simple like gedit, just because Vi is so powerful AND you can use it on a terminal.  But, the students are often confused by Vi, and because I’ve used it for so long and it’s just second nature to me, I’m surprised at how many students don’t use the great features of Vi and just use it like gedit.  So, I thought it would be a good idea to start a series of tips on using Vi, and this is the first one.

This week’s tip: Read the manual!  There’s a great manual written way back in the either the late 70s or early 80s by Bill Joy, the creator of Vi.  It’s called An Introduction to Display Editing with Vi, and if you work through that, you’re going to know more than probably 95% of the people who use Vi.  Anyone who wants to start using the true power of Vi should read this manual.  Actually, even as someone who has used Vi since 1982, everytime I re-read the manual I always find some command that I had forgotten about.

Be warned, though, that some of the things described are more relevant to a 1970s timesharing system than today’s computers: do we really have to worry about editing on slow terminals nowadays???  But, for the most part, this is a great manual.

You should be able to do a google search for the manual, or it’s available in the FreeBSD documentation at http://docs.freebsd.org/44doc/usd/12.vi/paper.html.

This is part of a series of tips on using Vi.  To see the complete series, click here.