Delete All Characters After "." In Each Line
Solution 1
Various additional :normal
solutions:
:%norm )Dx
:%norm $T.D
:%norm f.C.
:%norm 0/\. /e<C-v><CR>D
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
Lucas
Updated on February 01, 2020Comments
-
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 almost 10 yearsTry this, not 100% sure if its correct thats why its only a comment. :g\..*$
-
Xatenev almost 10 yearsBut probably this will only find all but not delete them..
-
FDinoff almost 10 yearsThere are a couple of ways of doing this. For example
:s
,:normal
, or macros. -
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 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 almost 10 years@Patrick There are not lines without periods, but there are a few lines without periods... does that change anything?
-
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 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 almost 10 yearsThe final
g
does not look mandatory here. Also, I believe we want to keep the.
in the end of sentences. -
Qeole almost 10 yearsNote that this command will also delete all lines containing no
.
at all. -
Qeole almost 10 yearsCommand #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 “typingCtrl + v
, not writing it as is? -
Patrick Bacon almost 10 yearsYes, I agree with you. If "a large portion of each line of text" has a period, this will work as specified; otherwise not.
-
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>
meansCtrl+v
thenEnter
and should print something that looks like^M
. -
Qeole almost 10 yearsI just thought it was worth mentioning this assumption (as well as detailing
<C-v>
as you did) :) I'm upvoting. -
Lucas almost 10 yearsThank you! I ended up using the third command and it worked out great.
-
sankoz almost 10 yearsGuess the "1" is redundant. %norm f.D would work just as well.
-
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 about 7 yearsSubstitution 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 itg
- globally -
romainl about 4 years@tarsis
)
jumps to end of sentence, thenD
cuts text from cursor to EOL, thenx
cuts the trailing space. -
Patrick Bacon about 4 years@Qeole Waited 4 years to modify my answer to take into consideration your feedback.
-
Qeole about 4 yearsWow :) Thank you!
-
romainl about 4 years@KolobCanyon did you forget the colon before
%
?