Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I found a Bash script snippet earlier with which to echo a string to stderr:

echoerr() { echo "$@" 1>&2; }
echoerr hello world

This remained in my clipboard, and when I wanted to edit a file (with VIM) I accidentally pasted this snippet again instead of the file name:

vim echoerr() { echo "$@" 1>&2; }
echoerr hello world

It seems to have re-assigned echoerr to vim:

$ where vim
vim () {
    echo "$@" 1>&2;
}
/usr/bin/vim

Also, attempting to open a file with VIM now just echos the file name back:

vim path/to/some-file

Prints:

path/to/some-file

What happened? (I'm running zsh inside tmux)

share|improve this question
add comment

2 Answers 2

up vote 5 down vote accepted

Because zsh allows you to define function with multiple names. From man zshmisc:

function word ... [ () ] [ term ] { list }
       word ... () [ term ] { list }
       word ... () [ term ] command
              where term is one or more newline or ;.  Define a function which
              is referenced by any one of word.  Normally, only  one  word  is
              provided;  multiple  words  are  usually only useful for setting
              traps.  The body of the function is the list between the  {  and
              }.  See the section `Functions'.
share|improve this answer
add comment

You have managed to create a function called vim(). This is possible because zsh allows you to create a single function with more than one name at the same time

% vim dud() { echo ran dud function }
% dud
ran dud function
% vim path/to/some-file
ran dud function

Note how vim() and dud() both got set as functions.

You can kill off the mistaken one by unsetting the function def for it like so:

% unset -f vim

Now vim path/to/some-file should open your editor.

share|improve this answer
1  
+1 for unset -f, although before knowing this I just started a new terminal session. –  opyate Jun 14 at 10:29
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.