The key for this is the :substitute
command; it is very powerful (and often used in vi / Vim).
You need to come up with a regular expression pattern that matches what you want to delete. For the last word, that's whitespace (\s
), one or more times \+
(or any number (*
), depending on how you want to treat single-word lines), followed by word characters (\w\+
), anchored to the end of the line ($
). Note that word has a special meaning in Vim; you may want to use a different atom (e.g. \S
). Voila:
:%s/\s\+\w\+$//
For the second word, you can make use of the special \zs
and \ze
atoms that assert for matches, but do not actually match: Anchored at the start (^
), match a word, then start the match for a second one:
:%s/^\w\+\s\+\zs\w\+\s\+//
Soon, you'll also want to reorder things, not just remove them. For that, you need to know capturing groups: \(...\)
. The text matched by those can then be referred to in the replacement part. For example, to swap the first and second words:
:%s/^\(\w\+\s\+\)\(\w\+\s\+\)/\2\1/
For details, have a look at the help, especially :help :substitute
and :help pattern
.