program story

OSX의 명령 줄에서 GUI Emacs를 시작하는 방법은 무엇입니까?

inputbox 2020. 9. 18. 08:07
반응형

OSX의 명령 줄에서 GUI Emacs를 시작하는 방법은 무엇입니까?


OSX의 명령 줄에서 GUI Emacs를 어떻게 시작합니까?

http://emacsformacosx.com/ 에서 Emacs를 다운로드하여 설치했습니다 .

다음 기준을 모두 충족하는 답변을 받겠습니다.

  1. emacs 창이 터미널 창 앞에 열립니다 .
  2. "emacs"를 입력하면 GUI Emacs 창이 열립니다. 해당 창에서 파일을 찾는 것은 기본적으로 내가 Emacs를 시작한 디렉토리를 찾는 것입니다.
  3. foo.txt가있을 때 "emacs foo.txt"를 입력하면 foo.txt가로드 된 GUI Emacs 창이 시작됩니다.
  4. foo.txt가 없을 때 "emacs foo.txt"를 입력하면 "foo.txt"라는 빈 텍스트 버퍼가있는 GUI Emacs 창이 시작됩니다. 해당 버퍼에서 ^ X ^ S를 수행하면 Emacs를 시작한 디렉토리에 foo.txt가 저장됩니다.

다음 스크립트 "emacs"를 호출하여 PATH어딘가에 넣으십시오 .

#!/bin/sh
/Applications/Emacs.app/Contents/MacOS/Emacs "$@"

그것은 # 2, # 3, # 4를 포함합니다.

# 1의 경우 .emacs 파일에 다음을 넣으십시오.

(x-focus-frame nil)

emacsformacosx.com의 사이트는 지금이 방법으로 페이지 상단 조각이 어디에서 왔는지이다. emacsclientEmacs를 실행 하고 연결 하는 방법에 대한 더 많은 정보가 git mergetool있습니다.


셸에서 'emacs'명령을 별명으로 지정하여 OSX emacs 응용 프로그램을 가리 킵니다.

내 셸 (기본 bash 실행)에는 다음이 있습니다 (내 .bashrc에)

alias emacs='open -a /Applications/Emacs.app $1'

그런 다음 명령 줄에 emacs를 입력하면 emacs 응용 프로그램이 시작됩니다.

그러나 나는 당신이 emacs의 사본을 열고 그것을 계속 실행하는 것을 권장합니다. 이 경우 기존 emacs 사본에 파일을로드하려면 .bashrc에 다음을 배치하여 emacsclient를 사용할 수 있습니다.

alias ec='/Applications/Emacs.app/Contents/MacOS/bin/emacsclient'

그런 다음 .emacs 파일에 다음을 추가하여 emacs 서버 (emacsclient 호출 수신)를 시작합니다.

;;========================================
;; start the emacsserver that listens to emacsclient
(server-start)

그런 다음 입력 할 수 있습니다.

ec .bashrc

.bashrc의 사본을 기존 emacs 세션에로드하려면!


이것은 백그라운드에서 Emacs를 시작 하여 David Caldwell의 답변향상시킵니다 .

#!/bin/sh
$(/Applications/Emacs.app/Contents/MacOS/Emacs "$@") &

다른 답변에서 언급했듯이 이것은 # 2, # 3 및 # 4를 포함합니다. # 1의 경우 .emacs 파일에 (x-focus-frame nil).

참고 다음은 않습니다 하지 나를 위해 작동 -이 명령 행에 지정된 디렉토리에 이맥스 시작되지 않습니다 (예를 emacs .)

# NOT RECOMMENDED
#!/bin/sh
/Applications/Emacs.app/Contents/MacOS/Emacs "$@" &

나는 당신이 다음 중 하나를 가정합니다.

  • 로그인시 emacs 데몬 시작
  • .emacs에 (server-start)가 있습니다.
  • 실행중인 emacs의 개별 복사본이 많이 있어도 상관 없습니다

그렇다면 이것이 원래의 4 가지 기준과 1 가지 더 많은 기준을 충족한다고 생각합니다.

  1. emacs 창이 터미널 창 앞에 열립니다.

항상 전경으로 열립니다 (x-focus-frame 사용).

  1. "emacs"를 입력하면 GUI Emacs 창이 열립니다. 해당 창에서 파일을 찾는 것은 기본적으로 내가 Emacs를 시작한 디렉토리를 찾는 것입니다.

dired 모드에서 기존 emacs 창을 엽니 다.

  1. foo.txt가있을 때 "emacs foo.txt"를 입력하면 foo.txt가로드 된 GUI Emacs 창이 시작됩니다.

If emacs is already running and has a server, then it will open in the existing window and come to the foreground.

  1. Typing "emacs foo.txt" when foo.txt does not exist launches a GUI Emacs window with an empty text buffer named "foo.txt". Doing ^X^S in that buffer will save foo.txt in the directory from where I started Emacs.

Correct.

One extra:

Control returns to the terminal session immediately after typing the command.

~/bin/emacs

#!/bin/bash
EMACSPATH=/Applications/Emacs.app/Contents/MacOS

# Check if an emacs server is available 
# (by checking to see if it will evaluate a lisp statement)

if ! (${EMACSPATH}/bin/emacsclient --eval "t"  2> /dev/null > /dev/null )
then
    # There is no server available so,
    # Start Emacs.app detached from the terminal 
    # and change Emac's directory to PWD

    nohup ${EMACSPATH}/Emacs --chdir "${PWD}" "${@}" 2>&1 > /dev/null &
else
    # The emacs server is available so use emacsclient

    if [ -z "${@}" ]
    then
        # There are no arguments, so
        # tell emacs to open a new window

        ${EMACSPATH}/bin/emacsclient --eval "(list-directory \"${PWD}\")"
    else    
        # There are arguments, so
        # tell emacs to open them

        ${EMACSPATH}/bin/emacsclient --no-wait "${@}"
    fi

    # Bring emacs to the foreground

    ${EMACSPATH}/bin/emacsclient --eval "(x-focus-frame nil)"
fi

On Mountain Lion, I am using Yamamoto Mitsuharu's port https://github.com/railwaycat/emacs-mac-port with the following alias:

alias emacs=/Applications/Emacs.app/Contents/MacOS/Emacs

and it satisfies all of your criteria.


Just built emacs with homebrew package manager according to this guide: http://www.emacswiki.org/emacs/EmacsForMacOS with brew install --cocoa emacs After that one should launch the .app version to get gui, which in my case was /usr/local/Cellar/emacs/24.3/Emacs.app/Contents/MacOS/Emacs


Further improving on David James' response the following works for me:

Per instructions to open a file from a terminal found at http://www.emacswiki.org/emacs/EmacsForMacOS#toc20

open -a /Applications/Emacs.app <file-name>

combining this with David Jame's response I've created the following emax bash script and placed it in my path at ~/bin

#!/bin/bash
(open -a /Applications/Emacs.app "$@") &

Caveat: in order to get emacs to open the current directory in Dired by name mode, you need to use

emax .

Environment:

  • OS X Yosemite Version 10.10.2
  • GNU Emacs 24.4.2 (x86_64-apple-darwin14.0.0, NS apple-appkit-1343.14) of 2014-11-13

The other answers here didn't quite work for me. In particular, on my machine, the bash script

#!/bin/sh
/Applications/Emacs.app/Contents/MacOS/Emacs "$@" 

always opens emacs in the home directory. To get it to open in the current working directory, I had to do

#!/bin/sh
/Applications/Emacs.app/Contents/MacOS/Emacs "$PWD/$@"

instead.


Simple solution...

A lot of very complex solutions to this problem are posted here. That's fair because it seems non-trivial.

However, this solution works really well for me.

ec() {
  emacsclient -n $@ 2> /dev/null
  if [[ $? == 1 ]]; then
    open -a Emacs.app  -- $@
  fi
}

Usage

ec file [...]

Let's unpack what's happening:

  1. pass all the ec arguments to emacsclient and don't (-n) wait for emacs before continuing.
    1. If Emacs is already running, we're all done and you're editing.
  2. swallow up the error message posted by emacsclient when there's no emacs running. (2> /dev/null)
  3. Manually handle the exit code 1 ([[ $? == 1 ]])
    1. open Emacs.app and pass file arguments to it (paths will be correctly opened.)
    2. You're all done, and Emacs has opened your files.

Compile Emacs according to the following steps:

./configure --with-x --prefix=/usr
make
sudo make install

And your done! It may help to download and install XQuartz, but that's just my opinion.


This is my script for open emacs/emacsclient on osx.

#!/bin/bash

# Ensure (server-start) is added in your emacs init script.

EMACS=/Applications/MacPorts/Emacs.app/Contents/MacOS/Emacs
EMACSCLIENT=/Applications/Macports/Emacs.app/\
Contents/MacOS/bin/emacsclient

# test if client already exsit.
$EMACSCLIENT -e "(frames-on-display-list)" &>/dev/null

# use emacsclient to connect existing server.
if [ $? -eq 0 ]; then
    $EMACSCLIENT -n "$@"
# open emacs.app instead.
else
    `$EMACS "$@"` &
fi

참고URL : https://stackoverflow.com/questions/10171280/how-to-launch-gui-emacs-from-command-line-in-osx

반응형