How to count search results in Vim?

You can count the number of matches using the n flag in the substitute command. Use the following to show number of times that some-word matches text in current buffer: :%s/some-word//gn You can read all the details on the vim tips wiki

Case-insensitive PowerShell replacement

Call me pedantic but while nobody here was outright wrong, nobody provided the correct code for the final solution either. You need to change this line: $NullSessionPipes = $NullSessionPipes.replace(“browser”, “”) to this: $NullSessionPipes = $NullSessionPipes -ireplace [regex]::Escape(“browser”), “” The strange [regex] text isn’t strictly necessary as long as there are no regular expression characters (ex. … Read more

Is str.replace(..).replace(..) ad nauseam a standard idiom in Python?

Do you have an application that is running too slow and you profiled it to find that a line like this snippet is causing it to be slow? Bottlenecks occur at unexpected places. The current snippet traverses the string 5 times, doing one thing each time. You are suggesting traversing it once, probably doing doing … Read more

Replace multiple spaces with one space in a string

bool BothAreSpaces(char lhs, char rhs) { return (lhs == rhs) && (lhs == ‘ ‘); } std::string::iterator new_end = std::unique(str.begin(), str.end(), BothAreSpaces); str.erase(new_end, str.end()); How this works. The std::unique has two forms. The first form goes through a range and removes adjacent duplicates. So the string “abbaaabbbb” becomes “abab”. The second form, which I used, … Read more

replacing text in a file with Python

This should do it replacements = {‘zero’:’0′, ‘temp’:’bob’, ‘garbage’:’nothing’} with open(‘path/to/input/file’) as infile, open(‘path/to/output/file’, ‘w’) as outfile: for line in infile: for src, target in replacements.items(): line = line.replace(src, target) outfile.write(line) EDIT: To address Eildosa’s comment, if you wanted to do this without writing to another file, then you’ll end up having to read your … Read more