명령이 재할당됨

명령이 재할당됨

이전에 stderr에 문자열을 에코하는 Bash 스크립트 조각을 찾았습니다.

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

이 내용은 내 클립보드에 남아 있었는데, VIM을 사용하여 파일을 편집하려고 할 때 실수로 파일 이름 대신 이 조각을 다시 붙여넣었습니다.

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

echoerr다음에 다시 할당된 것 같습니다 vim.

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

또한 이제 VIM을 사용하여 파일을 열려고 하면 파일 이름이 다시 표시됩니다.

vim path/to/some-file

인쇄물:

path/to/some-file

무슨 일이에요?(tmux 내에서 zsh를 실행 중입니다)

답변1

왜냐하면 zsh여러 이름으로 함수를 정의할 수 있기 때문입니다. 에서 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'.

답변2

라는 함수를 만들었습니다 vim(). 이는 zsh를 사용하면 동시에 둘 이상의 이름을 가진 단일 함수를 생성할 수 있기 때문에 가능합니다.

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

어떻게 vim()그리고 dud()둘 다 함수로 설정되었는지 확인하세요.

다음과 같이 def 함수의 설정을 해제하여 잘못된 것을 없앨 수 있습니다.

% unset -f vim

이제 vim path/to/some-file편집기를 열어야 합니다.

관련 정보