VIM script to remove Ghost newlines - linux

Is there any way using either Vim scripting or search/replace to remove ghost newlines? I have come across times when I have to edit files that other developers using crappy software have left double newlines in like the attached picture.
I would love to find a way in Vim to remove all those stupid double spacing junk so it actually looks like it should.

Running the search-and-replace
%s/\n\n/\r/g
will replace every pair of two newlines with a single one. This will inevitably screw up files that aren't already double-spaced, though, so don't use it blindly.

Shorter solution:
:v:.:d
It those lines are really empty. If there might be white spaces:
:v:^\S*$:d

Related

Turn off remove trailing whitespace for Vim

I like to use vim's auto formatting command but it is causing a problem with me and my boss. At some point someone's editor added blank lines with spaces. When i run gg=G it removes that whitespace and is creating a bunch of noise in my git commits. I still want to be able to format the text but I don't want whitespace removed.
This seems like I need to change a setting somewhere. Any ideas?
There's no built-in option that you can simply set. The closest is autoformatting; with a and w in 'formatoptions', Vim will format as you type, and keep a single trailing space at the end of each line belonging to a paragraph. But that doesn't preserve existing trailing whitespace.
If you need to reindent and reformat with gg=G, that implies that the existing layout is pretty broken. Most people will argue that you shouldn't care about preserving trailing whitespace (itself a bad practice, and usually flagged by Git) in that case.
If you can, limit the scope of the = command, e.g. by applying it selectively only on few (bad) lines, or the visual selection. That will reduce the noise in commits.
If you really need to reformat while preserving trailing whitespace, I would open a diff inside Vim (using Git, the Fugitive plugin makes this very easy), and then manually edit the trailing spaces back in. The effort required in this should further convince you (and your team) that a bit more care about proper editing hygiene benefits everybody :-)

Auto-closing for single quote in vim

I've setup auto-completions for parentheses and double quotes following the directions on this page.
The auto closing for single quotes is a bit more complicated, however. When dealing with single quotes, I have to check if it is used as an apostrophe or as a quote. The criteria for an apostrophe would perhaps be when the single quote is immediately preceded by an alphabet or number.
How do I implement this setting in my vimrc?
Well, the details of that algorithm depend on the type of text (programming languages vs. prose) and individual tradeoffs. I'd recommend to study how the 6+ plugins listed on the mentioned page handle that; the different approaches are probably quite instructive.
Maybe you'll also find information how some popular IDEs implement this (at least for the open Eclipse, this information should be available).
Using this will help
ino ' ''<left>
If you dont't want completion Ctrl-v need to be typed prior to '.
Not exactly what you are looking but a way to work without complex logic at the cost of extra key stroke

Vim replacing linefeeds

I resisted Vim, but have now given in. It works large files like a hot knife through butter.
Situation: I have a large text file, I want to put a pipe character at the beginning and ending of each line.
Problem: These Vims and other variations didn't work:
:%s/$/|\$|
:%s/\r/|\r|
:%s/$/|\r|
I suspect it's something simple to fix this, but searching Google and Stack didn't help.
You nearly had it:
:%s/^\|$/|/g
^\|$ means beginning or end of line. In a Vim regex, the | "or" pipe gets escaped. That is followed by /|/g -- replace with | globally.
Personally, I'd prefer the expressiveness of 'surround each line with pipe chars':
:%s/.*/|&|
This, in my brain, is more intuitive than 'replace a non-existing, imaginary character at the start or end of a line'.
It also depends on fewer magic chars (^,$, quoted \| which are all clumsy to type and error prone. (I mean, do you remember to quote the regex |? Do you know the difference between $ and \_$? Did you know some, if not all, of these depend on the 'magic configuration' of vim?)).
The above suffers from none of those.

Remove Various Whitespaces While Editing in Vim

So oftentimes, while editing with Vim, I'll get into a variety of situations where whitespace gives me hassle. For example, say I have a comment like this:
#This program was featured on the Today show, it is an algorithm for promoting world peace in third-world countries
#given the name of that country and the name of a celebrity to endorse its cause
If I want to, for example, trim the lines so they go to X characters, I end up putting a newline somewhere in the middle of the top line to get this (after hitting the newline and auto-indenting):
#This program was featured on the Today show, it is an algorithm for promoting
world peace in third-world countries
#given the name of that country and the name of a celebrity to endorse its cause
I then add a # to the beginning of the line, and that's all well and good, but then I want that line to line up, too. To do so, I have to delete the newline, all the whitespace for the indent on the next line, and then the commenting # mark. It doesn't take an awfully long amount of time to do that, but this and similar situations all add up over a day's worth of coding.
Now the example above is pretty specific, but my question isn't. What's a good way in Vim to delete all whitespace INCLUDING NEWLINES up until the next non-whitespace character? If Vim already has movements that do that, that would be awesome, but if not, does anyone have a favorite Vim function they use to do the above that could be mapped to a key? At the very least, am I missing some Vim usage idiom that prevents me from even having to worry about this case?
EDIT: Formatting to width, while useful and applicable to the case above, isn't the focus of this question. I'm concerned more with whitespace removal that doesn't stop at the end of a line, but instead carries on to the first non-whitespace character of the next line.
You really just want to reformat that comment to fit the current 'textwidth'. If the comment is a paragraph (i.e., separated by a line of whitespace above and below), then you can just use gqip (gq is the reformat command, ip is the "inner-paragraph" text object) to reformat it. If it's not a standalone paragraph, you can visually select those lines and then use gq.
This likely also relies on having 'formatoptions' set correctly to make sure the comment characters are handled properly, but in many cases the ftplugin has already done that.
This is a while later, but I found that there is a command that does what I need to in 90% of circumstances:
J -- join line below to the current one
This command seems to work:
:.s/\W*$\n\W*//g
it uses a replace to remove whitespace up to end of line and the new line at the end.
In this example:
testting aad $
asdjkasdjsdaksddjk$
(to see meta characters in vim use the command :set list)
if you place the cursor on the first line and use the first command it will delete everything from aad to $ (not including aad but including $ and a newline.)
Also, note for what you are doing it is far more efficient to use an external program to format comments for you. In particular, par is a great small C program that edits text and wraps it to desired lengths.
If you have par in your path, to do what you are trying to do is as easy as selecting the block of comment with Shift+v and running the command
:!par 40pgr
where 40 is the desired width in columns.
If you are feeling hackish, write your own program in C/perl/C++/python that edits comments however you like, then put it in path and use the external filter command :! to process blocks of text through it.

^M in PHP Files

^M is the dos carriage return that's left after each line when you move a file from a Windows box to a *NIX box. I know how to remove it. I am curious to know is there any other reason besides aesthetics that it should be removed from a PHP script.
The PHP script runs fine with it in. Normally, I would remove it without hesitation, but don't want to have my name next to each line in an svn blame command. (besides the point).
Question: Is there a reason in regards to functionality of why it should be remove other than aesthetics? It doesn't seem to break anything to keep it in. (Give me a good reason plz)
All in all, it should be fine. Other languages are picky about their line endings; I've seen it cause issues in Perl scripts, for example. But for PHP, i've never seen it matter much.
One occasion where it could conceivably matter is in multi-line strings, where the extra chars would make it through to the output. This might matter if your output is not HTML or XML. But JS shouldn't be particular about extraneous CRs, and HTML and XML will generally treat any whitespace the same as a single space (or in many cases, disregard whitespace altogether). Textareas and <pre> elements and such might end up with extra whitespace in them. That's about the only issue i can think of.

Resources