Friday, June 21, 2013

Bash function for finding & editing content

This is just a little bash script for those times when you know what you want to change, but aren't sure where it is in the directory.

fixwhere()
{
    files=$(grep -ril "${1}" *)
    if [ -n "$files" ]; then
        vim +/"${1}" ${files}
    else
        echo "not found: [${1}]"
    fi
}

I use this whenever I have a bunch of text files. It's especially handy when you're working with a markup language.

I specifically wrote it to use 'vim' and the +/pattern function.  I don't actually know a good way to have it load any ${EDITOR} you use and search for the pattern in that editor.

I suppose if the time comes and I need to generalize, I will write a load_search_and_edit() function. For now, it works for me and makes me happy.

Cheers.