Vim & Bash ❤
Vim is a powerful text editor. I started by using emacs in university but on my first job everyone used vim and, after feeling a bit comfortable with the edit commands, i felt in love with it!
It’s like you’re talking to it while editing.
ma — mark a
y’a — yank till a
dw — delete word
I do everything on vim: take notes, write user stories, my shopping list… i’m so used to it that sometimes i get myself pressing “esc” on other editors when i stop writing :D
And then bash…
Someone once told me “If you did it (or plan to do it) more than 5 times, automate it!”.
I took it very seriously and the more comfortable i felt using vim and bash scripts, the more things i was automating.
Real use case example: when i started programming for iOS, at the beginning we didn’t have a translations management tool. We had an excel with all the keys we needed on first column and then its correspondent translation on other columns, one per language. I realized that all developers were actually copy pasting the values for the strings files… every time a translation changed.
More than 5 times.. automate it guys!
What we needed:
- get the excel into a CVS;
- get the key (column one) and translation for specific language (column N);
- put it in the correct format to work on the project — Android and iOS.
Let’s imagine a simple CSV
id,EN,PT,IT
string_hello,hello,ola,ciao
string_good_morning,good morning,bom dia,buongiorno
Open the file and get rid of the first line
vim file.csv
dd
:wq
Command to get iOS translations for Portuguese
$ cat file.csv|cut -d, -f1,3|sed 's/^/"/g'|sed 's/,/"="/g'|sed 's/$/";/g'"id"="PT";
"string_hello"="ola";
Command to get Android translations for Italian
$ cat file.csv|cut -d, -f1,4|sed 's/^/<string name="/g'|sed 's/,/">/g'|sed 's/$/<\/string>/g'<string name="string_hello">ciao</string>
<string name="string_good_morning">buongiorno</string>
But i don’t want to do this every time… i want to run a command and have all the files for both.
$ vim genTranslationFiles#!/bin/bashrm -rf it.lproj en.lproj pt.lproj values-en values-it values-pt
mkdir it.lproj en.lproj pt.lproj values-en values-it values-pt# Gen Android files
# English
cat file.csv|cut -d, -f1,2|sed 's/^/<string name="/g'|sed 's/,/">/g'|sed 's/$/<\/string>/g' > values-en/strings.xml
# Portuguese
cat file.csv|cut -d, -f1,3|sed 's/^/<string name="/g'|sed 's/,/">/g'|sed 's/$/<\/string>/g' > values-pt/strings.xml
# Italian
cat file.csv|cut -d, -f1,4|sed 's/^/<string name="/g'|sed 's/,/">/g'|sed 's/$/<\/string>/g' > values-it/strings.xml# Gen iOS files
# English
cat file.csv|cut -d, -f1,2|sed 's/^/"/g'|sed 's/,/"="/g'|sed 's/$/";/g' > en.lproj/Localizable.strings
# Portuguese
cat file.csv|cut -d, -f1,3|sed 's/^/"/g'|sed 's/,/"="/g'|sed 's/$/";/g' > pt.lproj/Localizable.strings
# Italian
cat file.csv|cut -d, -f1,4|sed 's/^/"/g'|sed 's/,/"="/g'|sed 's/$/";/g' > it.lproj/Localizable.strings
This is shitty in terms of performance… yes! Does it matter for the case? Not really, don’t overcomplicate it :)
Check my project for this on github: gen_strings_files
Some useful vim edit commands
# quit :P (without saving)
:q!
# substitute a by b, globally
:%s/a/b/g
# add a ; at the end of every line
:%s/$/;/g
# execute pwd on terminal without exiting vim
:!pwd
# duplicate the lines of my file
yGP
# delete all lines from beginning of file till line that starts by "1234" (inclusive)
/^1234
ma
:1
d'a
# delete all lines from end of file till line that starts by "1234" (inclusive)
/^1234
ma
G
d'a
# substitute two next words by "aaa"
2cw
aaa