Why does emacs does not find psql? - postgresql

This is somewhat weird, I compiled from source postgresql. I've had no problem using psql at all.
$ psql -d rita
psql (9.3.2)
Type "help" for help.
rita=>
`
However when I try to run M-x sql-postgres on emacs, it prompts for user, password, database, host. But when I input the data in the minibuffer just appears: Unable to locate SQL program psql. I hope anyone can tell me how can I fix this, or what is happening.

Your psql isn't on the standard path.
You are likely setting the PATH in
~/.bashrc, and Emacs will not see it unless you launch Emacs from the terminal.
A solution is:
(setq sql-postgres-program "/usr/local/pgsql/bin/psql")
Or ln -s to /usr/bin if you want.

Alternatively, you can add the path to the psql binary to your emacs 'exec-path list as follows:
(add-to-list 'exec-path "/path/to/program")
(setenv PATH (mapconcat 'identity exec-path ":"))
I run Emacs on a Mac as my development environment. Launching emacs from the Dock doesn't include the path for programs installed via MacPorts. This is going to be a standard problem for anyone who has "non-standard" exec path elements and launches Emacs via their GUI. For me, OSX : MacPorts : /opt/local/... this includes psql, mysql, py-flakes and others. I could either set a variable for each add-on or modify exec-path. The setenv is needed where the elisp function defers searching the exec-path to the shell. The C-c C-v binding in python mode leverages the shell and the $PATH environment to find py-flakes.
The add-to-list is a simple solution that prepends the path to the list. One could make a case for more complex path wrangling in your .emacs file. YMMV.

Related

How to get value of user-emacs-directory from shell?

I'm writing a script to automatically install some elisp. I'd like to find the value of user-emacs-directory so that I can copy the file to the right directory. Is there any way to do this from the shell?
I was hoping it would work to run:
$ emacs --batch --eval="(print user-emacs-directory)"
Symbol's value as variable is void: user-emacs-directory
but as you can see that was not fruitful. Then I tried loading my .emacs file first:
$ emacs --batch -l ~/.emacs --eval="(print user-emacs-directory)"
Loading /Users/noah/dotfiles/emacs/init.el (source)...
Symbol's function definition is void: global-visual-line-mode
but that also chokes. Googling hasn't led me to an answer yet. Any ideas?
edit: The specific problem was that I was using an old version of Emacs (22.1.1). Running Emacs 24.4.1 works:
$ emacs -l ~/.emacs --batch --eval="(print user-emacs-directory)" 2>/dev/null
"~/dotfiles/emacs"
(which is the custom value I've set in my own ~/.emacs file)
Note that I'm redirecting stderr to /dev/null to suppress some loading messages.
Works for me on vanilla GNU Emacs 24.5.1 shipped with Ubuntu:
$ emacs -Q --batch --eval="(princ user-emacs-directory)"
~/.emacs.d/
note princ instead of print to avoid extra whitespace and quote marks.
--batch implies -q which means there is no user-emacs-directory
edit: Ugh. No, -q doesn't mean that at all. As sds points out, the proposed code should probably be working. That'll teach me to jump to conclusions. I'd delete this answer, but as the original approach evidentially doesn't work for Noah for some reason, I'll leave this here just in case it still helps...
Typically you just want $HOME/.emacs.d/
See C-hig (emacs) Find Init RET for more info.

how to use emacs tramp with ssh remote to server

I want use emacs remote server(freebsd use sftp with ssh).
I already read this and write (require 'tramp) (setq tramp-default-method "ssh") in my .emacs. I use C-c C-f RET /sftp:ganluo-home-vm-freebsd-user#192.168.1.104/.
But it does not work
Look Here:I use emacs for windows so can't direct use tramp.Have to download plink.exe(I use it maybe had other way) and do some other exp:
first download here (also you can found some windows conntion unix\linux software in this website).Find plink.exe download it (offer release and development and source code)
than you can cut plink.exe in the your emacs\bin directory.
find your Environment Variables Edit/New "PATH" Add your emacs\bin complete directory and last the path must with ';'
C:\Emacs\bin;
then sign out use cmd test plink command is useful.if doesn't restart you PC.
C-c C-f /pink:YouName#HOST[IP,HOSTName]:/
it had some problem,I will post new status right here.

Emacs: Default binary to run with M-x ansi-term

Is there a way to have Emacs to prompt by default for a binary of my choice when running M-x ansi-term. It always asks me for /bin/zsh but I have a new installation of zsh in a different path.
Alternatively, it would be great to have Emacs to just run the binary I want when I type M-x my-ansi-term
There are a number of ways to specify the default shell:
Set the variable explicit-shell-file-name. For example, in your .emacs: (setq explicit-shell-file-name "/bin/zsh")
Set the environment variable ESHELL
Set the environment variable SHELL
Alternatively, it would be great to have Emacs to just run the binary I want when I type M-x my-ansi-term
See Remote ssh connection from within Emacs.

Emacs compilation mode won't see bash alias

Emacs M-x compile does not see any aliases set in .bashrc. If I use M-x shell then type the alias, it is fine. I tried sourcing .bashrc from /etc/profile, from ~/.profile, ~/bash_env, anything I can think of to no avail.
I am on Emacs 23 and Ubuntu 11. I start emacs using /usr/bin/emacs %F, from a desktop button.
Emacs inherits its environment from the parent process. How are you invoking Emacs - from the command line, or some other way?
What happens if you:
M-x compile RET C-a C-k bash -i -c your_alias RET
Invoking bash as an interactive shell (-i option) should read your .bashrc aliases.
Edit: I think both M-x shell-command and M-x compile execute commands in an inferior shell via call-process. Try the following in your .emacs (or just evaluate):
(setq shell-file-name "bash")
(setq shell-command-switch "-ic")
I notice that after evaluation of the above, .bashrc aliases are picked up for use by both M-x shell-command and M-x compile, i.e
M-x compile RET your_alias RET
should then work.
My environment: Emacs 24.1 (pretest rc1), OSX 10.7.3
Keith Flower's answer works but can result in some slowdowns due to .bashrc being unnecessarily loaded in other places (presumably many many times, my computer is not exactly under-powered but emacs was almost unusable when trying to use autocomplete.el).
An alternative way is to locally modify shell-command-switch only for the functions where it is needed. This can be done using emacs' "advice" feature to create a wrapper around those functions. Here's an example that modifies compile:
;; Define + active modification to compile that locally sets
;; shell-command-switch to "-ic".
(defadvice compile (around use-bashrc activate)
"Load .bashrc in any calls to bash (e.g. so we can use aliases)"
(let ((shell-command-switch "-ic"))
ad-do-it))
You need to write similar "advice" for each function that you want to use .bashrc (e.g. I also needed to define the same advice for recompile), just copy the above and replace compile in the above with another function name.
You may like emac's bash-completion :
https://github.com/szermatt/emacs-bash-completion
You'll be able to use tab completion of your aliases in the compilation minibuffer and in shell-mode.
Enjoy !
(they speak about it here Bash autocompletion in Emacs shell-mode )
I think compilation commands are not interpreted through a shell: they are juste exec'ed by emacs (which means aliases, shell functions and other shell-specific things are not taken into account).
Try to wrap you compilation command into a shell-script which would source the correct environment.
You can do this either with a full-fledged shell-script in the form
#!/bin/bash
source "~/.bashrc"
my_command
or directly in emacs with a compilation command of the form
bash -c "source ~/.bashrc; my_command"
See Is there a way to get my emacs to recognize my bash aliases and custom functions when I run a shell command? for a fix which doesn't run all your .bashrc and doesn't create these error messages:
bash: cannot set terminal process group (-1): Inappropriate ioctl for device
bash: no job control in this shell

I want to run the cygwin bash shell from native windows emacs app

I have followed instructions from How can I run Cygwin Bash Shell from within Emacs? this question and I have gone further and added the (setq explicit-bash-args '("--login" "-i")) command, however emacs continues to only display the dos prompt when I type M-x shell. In summery my .emacs file looks like this:
(defun cygwin-shell ()
"Run cygwin bash in shell mode."
(interactive)
(let ((explicit-shell-file-name "C:/cygwin/bin/bash"))
(call-interactively 'shell)))
(setq explicit-bash-args '("--login" "-i"))`
Please be gentle with the answers as I am right at the bottom of the famous vertical emacs learning curve!
If you implemented the answer from that question, note that you have to do M-x cygwin-shell to start bash. If you want to use it for every M-x shell you need to call
(setq explicit-shell-file-name "C:/cygwin/bin/bash")
Since you stated that you are learning, here's a few tips when trying this out.
type C-x C-f ~/.emacs to open your .emacs file in your user path.
Enter your function above at the end
M-x load-file [RET] .emacs: loads the buffer (no need to restart emacs)
C-h a: If you are interested in some specific action, you can look it up
C-h v [RET] variable: can inspect the variable, check the value of explicit-bash-args for instance
And, btw, I'm not sure what the "--login -i" does, but someone stated in a comment that you should have that so "ls" would work. If you have your cygwin bin path in your PATH environment variable, bash will find ls anyway. No need to escape the path variable either, this is handled by bash (do an echo $PATH in bash when you get it working and you'll see).