how to get grep working in Emacs on Windows (EmacsW32) - windows

M-x grep, M-x lgrep, M-x rgrep don't work in EmacsW32 for me.
I do M-x lgrep and it says grep is not a command:
grep -i -n "hello" * NUL
'grep' is not recognized as an internal or external command,
operable program or batch file.
Grep finished with no matches found at Sun Jan 31 05:59:06
Also what is that NUL thing? EmacsW32 homepage says it ships with Gnuwin32 utilities but it seems the work to configuring to actually use the Gnuwin32 grep is left to users.
How can I configure it to use either the shipped Gnuwin32 grep or the cygwin grep? Are both fine?

This article has some tips on how to get this working.
I got this working the other day, you can set the PATH environment variable inside emacs, and if you have cygwin and/or gnuw32 installed just set the path to those. This is a snippet from my .emacs that's applicable on windows only. I set to variables (cygwin-bin, gnu-bin) to the path where the programs are installed. Then build the path to those. One drawback is it blows away the rest of my path. Which hasn't been an issue so far, but If I was smarter with LISP I could probably figure something out. Anyway, hope this helps.
;;windows only stuff
(when (string-equal system-type "windows-nt")
(progn
(setq cygwin-bin "c:\\apps\\cygwin\\bin")
(setq gnu-bin "C:\\apps\\GnuWin32\\gnuwin32\\bin")
(setenv "PATH"
(concat cygwin-bin ";" gnu-bin ";"))
(setq exec-path
'(cygwin-bin gnu-bin))))
I should add, I arrived at this solution because putting qnuwin32 in front of the path in windows seems to be a risky proposition, you run the risk of messing up other programs on your machine. So this seemed to be a good compromise.

Windows doesn't come with a grep utility (in fact it does have findstr, but it's not exactly the same), so you need to install something like cygwin or unix utils for windows. After installing those, you need to add them to %PATH%, so that windows would know where to look for them. You can do that either in command line by running cmd.exe and issuing a command set PATH=<your_path>;%PATH% or in some settings widow you get by right clicking a My Computer icon (called something like environment variables here are some pictures of it).
Note, windows has a utility called find and it is also in system %PATH%, that can make certain commands (like M-x grep-find) not work correctly, so it is important you put unix-find first in your %PATH%.

I think the EmacsW32 user first need to set the user option w32shell-shell via the menu Options > Customize EmacsW32, which is set to none originally, and the user should set it to either cmd or cygwin. Setting it to cmd means M-x grep will use the built-in Gnuwin32 grep (and cmd.exe as a shell) and if the setting is saved correctly, the dotemacs will contain lines like the following:
(custom-set-variables
...
'(w32shell-shell (quote cmd))
...)
The custom-set-variable form should be placed as close to the beginning of the dotemacs as possible.
For some reason in my system, this is not enough, M-x grep will insist to use "grep -n" as its template rather than "grep -nH -e" and it always appends "NUL". In that case, add the following line in the dotemacs file.
(grep-compute-defaults)

Here's a no-nonsense answer that works:
http://groups.google.com/group/gnu.emacs.help/browse_thread/thread/39cd9d63b07a9988#
Enjoy!

I use Cygwin (an old version). For Emacs I use libraries cygwin-mount.el and setup-cygwin.el, loaded in that order. Works just fine.

Related

Is it not possible to use Windows Bash as shell in Emacs for ssh etc?

I tried but get error: Process shell exited abnormally with code 255.
Mainly want this for SSH, and avoid Cygwin or plink/Putty.
I have this in config:
(setq explicit-shell-file-name "C:\\Windows\\System32\\bash.exe")
(setq explicit-bash.exe-args '("--noediting" "--login" "-i"))
(setenv "SHELL" shell-file-name)
(add-hook 'comint-output-filter-functions 'comint-strip-ctrl-m)
Thank you
I use Bash for Windows with the latest Windows Creator's Update and Ubuntu 16.04 they have included. It runs quite well on Visual Studio Code and Cmder (a shell app like MobaXTerm etc). I also use ZSH instead of Bash (with oh-my-zsh and powerline9k) but i had some adjustments to do (and, it takes time to load, but i've read somewhere that Microsoft is working to fix this slow issue).
To be honest, it's a good way to replace Putty, but it has to grow a little. There's a lot of network tools that can't work on WSL for example.
I know the pain to prepare a Cygwin or use Putty, but you can take a look on MobaXterm, a really good ssh client that includes a package manager to allow you to do a lot of things from your Windows. I don't know if they use Cygwin like in the past ... But it's a ready-to-use solution with local bash shell.
To finish this and maybe help you, here is my startup line to run WSL / Bash for Windows in Cmder: bash -l -i -cur_console:p -c zsh. If you need any more information just ask :).

How To Run Scheme in Mac Emacs? - "process scheme exited abnormally with code 1 / 126"

Questions with similar titles (linked) do not cover the specific errors I am encountering, hence the new ask.
Context:
I am trying to set up an environment for working with scheme in emacs so I can work through the SICP book. I understand it is possible to integrate the two nicely so they work well together. I understand there are many ways of doing this even with extra features (I've seen the names quack/racket ...) but those seem like the more advanced options. Since I am having trouble setting up this bare basics 'run-scheme' in emacs I have not looked into these options extensively.
Where I'm at / What I've tried:
I downloaded MIT:GNU scheme 9.2 for OS X (x86-64). This works as expected and I can run the interpreter either from the terminal with:
$ scheme
or by executing the file "/Applications/MIT:GNU Scheme.app/Contents/Resources/mit-scheme" (which opens the interpreter in a new terminal window).
At this point M-x run-scheme returns a message saying that no such file or directory exists.
I read these two questions for reference and then:
I created a symbolic link to the binary/exec file "/Applications/MIT:GNU Scheme.app/Contents/Resources/mit-scheme" (which works perfectly when executed from finder/the terminal), then placed this file in "/usr/bin" with name "scheme".
At this point M-x run-scheme returns:
/usr/local/Cellar/emacs/24.5/Emacs.app/Contents/MacOS/Emacs:/usr/bin/scheme: Exec format error
Process scheme exited abnormally with code 126
The symlink file looked very out of place in the bin folder so not knowing what else to try I removed the symlink and instead created an actual copy of the mit-scheme exec file and placed that in "/usr/bin".
M-x run-scheme now returns this:
/usr/bin/scheme: can't find a readable default for option --band.
searched for file all.com in these directories:
/Applications/mit-scheme.app/Contents/Resources
Inconsistency detected.
Process scheme exited abnormally with code 1
Searching for help with understanding these error messages lead me to threads about issues with setting up scheme on windows OS, which is confusing, and I feel I am now out of my depth -
hoping someone knows where I'm going wrong.
End
It seems strange because I know that the 'M-x run-scheme' command is trying to access the location "/usr/bin/scheme" and I have made sure that such a location exists, and that the scheme exec file at that location works properly - I don't understand what other requirements emacs has for this to work.
What is the run-scheme command actually doing? What other requirements does it have to function as expected?
MIT Scheme looks for its libraries (including its 'bands') in a location derived from the executable's path. If the libraries are in a non-standard location, then the mit-scheme executable won't find them.
For example, in my setup, MIT scheme is located at: /usr/local/scheme/bin/mit-scheme and the band, and other stuff, is located at /usr/local/scheme/lib/mit-scheme/.
For Emacs I've defined:
(defun mit-scheme ()
(interactive)
(run-scheme "/usr/local/scheme/bin/mit-scheme -library /usr/local/scheme/lib/mit-scheme"))
Which ensures mit-scheme can find its library when, in Emacs, I perform M-x mit-scheme. If I want MIT Scheme to be the default, I would use:
(setq scheme-program-name "/usr/local/scheme/bin/mit-scheme -library /usr/local/scheme/lib/mit-scheme")
You'll need to set your values according to your installation. You also might have an option of setting MIT Scheme related environment variables to point to your library location. The relevent one is MITSCHEME_LIBRARY_PATH.
$ mit-scheme --help
Usage: mit-scheme --OPTION ARG ... --OPTION ARG ...
This machine accepts the following command-line options. The options
may appear in any order, but they must all appear before any options
for the band.
--library PATH
Sets the library search path to PATH. This is a colon-separated
list of directories that is searched to find various library files,
such as bands. If this option is not given, the value of the
environment variable MITSCHEME_LIBRARY_PATH is used; if that isn't
defined, "/usr/local/lib/mit-scheme" is used.
--band FILENAME
Specifies the initial band to be loaded. Searches for FILENAME in
the working directory and the library directories, returning the
full pathname of the first readable file of that name. If this
option isn't given, the filename is the value of the environment
variable MITSCHEME_BAND, or if that isn't defined, "runtime.com"; in
these cases the library directories are searched, but not the
working directory.
...
I'm also trying to play with the materials from SICP. It took a couple of hours and attempts to get things working.
I realized my paths were wrong after following Don't Panic! on Mojave (macOS 10.14).
I installed mit-scheme 10.1.5 from source running the installation steps from here and in the readme. My paths were different, which caused the --band command to fail.
If you're having issues double check all your paths are what you expect.
I used make install with default paths to install to /usr/local/bin and /usr/local/lib
Mac comes with emacs 22.1.1, which is old, but it works with the extra scripts from the professor for customizing emacs.
NOTE: If you quit emacs 22.1.1, it doesn't warn you about stopping scheme, which will lead to a 100% CPU usage process that you can't stop without Activity Monitor. Newer versions of emacs will prompt you to quit the scheme process before exiting (there may be a setting for older versions as well).
I installed emacs 26.1 using Homebrew and was able to get everything running after fixing these issues:
There is an error on newer versions of emacs (24+?) with ad-advised-definition-p, fix it by commenting out the function in "multi-term.el" around line 334:
; (defcustom multi-term-dedicated-skip-other-window-p nil
; "Default, can have `other-window' select window in cyclic ordering of windows.
; In cases you don't want to select `multi-term' dedicated window, use `other-window'
; and make `multi-term' dedicated window as a viewable sidebar.
;
; So please turn on this option if you want to skip `multi-term' dedicated window with `other-window'.
;
; Default is nil."
; :type 'boolean
; :set (lambda (symbol value)
; (set symbol value)
; (when (ad-advised-definition-p 'other-window)
; (multi-term-dedicated-handle-other-window-advice value)))
; :group 'multi-term)
Path setup
I had a warning:
Warning (initialization): Your ‘load-path’ seems to contain
your ‘.emacs.d’ directory: ~/.emacs.d
This is likely to cause problems...
Consider using a subdirectory instead, e.g.: /Users/paulsolt/.emacs.d/lisp
Which I solved by pointing the top line of the ~/.emacs file to:
;; (setq load-path (append (list "~/.emacs.d") load-path))
(setq load-path (append (list "/Users/paulsolt/.emacs.d/config") load-path))
The config folder already had all the scripts, that I think I was having issues getting Emacs to see.
I installed scheme from source without modifying install paths, so I changed this line:
;; This is the place where you have installed scheme. Be sure to set
;; this to an appropriate value!!!
;;(setq scheme-root "/mit/6.945")
(setq scheme-root "/usr/local")
Next to fix the issue that was causing --band to fail I modified this line.
(setq scheme-program-name
(concat
scheme-root "/bin/mit-scheme "
"--library " scheme-root "/lib/mit-scheme-c "
;;"--band " scheme-root "/lib/mit-scheme-c/all.com "
"--band " scheme-root "/lib/mit-scheme-x86-64/all.com "
"-heap 10000"))
My scheme library was installed in a mit-scheme-x86-64 folder.
Lastly, it appears that there's some other commands I had to comment out, since they don't seem to exist ...
;;(tool-bar-mode -1)
;;(scroll-bar-mode -1)
After all that setup I'm now able to create a scheme script in emacs, and I can run the script using the meta key (iTerm set my Alt key to Esc+):
M-x run-scheme
Tip: Being newer to emacs, I'm working through the emacs tutorial (C-h t) which is essential if you're going the text-based approach. In college I used emacs outside of terminal, but I'm giving it go inside the terminal this time to learn more hotkeys.

Garbage characters in my Windows Emacs shell. . . not sure what's wrong

I'm running GNU Emacs (23.1.1) on Windows and when I run a cygwin shell inside emacs I get garbage like the following, after each command:
^[]0;/cygdrive/c/emacs-23.1/bin
^[[32user#HOST [33m/cygdrive/c/emacs-23.1/bin[0m
Note: I've replaced my real user and host name with user/HOST.
I've found a few links that describe how to set up emacs with cygwin on Windows including this one on SO:
How can I run Cygwin Bash Shell from within Emacs?
That hasn't helped. As of right now, I don't have anything referencing shells in my .emacs file except for this:
;; For the interactive shell
(setq explicit-shell-file-name "bash")
So I'm not even sure how emacs is finding my cygwin shell. Any help is appreciated. Thanks.
Try adding these lines to your .emacs file
(autoload 'ansi-color-for-comint-mode-on "ansi-color" nil t)
(add-hook 'shell-mode-hook 'ansi-color-for-comint-mode-on)
I'm not sure if these are the correct ones but let me know (as my .emacs file has gotten quite large over the years)

Getting Windows 7 SUA's bash shell working with emacs (EmacsW32)?

Having recently purchased Windows 7 Ultimate in order to gain access to the SUA - http://www.suacommunity.com - subsystem, I have been struggling to get SUA's bash utility (/usr/local/bin/bash) working with EmacsW32. SUA comes with ksh and csh by default, so I installed a community bundle to obtain the bash process.
M-x shell normally invokes a shell process and pipes stdio through an Emacs buffer. This works well with Cygwin. I have tried adjusting emacs variables like w32-shell-* to point it at the SUA bash executable, and also tried invoking bash via the posix.exe tool provided with SUA. I often see that the file descriptors associated with the bash process are deleted as soon as the process is created by EmacsW32.
Cygwin is very slow compared to SUA, so I am very keen to get this facility working with the EmacsW32 + SUA combo. Any tips, experience, solutions would be appreciated.
I don't know the w32-shell-* variables. Maybe you could show some code to illustrate what you mean.
I also don't know SUA.
I use GNU emacs v22 on Windows, and I run powershell as an inferior shell. I had some difficulties initially, similar to yours, and solved them with a better understanding of how to start the shell. Maybe this will help you.
I use these variables:
(setq explicit-shell-file-name
"c:\\windows\\system32\\WindowsPowerShell\\v1.0\\powershell.exe")
(setq explicit-powershell.exe-args
'("-Command" "-" )) ;; interactive, but no command prompt
I had difficulty until I realized two things:
if you don't specify command-line arguments for the shell process, emacs silently defaults to using -i. In the case of powershell, that argument was either not supported or it did something other than what I wanted (I forget). So I had to explicitly set the arguments to invoke the shell.
the name of the variable for specifying arguments to the shell, depends on the name of the program used to start the shell. This isn't documented as far as I know (unless you consider the source code to be documentation!). If you're on Linux and running sh, then the variable is explicit-sh-args. If bsh, then explicit-bsh-args. If you're on Windows, you need to use the proper name of the exe, including the .exe suffix. It makes for a strange looking variable name, but it works.
The full code to start powershell as an inferior emacs shell is like this:
;; get a name for the buffer
(setq buffer (get-buffer-create "*PowerShell*"))
(let ((tmp-shellfile explicit-shell-file-name))
(setq explicit-shell-file-name
"c:\\windows\\system32\\WindowsPowerShell\\v1.0\\powershell.exe")
(setq explicit-powershell.exe-args
'("-Command" "-" )) ; interactive, but no command prompt
;; launch the shell
(shell buffer)
; restore the original shell
(if tmp-shellfile
(setq explicit-shell-file-name tmp-shellfile)))
Maybe something like that would work for you.

Emacs is ignoring my path when it runs a compile command

I'm trying to get a compile command (rake cucumber) to run with a specific ruby version on my Mac OS X system, I use rvm to do this currently in the terminal. My ~/.MacOSX/environment.plist has the correct path in it, but emacs insists on prepending to this path and therefore making it useless. I've also tried:
(when (equal system-type 'darwin)
(setenv "PATH" (concat "/Users/fearoffish/.rvm/bin:/Users/fearoffish/.rvm/rubies/ruby-1.8.7-p249/bin:/Users/fearoffish/.rvm/gems/ruby-1.8.7-p249/bin:/Users/fearoffish/.rvm/gems/ruby-1.8.7-p249%global/bin:/Users/fearoffish/.rvm/bin"))
(push "/Users/fearoffish/.rvm/bin" exec-path)
(push "/Users/fearoffish/.rvm/rubies/ruby-1.8.7-p249/bin" exec-path)
(push "/Users/fearoffish/.rvm/gems/ruby-1.8.7-p249/bin" exec-path)
(push "/Users/fearoffish/.rvm/gems/ruby-1.8.7-p249%global/bin" exec-path)
(push "/Users/fearoffish/.rvm/bin" exec-path))
It was the desperate attempt of an emacs beginner to get what I wanted. It still prepends in front of it, so my path ends up being:
/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin:/Users/fearoffish/.rvm/bin:/Users/fearoffish/.rvm/rubies/ruby-1.8.7-p249/bin:/Users/fearoffish/.rvm/gems/ruby-1.8.7-p249/bin:/Users/fearoffish/.rvm/gems/ruby-1.8.7-p249%global/bin
I don't want /usr/bin and others prepending, I want my path first and the emacs prepended path to be at the end, I reckon this would fix my problem.
I test this by simply opening Aquamacs and running meta-x compile and then echo $PATH.
Any ideas?
A small modification to the solution by sanityinc (couldn't find a way to enter it in the comments above -- is that just me?)
I use -l option to the shell to force a login shell (which reads .profile or .bash_profile), rather than an interactive shell (which only reads .bashrc).
I do some string trimming on the returned path (as inspection shows a newline sneaking in).
Modified code:
(defun set-exec-path-from-shell-PATH ()
(let ((path-from-shell
(replace-regexp-in-string "[[:space:]\n]*$" ""
(shell-command-to-string "$SHELL -l -c 'echo $PATH'"))))
(setenv "PATH" path-from-shell)
(setq exec-path (split-string path-from-shell path-separator))))
(when (equal system-type 'darwin) (set-exec-path-from-shell-PATH))
Everyone seems to have misunderstood the original issue: the path is already setup correctly in Emacs, and the correct path is already passed to the shell started by the compile command! So what gives? Here is the answer:
In MacOS X, there is a small tool called path_helper(1). It is called by default from /etc/profile, which is executed by Bash on shell startup. When you start a compilation from Emacs, it launches a shell (which by default is Bash on MacOS X), and therefore executes this path_helper tool. And here comes the key point: path_helper rearranges your path, moving the standard directories like /usr/bin in front of your custom added directories, no matter where you originally added them. Try this yourself by opening a shell and first having a look at what PATH is, and then execute /usr/lib/path_helper and have look at the resulting PATH!
The brute force solution for you might be to simply comment out the call to path_helper in /etc/profile. Note however that then you won't automatically get the paths in /etc/paths.d setup by path_helper, which is the tool's main purpose.
I don't have a Mac, so I cannot test this directly, but this can all be found in the *info* page Interactive Inferior Shell.
When you start a shell in Emacs, the process that gets spawned is the program in the Emacs variable explicit-shell-file-name (and if that is nil, the environment variables ESHELL and SHELL are used).
It then sends the contents of ~/.emacs_*shellname* (e.g. if your shell is csh, then ~/.emacs_csh would be sent over. Also, the appropriate .rc files for csh program is sourced, so you can update that as well (in my case .cshrc). Additionally, you can wrap customizations in the .rc file with a check for the environment variable INSIDE_EMACS (which which Emacs sets before it runs a shell).
You need to update those files to change the path in the shell, not the Emacs variable exec-path. exec-path - which is just a list of directories Emacs uses to find executable programs. The behavior of the executables are not affected by changes to exec-path.
I find the environment.plist scheme on Macs pretty ugly, so I use the following snippet, which assumes you want Emacs to use the same PATH that you see in your Terminal.app:
(defun set-exec-path-from-shell-PATH ()
(let ((path-from-shell (shell-command-to-string "$SHELL -i -c 'echo $PATH'")))
(setenv "PATH" path-from-shell)
(setq exec-path (split-string path-from-shell path-separator))))
(This works for me in Emacs 23; haven't tried it in other versions, but I'd expect it to work.)
try this maybe. replace path string with yours.
(add-to-list 'load-path "~/opt/swank-clojure/src/emacs")
As far as I observed, Emacs takes the path variable from the shell it is launched from, so one solution is to change $PATH in the shell before you launch Emacs.
One other approach I used, which is more flexible, is to use a Makefile and append a "source ~/script_that_set_path" in front of each make commands you have.
I have tried so many different approaches to this that ended up not using emacs to setup my compilation command environment.
What I do now is to create a run_helper.sh file that simply initializes a clean environment and then uses exec $* to execute the command passed as argument to run_helper.sh
This run_helper.sh is usually project specific, but I keep a template which I use to start with when I create a new project.
Then I simple run compile from emacs like bash run_helper.sh rspec path/to/tests for example.
If I am using this to run ruby tests, my helper initializes RVM to use the proper ruby and gemset. If I am using some other language it may just export required environment variables or perform some other initialization, but this way I can do it in bash script instead of always having to mess with emacs paths and elisp every time I start a new project.
Here's an example of a run_helper.sh file
#!/bin/bash
cd /Users/simao/Documents/sp
export DYLD_LIBRARY_PATH="/usr/local/mysql/lib:$DYLD_LIBRARY_PATH"
source "$HOME/.rvm/scripts/rvm" # This loads the proper ruby and gemset from .rvmrc
export RAILS_ENV=test
exec $*
This also makes my tests run faster because I have lots of stuff in my .zshrc that I don't want to load just to run some tests.
It worked for me with two things.
First I followed sanityinc advice
An improved and modified version of the code snippet is now published as elisp library called exec-path-from-shell; installable packages are available in Marmalade and Melpa
I still had a problem with compile commands. Valko Sipuli is right there was a problem involving path_helper.
I commented the corresponding line in /etc/profile and it did not help. Problem still there.
I don't use bash but zsh. Digging a little I found /etc/zshenv. This file also calls path_helper.
After commenting the path_helper section in /etc/zshenv my path is finally correct

Resources