Hello all,

my name is Marvin. I am working on obtaining my linux system administrator certificat. I created a file with a few sentences. The only task that I am having problems with is to delete word "and" using the vi last-line mode. I know hot do delet a word with out using the last-line mode by just moving to the word and typing 'x' until the word is gone. But this is not how I am supposed to do it. I have read the entire chapter on the vi section and I cannot find an example or the command to do it. The closes thing I found was to do: :5,$s/(and) and ()/\2 and \1/
but this last command deletes all the "and." I have tried googling for a vi command and all I find is the 'x' option. Please help!

Recommended Answers

All 3 Replies

If you want to get rid of the "and" and leave the spaces, then do this: $s/ and/ /
If you want to get rid of the "and" and the leading space then this: $s/ and//

No rocket science required! :-)

commented: definitely not rocket science! :) +9

Rubbermans suggestion is the best one for last line mode.

But in command mode there are several methods of removing text other than using x.

Assuming the cursor is placed under the first letter of the word you wish to remove; Other than using x, which removes a single character you also use the following commands:
3x = delete 3 characters, would remove 'and' for you
dw = delete word - deletes the entire word under the cursor (up to any special, non-alphanumeric character or whitespace character)
dW = delete word - deletes the entire word under the cursor (up to the next whitespace character, regardless of any special non-alphanumeric characters)

There are several other deletion commands in command mode that will allow you to delete entire lines of text (dd, 2dd etc), or from the cursor to the start/end of the line (d0, d$) and plenty more besides!

vi and vim are extremely powerful text editors. There are a lot of commands available in them. By using combinations of commands, you can perform complex editing tasks quickly and easily. You can even compile commands into macros/scripts and automate common editing tasks. It is worth taking the time to learn how to use these editors properly as they can really boost your productivity!

Try out vimtutor, the built-in interactive vi/vim tutorial. That will give you a good headstart on using vi/vim. Also read the man pages and browse vi/vim's built-in help (using the :help command) as this will open your eyes to a lot of functionality that you probably wouldn't otherwise know about!

commented: Good advice. +12

Thanks everyone! That really helped. I can see I was somewaht close. I will keep bringing questions in the near future.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.