Delete All Characters After "." In Each Line

vim
41,536

Solution 1

Various additional :normal solutions:

:%norm )Dx
:%norm $T.D
:%norm f.C.
:%norm 0/\. /e<C-v><CR>D

:normal

Solution 2

With substitutions:

:%s/\..*/./

With :normal command:

:%norm f.lD

Solution 3

Use the Substitution Ex Command to Trim All Lines

This is very similar to both answers, yet I think there is value in presenting it.

Like the other answers, I just used the ex substitution command:

 :%s/[^.]*$//

Explanation of substitution:

% indicates a range for all lines.

[^.] is a character class of all non-period characters

* is a quantifier indicating 0 or more matches.

$ is an anchor which communicates to VIM that we want this pattern to match at the end of the line.


Addendum

The solution assumes each line will have a period, otherwise the command will not work as expected as @Qeole has indicated. Qeole's solution addresses non-periods lines appropriately.

Solution 4

Use search and replace "vim feature" combined with regex: :%s/\..*$//g

Solution 5

with the cursor at the first character of first line.

fS<Ctrl-V>G$d
Share:
41,536
Lucas
Author by

Lucas

Updated on February 01, 2020

Comments

  • Lucas
    Lucas about 4 years

    I have a text file with about 2,000 lines of text and I want to remove a large portion of each line of text.

    The text is in this format:

    Important Text that I care about. Stuff I want to delete
    

    I am pretty new to vim and have been reading some manuals on vim commands but I am still unsure as to how to delete all of the text after the "." in each line.

    Can someone give me a quick command that would do this?

    Thanks

    • Xatenev
      Xatenev almost 10 years
      Try this, not 100% sure if its correct thats why its only a comment. :g\..*$
    • Xatenev
      Xatenev almost 10 years
      But probably this will only find all but not delete them..
    • FDinoff
      FDinoff almost 10 years
      There are a couple of ways of doing this. For example :s, :normal, or macros.
    • Lucas
      Lucas almost 10 years
      @Xatenev The command didn't execute because Vim said "\" needed to be followed or proceeded by a "/", "?", or "&". So I changed it to: :g/\..*$ and you were right, that just finds all the instances that a "." occurs.
    • Patrick Bacon
      Patrick Bacon almost 10 years
      @Lucas Can there be multiple periods in each line? If there are multiple periods, which one are you looking for? Are there lines without periods?
    • Lucas
      Lucas almost 10 years
      @Patrick There are not lines without periods, but there are a few lines without periods... does that change anything?
    • Patrick Bacon
      Patrick Bacon almost 10 years
      @Lucas If you have some lines not having a period, then my solution will not work for the lines not having a period. It sounds like you meant to say, "there are not lines with multiple periods". If there were, an approach would need to address the multiple periods.
    • Lucas
      Lucas almost 10 years
      @Patrick You're right, I didn't notice that. I meant, there are not lines with multiple periods, but there are a few lines without periods.. Thanks
  • Qeole
    Qeole almost 10 years
    The final g does not look mandatory here. Also, I believe we want to keep the . in the end of sentences.
  • Qeole
    Qeole almost 10 years
    Note that this command will also delete all lines containing no . at all.
  • Qeole
    Qeole almost 10 years
    Command #1 seems to delete all lines containing no .. Command #2 removes final .. #3 and #4 seem to work very well. I suppose that <C-v> means here “typing Ctrl + v, not writing it as is?
  • Patrick Bacon
    Patrick Bacon almost 10 years
    Yes, I agree with you. If "a large portion of each line of text" has a period, this will work as specified; otherwise not.
  • romainl
    romainl almost 10 years
    @Qeole, all commands assume the buffer only consists of lines similar to the one in the question. #1 moves to next sentence ()), deletes the rest of the line (D) and the remaining space (x). #2 moves to the end of the line ($), moves to the character just after the dot (T.) and deletes the rest of the line (D). #3 moves to the first dot (f.) and change the rest of the line with a dot (C.). Yes, <C-v><CR> means Ctrl+v then Enter and should print something that looks like ^M.
  • Qeole
    Qeole almost 10 years
    I just thought it was worth mentioning this assumption (as well as detailing <C-v> as you did) :) I'm upvoting.
  • Lucas
    Lucas almost 10 years
    Thank you! I ended up using the third command and it worked out great.
  • sankoz
    sankoz almost 10 years
    Guess the "1" is redundant. %norm f.D would work just as well.
  • Qeole
    Qeole almost 10 years
    @sankoz Actually it's not a 1 but a l (L). And I put it there to keep the dot at the end of the line.
  • Nikola Đuza
    Nikola Đuza about 7 years
    Substitution helped me a lot, I need to delete everything after "/" character so I did :%s/\/.*//g where I substitute everything after "/" character with "" empty string and I do it g - globally
  • romainl
    romainl about 4 years
    @tarsis ) jumps to end of sentence, then D cuts text from cursor to EOL, then x cuts the trailing space.
  • Patrick Bacon
    Patrick Bacon about 4 years
    @Qeole Waited 4 years to modify my answer to take into consideration your feedback.
  • Qeole
    Qeole about 4 years
    Wow :) Thank you!
  • romainl
    romainl about 4 years
    @KolobCanyon did you forget the colon before %?