Setting up emacsformacosx as default from command line - macos

I tried to set emacsformaxosx as the emacs command called from the command line (in order to install mu4e) by putting the following inside /usr/bin/emacs:
#!/bin/bash
/Applications/Emacs.app/Contents/MacOS/Emacs "$#"
When calling . /usr/bin/emacs emacs get launched but when calling which emacs I get nothing in my prompt, neither do I with just emacs. whereis emacs seem to find well that emacs is in /usr/bin/emacs.

You need to run
chmod a+x /usr/bin/emacs
to make the /usr/bin/emacs script executable.
The reason it works with the command
. /usr/bin/emacs
is that in that case, your shell sources the contents of the /usr/bin/emacs script and directly runs each command it finds there.
Also, note that since the shell script has nothing else to do after running emacs, you could add the command exec in front of the emacs command in the script so that the emacs process replaces the shell script process:
#!/bin/bash
exec /Applications/Emacs.app/Contents/MacOS/Emacs "$#"

Related

What is the difference between bash as a default shell and running 'bash'?

I set up subl command in ~/bin
But I couldn't run the command subl unless I run bash in my terminal. I thought changing default shell from zsh to bash would fix it but it did not. I still have to run bash before subl and this is annoying.
What's the difference between default bash and command bash?
Why subl wouldn't work until I run bash and what should I do to make it work?
I've just started learning actual computer and I know these could be silly questions. Thanks a lot for your help.
TORIs-MacBook-Pro:~ taro$ echo $SHELL
/usr/local/bin/bash
TORIs-MacBook-Pro:~ taro$ subl --help
-bash: subl: command not found
TORIs-MacBook-Pro:~ taro$ bash
bash-5.0$ subl --help
Sublime Text build 3211
Execute ~/bin/subl
Your subl command located in ~/bin (very often equivalent to /home/user/bin) is probably not in zsh's PATH variable :
The command interpreter doesn't look everywhere on you computer when you execute a command, it has a few directories to search in. This list is stored in an environment variable called PATH. It contains something like this :
$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
In my case, ~/bin is not in the list, but I can add it for this session only executing PATH="$PATH:~/bin" or include it when zsh starts by adding this line to ~/.zshrc :
PATH="$PATH:~/bin"
Now my PATH is :
$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:~/bin
Change the default shell
I'm not sure how it works with macOS X, but on Linux to change the default shell you need to execute this command line :
$ chsh -s /bin/bash
NOTE : the argument following -s must be a path to an interpreter, like bash.
You can found its path with which bash for example.
Then just relaunch your terminal or log out and in.
important : see first comment below about the tilde expansion (thanks to #Charles Duffy)

Executing MSYS2 Emacs from Windows command line or shortcut

I recently installed MSYS2 with Emacs (64-bit) and am currently calling that Emacs from a Windows shortcut. It works fine, exactly like if I had downloaded the Emacs executable for Windows and unzipped it somewhere. Which is to say, it picks up all of my Windows environment variables and such.
For various reasons, I would prefer to run Emacs from an MSYS2 bash shell and use the environment variables in that shell. As it stands, I can open an MSYS2 MINGW64 shell, type emacs on the command line, and everything works the way I want it to.
Now I would like to package the whole thing up into either a one-liner I can stuff into to a Windows shortcut or a script I can call from a Windows shortcut. With the help of this post, I came up with the following:
C:\msys64\bin\mintty.exe /bin/env MSYSTEM=MINGW64 /bin/bash -l -c /mingw64/bin/emacs
This successfully opens Emacs, but fails to load the .bashrc file that I source in .bash_profile in the usual manner:
if [ -f "${HOME}/.bashrc" ] ; then
source "${HOME}/.bashrc"
fi
I define a function in .bashrc that I call in .bash_profile, so this is kind of important. It did not take much effort to realize that the problem is that HOME is not defined, so .bashrc is simply not found. However, if I define HOME like so:
C:\msys64\bin\mintty.exe /bin/env HOME=/home/alanhr MSYSTEM=MINGW64 /bin/bash -l -c /mingw64/bin/emacs
I get exactly the same result: .bashrc is not found and my function is not executed. Here's where it gets weird. If I simply leave off the call to emacs like so:
C:\msys64\bin\mintty.exe /bin/env HOME=/home/alanhr MSYSTEM=MINGW64 /bin/bash -l
I get a bash shell where .bashrc has been loaded correctly and my function is correctly executed. I can type emacs on the command line and have it function exactly as I want it to.
This feels like a classic case of missing something that is right under my nose, but I have read the bash man page to no avail. Does anyone have any idea how I can make this work?
It is the -i option to load .bashrc. The following works for me:
C:\msys64\usr\bin\mintty.exe -w hide /bin/env MSYSTEM=MINGW64 /bin/bash -l -i -c /mingw64/bin/emacs

Start Mac OS Emacs.app with command line arguments in background

This way -- creating an executable script named ~/bin/emacs with the following contents -- is recommended on the Emacs Wiki:
#!/bin/sh
/Applications/Emacs.app/Contents/MacOS/Emacs "$#"
However, when running emacs ., Emacs did not start in the current directory.
How can this be done?
This works for me:
#!/bin/sh
$(/Applications/Emacs.app/Contents/MacOS/Emacs "$#") &

How to restart vim from a bash script?

I want to restart vim from a bash script so that vim picks up out-of-band changes. I almost have it working but I am stuck trying to determine what to use to launch vim.
Here's what I have:
#!/usr/bin/env bash
local servername=$(vim --serverlist)
[ -n "$servername" ] && {
vim --servername "$servername" --remote-send '<C-\><C-N>:mks! _session.vim<CR>:wqa<CR>'
vim -S _session.vim
sleep 1
rm _session.vim
}
The problem is the vim called by the script is the very obsolete system vim at /usr/bin/vim, not "my" vim which is an alias to mvim -v (where mvim is the launch script which comes with MacVim).
This has two unfortunate consequences: (1) the system vim doesn't recognise --serverlist; (2) even if it did my script would subsequently launch the wrong vim.
What's the best way to invoke the vim on my path?
The default vim is never built with +clientserver so the portability you are afraid to loose was never there to begin with.
Aliases are not expanded in bash scripts so your script won't see mvim -v if you don't tell it explicitly to use that. Furthermore, your vim is an alias so it is not in your PATH.
You could define an environment variable somewhere near the top of your script and use it instead of vim:
#!/usr/bin/env bash
VIM='/path/to/mvim'
"$VIM" -v whatever
Or turn your alias into a proper script.
Or, maybe, place mvim earlier in your PATH and call mvim -v explicitly.

Cygwin Shell Scripts

I'm not running cygwin, but I have the cygwin ash.exe in my %PATH% as sh.exe and have cygwin1.dll in %PATH%
I am trying to invoke some shell scripts (named with no extension) using sh -c shell-script-name but I get a "permission denied" error. If I run sh and run ./script I also get this error. I have a proper #!/bin/sh shebang line and even renaming to .sh or .exe has no effect. What should I do?
One thing to try to see if Windows permissions are causing a problem is to run Process Monitor and filter it for sh.exe and shell-script-name. That will probably show you if there's particular permission you don't have (eg you might have read but not execute permission).
Try also running the shell interactively, ie:
c:\>sh
sh# . ./script or
sh# sh -c ./script
If this works then you know that the cygwin part is working correctly. Another thing to check is that the line endings for your script are unix, as that can stop scripts from executing correctly.
Everything worked for me after doing:
$ chmod +x script

Resources