You're viewing a comment by Michiel and its responses.

Michiel Permalink
June 01, 2012, 22:54

Hi Peter,

great reading as always. I wondered if you had a solution for a different user preference in #10: I personally prefer to have the extension intact when renaming the file to its name.

I.e. cp /path/to/file.ext /path/to/file_bk.ext

Probably not something I want to do by hand every time, but would be great as a small bash script e.g. 'bk' to first create a safe backup of a file I want to mess with and then automatically launch vim to edit the original.


Comment Responses

June 02, 2012, 17:57

What do you think about this?:

cp /long/path/to/file{,_bk}.ext

June 02, 2012, 20:32

Here is a function called bk that does it. It works with files both of form file.ext and also files of form file (without ext). It also checks if the destination file doesn't exist. If it does, it adds _bk until such file doesn't exist. After it's copied the file it opens the original in vim.

function bk {
 if [[ -z $1 ]]; then
    echo "Usage: bk <file>"

 file=$(basename $1)

 while :; do
  case $file in
    newfile=$(echo $file | sed 's/\(.*\)\.\(.*\)/\1'$bk'.\2/')
  if [[ ! -e $newfile ]]; then

 cp "$1" "$(dirname $1)/$newfile"
 if [[ $? -eq 0 ]]; then
    vim "$1"
June 03, 2012, 10:24

Helpful script :-)

Michiel Permalink
June 04, 2012, 21:32

Awesome. That is a tutorial in and by itself! The infinite loop is a construct I hadn't used before in bash.

Thank you so much.

Reply To This Comment

(why do I need your e-mail?)

(Your twitter handle, if you have one.)

Type the word "rocket_325": (just to make sure you're a human)

Please preview the comment before submitting to make sure it's OK.