Delete all characters after "." in each line - vim

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 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?

With substitutions:
:%s/\..*/./
With :normal command:
:%norm f.lD

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

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.

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

with the cursor at the first character of first line.
fS<Ctrl-V>G$d

Related

I need to replace the second occurrence of string1 by string2 on every line - vim

I know that there are multiple occurrences of string1 in every line of the file. I am looking for the shortest/quickest way to replace the second one by string2. Any method will do though vim is my preference.
You can find the second occurrence of "string1" using \zs.
Based on the :h \zs example, it will be
:%s/\(.\{-}\zsstring1\)\{2}/string2
It'll be more straightforward using external sed in Vim command mode
:%!sed 's/string1/string2/2'
See https://vi.stackexchange.com/questions/8621/substitute-second-occurence-on-line for more ways to accomplish it.
Here is one way:
:%normal 0/string1^Mncgnstring2<CR>
Breakdown:
:[range]normal <macro> executes normal mode <macro> on every line in [range],
% is a shorthand for range [<first line>,<last line>], which covers every line in the buffer,
0 is the first command of our macro, it places the cursor on the first column of the line, which is a good habit to have,
/string1^M moves the cursor to the first match for string1, the ^M is a literal <CR> obtained with <C-v><CR>,
n moves the cursor to to the next match,
cgnstring2 changes the current match to string2.
See :help :range, :help :normal, :help gn.
But it would have been more interesting to see what you tried and fix it, rather than provide you with a working solution.

Vim: How to delete the same block of text over the whole file

I'm reviewing some logs with Java exception spam. The spam is getting is making it hard to see the other errors.
Is is possible in vim to select a block of text, using visual mode. Delete that block every place it occurs in the file.
If vim can't do it, I know silly question, vim can do everything. What other Unix tools might do it?
Sounds like you are looking for the :global command
:g/pattern/d
The :global command takes the form :g/{pat}/{cmd}. Read it as: run command, {cmd}, on every line matching pattern, {pat}.
You can even supply a range to the :delete (:d for short) command. examples:
:,+3d
:,/end_pattern/d
Put this togehter with the :global command and you can accomplish a bunch. e.g. :g/pat/,/end_pat/d
For more help see:
:h :g
:h :d
:h :range
Vim
To delete all matching lines:
:g/regex/d
To only delete the matches themselves:
:%s/regex//g
In either case, you can copy the visual selection to the command line by yanking it and then inserting it with <C-r>". For example, if your cursor (|) is positioned as follows:
hello wo|rld
Then you can select world with viw, yank the selection with y, and then :g/<C-r>"/d.
sed
To delete all matching lines:
$ sed '/regex/d' file
To only delete the matches themselves:
$ sed 's/regex//g' file
grep
To delete all matching lines:
$ grep -v 'regex' file
grep only operates line-wise, so it's not possible to only delete matches within lines.
you can try this in vim
:g/yourText/ d
Based on our discussion in the comments, I guess a "block" means several complete lines. If the first and last lines are distinctive, then the method you gave in the comments should work. (By "distinctive" I mean that there is no danger that these lines occur anywhere else in your log file.)
For simplifications, I would use "ay$ to yank the first line into register a and "by$ to yank the last line into register b instead of using Visual mode. (I was going to suggest "ayy and "byy, but that wold capture the newlines)
To be on the safe side, I would anchor the patterns: /^{text}$/ just in case the log file contains a line like "Note that {text} marks the start of the Java exception." On the command line, I would use <C-R>a and <C-R>b to paste in the contents of the two registers, as you suggested.
:g/^<C-R>a$/,/^<C-R>b$/d
What if the yanked text includes characters with special meaning for search patterns? To be on the really safe side, I would use the \V (very non-magic) modifier and escape any slashes and backslashes:
:g/\V\^<C-R>=escape(#a, '/\')<CR>\$/,/\V\^<C-R>=escape(#b, '/\')<CR>\$/d
Note that <C-R>= puts you on a fresh command line, and you return to the main one with <CR>.
It is too bad that \V was not available when matchit was written. It has to deal with text from the buffer in a search pattern, much like this.

swapping characters in ex

I am pretty new to vim and ex and I was wondering if anyone could help me with an area I am fuzzy on. I would like to know how to swap characters on every line or occurrence of a pattern. For example How would I swap the first 2 characters of every line in a file. I know it can be done and I'm pretty sure it involves the use of parentheses to store the chars. But thats is all I know. Also, Say I wanted to replace the 2nd char on everyline with some string, how would I do that?
To replace second character in each line to r in vim: :%s/^\(.\)./\1r/:
:%s/p/r/ replace pattern p with r for all lines (because of %);
^ start line;
\( start a group;
. any character (the first in this example);
\) end the group;
. any character (the second in this example);
\1 back reference to the first group (the first character in this example);
r replacement text.
To swap two first characters: :%s/^\(.\)\(.\)/\2\1/.
Swapping the first two characters on every line:
:%s/^\(.\)\(.\)/\2\1/g
Replacing the second character on every line with "string":
:%s/^\(.\)\(.\)/\1string/g
More info on the substitute command: http://vim.wikia.com/wiki/Search_and_replace
You can do the following to swap the two first chars of every line in the buffer:
:%norm xp
or:
:%s/\v^(.)(.)/\2\1
You'll need the :global command to apply the commands above on every line matching a specific pattern:
:g/foo/norm xp
or:
:g/foo/s/\v^(.)(.)/\2\1
Reference:
:help :normal
:help :global
:help :s
:help range

Remove all arbitary spaces before a line in Vim

I'v written a plugin where it comes to parsing a XML tag. The content inside the tag is indented and when i copy the parsed string into the file it's gettting like:
Example line
This is part of the parsed line
Thats goes one
End of line
What I want is to remove all spaces in front of these lines, the final text should be
Example line
This is part of the parsed line
Thats goes one
End of line
I've tried to use = but it doesn't work the way I want. How can I do that with minimal key strokes ?
To format a line to the left I use :left. Use this format an entire file:
:%le
A simple search/replace s/^\s*// should do the trick, but it's probably not the minimal version.
Personally I would visually select the lines with V, then use 99< to push the text as far left as it could go.
Just type d followed by w followed by j at the beginning of each line.
How about this:
:%s/^ *//
Or are you looking for a vim-script solution?
To remove initial spaces and tabs at specified line numbers (E.g. from lines 5 to 10),
:5,10s/^\s*//
Yet another way to achieve this is using the the normal command :h :normal-range
:%norm d^
This goes to column 0 in each line (%) and deletes (d) to the first non-white character(^).
This is slightly more to type as the accepted answer, but allows for easy extension if you have a more complex scenario in mind, such as additional un-commenting or so:
:%norm d^I#
Resulting in:
#Example line
#This is part of the parsed line
#Thats goes one
#End of line
The search/replace suggested by Lukáš Lalinský or the %le approach in the wikia page is probably the way I'd do it, but as another alternative you could also do:
:%< 99
As a quick way to shift the whole file (%) 99 times to the left.
Remove all consecutive spaces: :%s/ */ /g
It was useful to me to go from:
$screen-xs-min: 480px;
$screen-sm-min: 768px;
$screen-md-min: 992px;
$screen-lg-min: 1200px;
To:
$screen-xs-min: 480px;
$screen-sm-min: 768px;
$screen-md-min: 992px;
$screen-lg-min: 1200px;

Vim delete blank lines

What command can I run to remove blank lines in Vim?
:g/^$/d
:g will execute a command on lines which match a regex. The regex is 'blank line' and the command is :d (delete)
Found it, it's:
g/^\s*$/d
Source: Power of g at vim wikia
Brief explanation of :g
:[range]g/pattern/cmd
This acts on the specified [range] (default whole file), by executing the Ex command cmd for each line matching pattern (an Ex command is one starting with a colon such as :d for delete). Before executing cmd, "." is set to the current line.
:v/./d
or
:g/^$/d
or
:%!cat -s
The following can be used to remove only multi blank lines (reduce them to a single blank line) and leaving single blank lines intact:
:g/^\_$\n\_^$/d
how to remove all the blanks lines
:%s,\n\n,^M,g
(do this multiple times util all the empty lines went gone)
how to remove all the blanks lines leaving SINGLE empty line
:%s,\n\n\n,^M^M,g
(do this multiple times)
how to remove all the blanks lines leaving TWO empty lines AT MAXIMUM,
:%s,\n\n\n\n,^M^M^M,g
(do this multiple times)
in order to input ^M, I have to control-Q and control-M in windows
How about:
:g/^[ \t]*$/d
This works for me
:%s/^\s*$\n//gc
work with perl in vim:
:%!perl -pi -e s/^\s*$//g
I tried a few of the answers on this page, but a lot of them didn't work for me. Maybe because I'm using Vim on Windows 7 (don't mock, just have pity on me :p)?
Here's the easiest one that I found that works on Vim in Windows 7:
:v/\S/d
Here's a longer answer on the Vim Wikia: http://vim.wikia.com/wiki/Remove_unwanted_empty_lines
Press delete key in insert mode to remove blank lines.
This function only remove two or more blank lines, put the lines below in your vimrc, then use \d to call function
fun! DelBlank()
let _s=#/
let l = line(".")
let c = col(".")
:g/^\n\{2,}/d
let #/=_s
call cursor(l, c)
endfun
map <special> <leader>d :keepjumps call DelBlank()<cr>
:g/^\s*$/d
^ begin of a line
\s* at least 0 spaces and as many as possible (greedy)
$ end of a line
paste
:command -range=% DBL :<line1>,<line2>g/^\s*$/d
in your .vimrc,then restart your vim.
if you use command :5,12DBL
it will delete all blank lines between 5th row and 12th row.
I think my answer is the best answer!
If something has double linespaced your text then this command will remove the double spacing and merge pre-existing repeating blank lines into a single blank line. It uses a temporary delimiter of ^^^ at the start of a line so if this clashes with your content choose something else. Lines containing only whitespace are treated as blank.
%s/^\s*\n\n\+/^^^\r/g | g/^\s*$/d | %s/^^^^.*
This worked for me:
:%s/^[^a-zA-Z0-9]$\n//ig
It basically deletes all the lines that don't have a number or letter. Since all the items in my list had letters, it deleted all the blank lines.

Resources