Shell script mode automatically at each emacs start - emacs

Each time when I edit bash script I type a command M-x shell-script-mode. And then I get nice shell code higlighting. How to get it automatically each time I start emacs so I do not have to type the command. When I added (shell-script-mode) to init.el it did not help.

You can set the default major mode to be whatever you want by adding
(setq-default major-mode 'shell-script-mode)
to your init file. That will ensure that any newly created buffer will be in shell-script-mode unless its mode is specified otherwise (e.g. through auto-mode-alist). Whether it's a good idea or not, I don't know: I probably would not want that to be my default setting - but to each her/his own.

One of the simplest ways to have Emacs set the desired mode for a buffer editing a file is to include a special comment in the first line of that file, e.g. for a shell script your first line might be:
# -*-sh-*-
For scripts it is also common, or and often even required, to have an interpreter file comment on the very first line of the file, which of course would preclude having an Emacs mode comment, so Emacs also looks for interpreter file comments and associates those with a major mode, so the first line of your shell script might be:
#!/bin/sh
There are a number of other ways to tell Emacs how to set the buffer mode when visiting a file. See, for example, Emacs Manual: Choosing File Modes

Related

What "local rules" have been set in Emacs?

I have been trying to solve a sh-mode indentation issue in Emacs (I get a double indent after a then) and found that I can set the indentation by hand and then run C-c > to automatically configure the indentation.
However, this configuration only applies to my current session and I can't seem to find any variables that have been modified as a result of the auto-configuration. (It worked though: the indentation is consistently what I want.) When it has run, it says "Local rules set" but doesn't tell me what local rules have been set.
And of course, when I restart Emacs, the local configuration is lost and I have to run the auto-config command again.
I've found a number of questions on this site about listing variables and their values, but I can't find out how to list ones that have only been changed for the current session. I even used this post to dump variables before and after running the C-c > command and comparing the output; nothing obviously different.
So I suppose my question is twofold:
If session variables are what's meant by "local rules" then how do I find out which ones were set?
If "local rules" means something else, then what is that and how can I somehow transfer those changes to my ~/.emacs file?
I think the result depends on your version of Emacs, but with a recent version, this is handled by SMIE, so the personal settings are kept in the smie-config variable.
The local settings in use in the current buffer are kept in an internal variable (smie-config--buffer-local). You can move them over to smie-config and save them into your ~/.emacs by calling smie-config-save.
One more detail: smie-config-save doesn't in itself save the settings to ~/.emacs. It only transfers them from the transient buffer-local variable smie-config--buffer-local to the global smie-config variable which is under the control of Customize and can have be saved to your ~/.emacs via something like M-x customize-save-customized. This should arguably be improved in smie-config-save. I suggest M-x report-emacs-bug to ask for this improvement.
This is more of an incomplete workaround than an answer, but it's a start!
The idea is to trigger sh-learn-buffer-indent as soon as a shell script is loaded. Add the following to your ~/.emacs file:
(add-hook 'sh-set-shell-hook 'sh-learn-buffer-indent)
A couple of important notes:
It assumes that your shell script is already correctly formatted when it's loaded.
It can be slow for large files.
(This is apparently mentioned in sh-script.el and was brought to my attention by Noam Postavsky via the bug report I filed.)

Open file from Emacs terminal without find-file

If I'm in a term-mode buffer and there is a file path displayed, how would I go about making the path "clickable", opening the file in a new buffer? It doesn't have to be mouse-clickable, in fact I'd prefer a key binding that works when the point is on the file path. Other than the common case of using ls, this function could be used when viewing a log file. Some debug info contains the file path and line number. Something like lib/library.rb:34 for example. Ideally, Emacs could open a new buffer and move the cursor to line 34.
The short answer is: don't work against Emacs. Let Emacs work for you.
While you can use find-file-at-point or put together something yourself, you will be much better off running make, grep and other stuff which prints "dir/file:pos" using M-x compile or M-x grep.
If you need to interact with your program which prints "dir/file:pos", you can pass a prefix argument to compile and the compilation buffer will be interactive.
If you have an arbitrary program whose output starts with "dir/file:pos", e.g., rails server, all you need to do is run it as (grep "rails server").

How to have find-file's prompt match Emacs shell's $PWD?

With Emacs, if the current buffer is one that's "visiting" a normal file (for example), whose full pathname is /path/to/somefile, and one runs find-file (C-x C-f), the prompt that appears in the mini-buffer is something like
Find file: /path/to/▮
...with the cursor placed as indicated above by ▮. IOW, the suggested path shown by default is always to the directory containing the file that the current buffer is visiting.
If, however, the current buffer is an Emacs shell process, and one runs find-file, then, AFAICT, the path shown in the prompt remains fixed at the value of $PWD when the shell process was started, irrespective of the current value of $PWD:
Find file: /pwd/at/startup/▮
This behavior is not so useful, because the $PWD at startup often becomes irrelevant later on. It would be really nice if the directory shown in find-file's prompt were instead the shell process's current $PWD.
Is there a simple way to modify find-file to behave this way whenever the current buffer is a shell process?
You want "shell directory tracking". E.g. check dirtrack-mode or shell-dirtrack-mode.
shell-dirtrack-mode tries to parse "cd" commands, (event_jr: which in my experience does not work consistently). dirtrack-mode uses the prompt regexp, which works very well.
There are a number of ways to manage this. As Stefan notes, there are a couple of built in packages that manage it.
My preferred way is to alter your prompt (when in Emacs) to have the $PWD embedded in it, and then Emacs strips it out and uses it. This has the benefit of always being up to date. I've found that dirtrack-mode sometimes gets out of sync.
Check out my solution here, which is a modification of a similar implementation on the Emacs Wiki.

Emacs: How can I automatically start a python interpreter in python-mode?

After every Emacs startup in python-mode I need to open the interpreter in a different buffer and
change the size of the new buffer that it fits emacs-code-browser's history. I want to automate
this activity.
How can I automate that an interactive python shell always appears (after Emacs startup) in an extra buffer like in the screenshot?
UPDATE: See below: workgroups.el and emacs-code-browser seem to collide. See screenshot
Before Editing
After Editing
I use Emacs23, emacs-code-browser and python-mode.el.
UPDATE: I tried to use workgroups.el. I defined a new python workgroup and added the following line to my .emacs: (wg-load "~/.emacs.d/workgroups/python_workgroup.wg"). The windows are, however, messed up. You can see it on the screenshot below:
It think that emacs-code-browser and the settings of my workgroup collide. Is there a way
to avoid this behavior?
also you may try desktop-save-mode:
desktop-save-mode is an interactive autoloaded Lisp
function.
(desktop-save-mode &optional ARG)
Toggle desktop saving (Desktop Save mode).
With a prefix argument ARG, enable Desktop Save mode if ARG is
positive, and disable it otherwise. If called from Lisp, enable
the mode if ARG is omitted or nil.
If Desktop Save mode is enabled, the state of Emacs is saved from
one session to another. See variable desktop-save' and function
desktop-read' for details.
How about using workgroups.el ?
It is used for saving your window configuration which is exactly what you need.
This way you can create a workgroup called for example Python, adjust buffers (including one containing interactive python shell) and everything and save the group, and next time you just open workgroup Python and all buffers open as they were before. If you want it to open automatically when emacs is started, just set your saved Python workgroup as a default workgroup.

How to force emacs to use \n instead of \r\n

I have to use windows to write some shell scripts. I decided to use emacs, but I get a weird error when running the script:
/bin/bash^M: bad interpreter: No such file or directory
Correct me if I'm wrong, but that looks like the shebang ends in \r\n instead of just \n. How can I tell emacs to only write \n? I'm in Shell-script major mode. It's quite surprising this isn't fixed by default.
As Jürgen mentioned, you need to use the set-buffer-file-coding-system. You can say
(set-buffer-file-coding-system 'unix)
and stick that into a function inside the find-file-hook so that it will set it for all the buffers you open. Alternatively, you can put it inside the write-file-hook list so that the file-coding-system is set properly before you dump the file to disk.
For a simpler way out, if you're using the GUI version of Emacs, you can click on the 3rd character in the modeline from the left. It's to toggle between eol formats.
Use:
set-buffer-file-coding-system
(should be bound to a key-sequence) before saving the file.