Vim. How to replace a word (string) in several files.

Posted in apps, howto, vim by pavlo on 15th 2008f May, 2008

I Write this post, because I sometimes search for this functionality by myself. Today happened such a case and after I have read again a little bit of online manuals, I decided to post a snippet.

This is taken from the VIM online help (http://vimdoc.sourceforge.net/htmldoc/usr_12.html)

*12.1*	Replace a word

The substitute command can be used to replace all occurrences of a word with
another word:

	:%s/four/4/g

The "%" range means to replace in all lines.  The "g" flag at the end causes
all words in a line to be replaced.
   This will not do the right thing if your file also contains "thirtyfour".
It would be replaced with "thirty4".  To avoid this, use the "\<" item to
match the start of a word:

	:%s/\<four/4/g

Obviously, this still goes wrong on "fourty".  Use "\>" to match the end of a
word:

	:%s/\<four\>/4/g

If you are programming, you might want to replace "four" in comments, but not
in the code.  Since this is difficult to specify, add the "c" flag to have the
substitute command prompt you for each replacement:

	:%s/\<four\>/4/gc

REPLACING IN SEVERAL FILES

Suppose you want to replace a word in more than one file.  You could edit each
file and type the command manually.  It's a lot faster to use record and
playback.
   Let's assume you have a directory with C++ files, all ending in ".cpp".
There is a function called "GetResp" that you want to rename to "GetAnswer".

	vim *.cpp		Start Vim, defining the argument list to
				contain all the C++ files.  You are now in the
				first file.
	qq			Start recording into the q register
	:%s/\<GetResp\>/GetAnswer/g
				Do the replacements in the first file.
	:wnext			Write this file and move to the next one.
	q			Stop recording.
	@q			Execute the q register.  This will replay the
				substitution and ":wnext".  You can verify
				that this doesn't produce an error message.
	999@q 			Execute the q register on the remaining files. 

At the last file you will get an error message, because ":wnext" cannot move
to the next file.  This stops the execution, and everything is done.

	Note:
	When playing back a recorded sequence, an error stops the execution.
	Therefore, make sure you don't get an error message when recording.

There is one catch: If one of the .cpp files does not contain the word
"GetResp", you will get an error and replacing will stop.  To avoid this, add
the "e" flag to the substitute command:

	:%s/\<GetResp\>/GetAnswer/ge

The "e" flag tells ":substitute" that not finding a match is not an error.
Advertisements

qubime – windows executable

Posted in apps, qubime by pavlo on 6th 2008f May, 2008

I have built a windows executable for the previously described program – Qubime using simple tool py2exe.

You can download it here [1,8MB].
http://www.box.net/shared/9hf7s8i4oc

The executable itself is 23KB, but one needs the libraries for running it at as a standalone application. These are (windows) hidden files and are included whithin the package, so please do not move qubime executables out of the folder. Instead, please make your copies of qubime in the same directory where you have it installed, and place links to the new/old tools you’ve created.

I also included some templates, so you can use them as examples.

qubime – lightweight image editor [UPDATE]

Posted in apps, code, python, qubime by pavlo on 4th 2008f May, 2008

Qubime” – simple to customize editor for multiple images, written in python by me. The name comes from me and it is an acronym for “Quick Bulk Image Editor”.

Contents:

Short description.
This program is made for quick editing of multiple images. It can resize, rotate, flip and convert them automatically just by dropping your files on the program icon.

A little bit of history.
The Idea of creating such a program appeared, when I wanted to send some pictures attached to an Email. These pictures were too large and I needed to make them smaller, and rotate some of them – an everyday task for many of you out there. I work in Ubuntu, so I have Eye of Gnome for viewing images, F-Spot Photo Manager – for managing photos, and GIMP for editing per default. Eye of Gnome can very simple rotate an image, but, honestly, I haven’t found the function to resize it. F-Spot Photo Manager appears to use Eye of Gnome to view and edit images, so it has the functionality of the latter. The thing I was left with was GIMP. It does its job perfectly, but it’s too heavy for such tasks. Especially, when you have to edit more than one image, you will end up with editing every one separately.

Idea and short usage explanation.
I thougt, that it would be useful to have a program, which can edit (resize, rotate, flip) multiple images at once. I had an Idea of creating an application without GUI. You should just select the images you want to edit, drag them and drop them onto the program. Some time pass (for calculations), and you receive edited images. How to edit the images should depend from the name of the program. So you can create a set of editors for your needs by yourself.
For example, if you want to shrink your images, so that the longer side will be 800px, you will have to have a copy of qubime called resize.800.py. If you want to rotate the images clockwise for 90°, then you have to use a copy, which is called rotate.clockwise.90.py or in simplified variation – rotate.90.py

After all, you decide, which kind of tools you want to have. The only thing you care about is, that the tools are placed to some visible positions on your desktop (e.g. some links to them on your panel) to have them handy for quick usage.

Download.
for Windows, Linux and Mac OSX you can download the program here:
http://www.box.net/shared/zdpcyhy4gk
It is a python executable. I have written it in Ubuntu and I only tested it under Ubuntu 8.04 and 7.10. Hope that it will work for Windows and Mac also.

[UPDATE]
You can also download a standalone version of the program here (for Windows only):
https://madyogi.wordpress.com/2008/05/06/qubime-windows-executable/

Feedback and bug reporting.
Please read the README file – most of the things are explained there. Nevertheless, I will very appreciate your comments, critiques and bright ideas about how to improve it. If you still think, that you have some unresolved issues or I am not answering for a long time, feel free to write me at
qubimemadyogi.info.

Contribution.
Ah. Nearly forgot it. I do not have any Icons you could use with qubime. If somebody likes to contribute some self-made icons, you are more then welcome. Just write me an e-mail to
qubimemadyogi.info and attach your icons package. I will publish them here along with the program.
I will be very welcoming every type of contribution, you make – build standalone executables, improve code, fix bugs etc. I will be very glad to publish them along with the program.
All the contributions will be published with the credits to the author of the work.

Enjoy!