Vi by example (cheat sheet)

There're several magnificent utilities on Unix/Linux platforms and one of the best is vi, a somewhat raw, but powerful text editor that has been available now for over three decades. It is fairly standard and thus makes you feel comfortable whatever platform you're working on. It is there on all Unix/Linux setups (I mean desktop and server environments ... it's probably not part of most embedded systems Smile ) so you don't have to find the text editor ... it's already there and can do anything you need for system administration. There're lots of tutorials and cheat sheets, so my point will be to collect some examples with the most frequently used commands.

Let's start in the middle. Smile

How to copy, paste and delete a part of text.

  1. move the cursor to the starting line of the block you want to move or copy
  2. mark the start of the block:
    ms
    (note: the letter "m" is the command, the letter "s" is the marker/label that you assign to the current position)
  3. move the cursor to the last line of the block to be moved/copied
  4. mark the line:
    me
  5. copy the lines between (and including) the two markers to a buffer:
    :'s,'ey
  6. move the cursor to the line where you want to copy to
  7. insert contents of buffer into file: p or P
  8. optionally (if you want to remove the lines from their original location - thus move the selected block) delete the lines between the markers:
    :'s,'ed
To make life easier, there're direct commands for copy and move.
After you've marked the start and end of the block, you can move the cursor to the destination and:
  • copy the marked block to current line with
    :'s,'eco.
  • move the block to current position with
    :'s,'em.
The dot at the end of the command sequence made the destination of the operation the current line. The copied/moved text will be placed after the current line, so position the cursor accordingly.

How to replace a regular expression in the whole file

:%s/regexp/replacement/g

The % means that the whole file should be selected for the operation. The g at the end means that the replace should take place for every occurance within a line.

Basic vi commands

The following commands work in command mode which is the default mode of vi.
  • Move one character to left, down, up, right: h, j, k, l
  • Move one word to right, left: w, b (note: W and B do the same)
  • Move to start, end of the current line: 0, $
  • Move to start, end of file: 1G, G
  • Move to line number n: nG
  • Scroll a page down, up: Ctrl + f, Ctrl + b
  • Search forward, backward for regexp pattern: /regexp, ?regexp
  • Repeat last search in selected, opposite direction: n, N
  • Display current line number: Ctrl + g
  • Mark current line as x: mx (note: x can be any letter in the English alphabet, a-z)
  • Copy the current line to the buffer: yy
  • Copy n lines starting with current line to the buffer: nyy
  • Delete current line: dd
  • Delete n lines starting with current line: ndd
  • Undo last edit: u
  • Join lines (the current and the following): J (note: this a capital!)
  • Join n lines starting with current line: nJ
  • Enter editing mode with insert, replace: i, R

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Copy/cut/paste

It requires less keystrokes if you use the following instead of your steps 4-5.

To copy the area: y's

To cut the area: d's

This also makes step 8 obsolete. Remember, after y and d commands you can use any cursor move command.

Re: copy/cut/paste

Thanks! That's a lot more simple indeed. Smile

"Remember ..."

I've got a tiny little problem with that: I never knew it so I cannot remember it. Laughing out loud Up til recently we were using only Win* servers at the company so my "vi" knowledge was not really challanged. However with the transition to Linux on almost all of our servers (and the lot of R&D that this requires on my side) I had to take a little deeper look at vi commands. Smile This was the main reason for this page in the first place. Eg. I didn't even know (up til yesterday) how to copy a block with markers (I didn't know anything about vi's markers at all). I used to do it by moving to the start of text, counting the lines that I wanted to copy/move, the yanking the exact number of lines, moving the cursor to the destination and pasting the text from the buffer. This works just fine for 10-20 lines ... but as soon as you start using vi more regularly, you'll hit the limit very soon. Smile

“Remember…” I like

“Remember…”

I like this word. It could also mean: “From now on remember that…” Wink

Keep reaching for the limits! Smile