11

Please let me know, How I can remove the last word from each line using vim commands? Before :

abcdef 123
xyz 1256
qwert 2
asdf 159

after :

abcdef
xyz
qwert
asdf

Similarly please let me know how to remove the second word from each line using vim command?

5
  • 3
    Really? Google didn't respond with a single reply? Apr 23, 2014 at 6:32
  • 4
    It doesn't really matter if Google has it: it matters if SO has it. The goal of SO is not to be a place to find answers to things that Google can't help with: it's to be a database of problems and solutions.
    – Sarah G
    Apr 23, 2014 at 6:38
  • 1
    @SarahG Actually it does. Hence the number one reason for a downvote; research effort. Aside from that, it isn't a programming question, which also puts in the realm of "off-topic". Apr 23, 2014 at 6:49
  • 6
    @BrianRoach We'll have to agree to disagree about whether this is a "programming" question: it's a gray area. As far as your Google link goes, I don't actually see any correct answers on the first page of the results you linked. The first link proposes a solution, but it doesn't actually work. I wouldn't call this a deep question, but I don't think it warrants a smug response.
    – Sarah G
    Apr 23, 2014 at 7:09
  • Hi Brian, I have searched the same in google, but havent find proper answer, also I am a beginner in VIM. The below links I got, but not give proper answer stackoverflow.com/questions/1568115/… stackoverflow.com/questions/10219438/… superuser.com/questions/610404/… stackoverflow.com/questions/7459677/…
    – imbichie
    Apr 23, 2014 at 7:17

4 Answers 4

15

Use the following regex (in ex mode):

%s/\s*\w\+\s*$//

This tells it to find optional whitespace, followed by one or more word characters, followed by optional whitespace, followed by end of line—then replace it with nothing.

3
  • 1
    That one leaves behind trailing whitespace. It's better to match for (mandatory) whitespace before the word. Apr 23, 2014 at 7:18
  • 1
    @IngoKarkat Edited, but I don't agree that the leading whitespace should be mandatory. That will fail if there is only one word on the line, and the last of one is still the last. And you definitely want to strip trailing whitespace.
    – Sarah G
    Apr 23, 2014 at 7:23
  • Thanks, that's better. You're right that the meaning of "last" when there's only one is unspecified by the OP; his example includes only multiple words. Apr 23, 2014 at 7:27
12

The question's been answered already, but here's what I'd more likely end up doing:

Record a macro:
qq to record a macro into register "q"
$ to go to the end of the line
daw to delete a word
q to stop recording

Then select the rest of the lines:
j to go down a line
vG to select to the end of the file

And apply the macro:
:norm @q


Some similar alternatives:

:%norm $daw

qq$dawjq (note the added j) then 999@q to replay the macro many times. (Macro execution stops at the first "error" -- in this case, you'd probably hit the bottom of the file, j would not work, and the macro would stop.)

2

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.

1

To remove the second word from the start of a line, use the following:

:%s/^\(\s*\w\+\s\+\)\w\+\s*/\1/

Update

To treat special characters as part of the word, you have to use the \S (which matches all non-whitespace characters) instead of \w (which matches only word characters [0-9A-Za-z_]). Then, the command would be:

:%s/^\(\s*\S\+\s\+\)\S\+\s*/\1/
2
  • Hi Sahu, thank you for your reply, but this command is not working if the first word have special character, for example abc[0] 123, abc[1] 987, xyz[0] 12, etc...
    – imbichie
    Apr 23, 2014 at 7:22
  • @imbichie I updated my answer to address your point.
    – R Sahu
    Apr 23, 2014 at 15:11

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.