samedi 30 août 2014

Beware of command substitution in bash

It's dangerous to use a loop with ls results in your bash scripts:

for i in $(ls)

Unix/Linux allows for almost any character in file names, including new lines and spaces. Then, what happens when there's a file with a space in its name:

╭─mansuro@localhost  ~/workspace/new_dir 
╰─$ touch some\ file
╭─mansuro@localhost  ~/workspace/new_dir  
╰─$ ls
some file
╭─mansuro@localhost  ~/workspace/new_dir  
╰─$ (for i in $(ls); do echo $i; done)


 As you can see, the file was treated as two different arguments, and that would have undesirable effects in your bash scripts.
How can we fix this? We could change 

for i in $(ls)


for i in *

╭─mansuro@localhost  ~/workspace/learning/Terminal/new_dir  
╰─$ (for i in *; do echo $i; done) 

some file

And you're done, your bash script works the way you want it to.

vendredi 1 août 2014

Vim persistent undo

Starting from Vim 7.3, it's possible to keep your "undo history". Even after closing and reopening the file, you can still use u and ctrl+r in order to go back to changes you made before closing the file.
In order to enable this feature, all you need to do is to set the properties undofile and undodir in your .vimrc file
set undofile
set undodir=$HOME/.undodir

Don't forget to create the directory undodir, or else this won't work
You can find more information about this feature here: Vim documentation: undo