Want some “almost free” books? : Humble Book Bundle: Unix presented by O’Reilly (pay what you want and help charity)

Give it a try, for $1 dollar you get 5 books, for $8 dollars you get 7 books more, and for $15 dollars you get 16 books in PDF, mobi or ebook. They can also email them to your kindle ;-). Did I mentioned they are DRM free? ?

Get a bundle of Unix ebooks and support charity!

Source: Humble Book Bundle: Unix presented by O’Reilly (pay what you want and help charity)

Search and replace in multiple lines in VIM

So.. I had this issue where I have to replace stuff in the code where the text is splitted across several lines, this is basically because the string is too long and we usually keep the 80 characters width in the code (PEP-8) but this also works in HTML where for example, comments are splitted across several lines.

Let’s say that I want to remove the call to a function that process a text, the most common function that process text is the one everyone uses for translations: _(). The easy way is just to try a simple search and replace in vim:

:%s/_(\(.\{-}\))/\1/gc

Which will replace search for something like _("Translate this") and will leave it like this: "Translate this".  the :%s  part indicates that we want to search across the whole file, / is the delimiter, there are three delimiters, the pattern, the replacement and operation flags. Since we want to match _(.*) but we want to preserve what’s inside the parenthesis we need to create a backreference and we need to surround that between \( and \). The dot . matches every character except new lines. Please note that we use \{-} instead of the most common * (asterisk) for “one or more” occurrences.  This is because \{-} will match as few as possible, while * is too greedy and will stop at the last occurrence. Then we set the replacement string, we wanted to leave the string intact so we just use the backreference: \1 is the first backreference, \2 for the second and so on. The last gc are there just to ask you for every match, but you can just press “a” to apply to everyone.

If you want to search/replace multiple lines, then you can just use the \_. pattern in vim, that pattern matches every character including new lines.

:%s/_(\(\_.\{-}\))/\1/gc

 

So, for example:

_("This is a text"
" That is splitted across"
" several lines").upper()

after :%s/_(\(\_.\{-}\))/\1/gc will be

"This is a text"
" That is splitted across"
" several lines".upper()

Vim Pro tip – Edit the current buffer in a new tab

Quite often I found in vim that I would like the current buffer in a new tab, I use a lot the vertical and horizontal splits in vim, then I reach the limit (my screen limit) for the vertical/horizontal splits, this means: if I add a new vertical split the code looks ugly even if it respects the 80 columns width, or if add a new horizontal split there would be just too few lines that is not worth to keep the buffer.

Then I used open a new tab, type

:e

then type the path to the buffer I like to edit. Until now, now I open a new tab, then type

:ls

check then number of the buffer I want in this new tab and then type

:bX

being “X” the number of the buffer, ej “:b2“. Another trick is to type

:bn

for the next buffer or

:bp

for the previous.