how to configure emacs to start in a directory (windows) - windows

I am running emacs 25.1 under Windows. It is a standalone binary, not running under cygwin
I have a .emacs file that works on both windows and linux. On Windows, however, the command:
(cd "c:/Users/xxx/git")
does not start emacs in that directory. The command works, because if I evaluate the buffer, it does go to that directory. It is as though something else is executing AFTER my script causing emacs to default to the stupid windows default, wherever the code is.
Any workaround to make emacs start in my desired directory?

Create a Windows shortcut to the Emacs executable, and use that to start Emacs. Create the shortcut by right-clicking the file runemacs.exe in folder bin and choosing Create shortcut.
Then fill out the Properties in the Shortcut tab:
Field Target has the command for starting Emacs: the location and name of the binary (executable) followed by whatever options you want and any file or directory that you want to start editing.
Field Start in has the directory that I want Emacs to start in.
For example:
Target: D:\Emacs-25.1\bin\runemacs.exe --debug-init "d:\usr\some-user-name\some-directory"
Start in: d:\usr\some-user-name\some-directory
Then just double-click your shortcut to start Emacs. Or single-click it, if you pin it to the Task Bar.
You can create as many such shortcuts as you want, either to the same Emacs executable (e.g. with different options or startup directories) or to different executables (e.g. different Emacs releases).

Try using emacs-startup-hook, which runs after processing the commandline and init.el.
(defun jpk/emacs-startup-hook ()
(cd "/some/path"))
(add-hook 'emacs-startup-hook #'jpk/emacs-startup-hook)
If that doesn't work, some package is setting the CWD, and you'll have to track it down.

Related

How to run Script per shortcut

I have a Terminal cmd running that executes a function from the .bash_profile file (as described here).
From the description, I added the function to the ~/.bash_profile (can be found here /home/user/.bash_profile)
From my Terminal everything works !
But how do I create a shortcut in Mac OS that executes this Terminal cmd ??
I tried Apple Automator (--> but error: Cmd not found)
I tried Alfred (--> but same error: Cmd not found)
How can I make this Terminal cmd execute from a keyboard-shortcut ??
Do I need to place the script-functions in another file (other than bash_profile ?) - or what is here to do ?
Here is an image of the Automator trial:
Place the functions in separate files, such as suspend-script.sh, resume-script.sh, and kill-script.sh; put them in a common directory like /home/user/scripts. To keep using them in your .bash_history, do:
# in .bash_history
source "suspend-script.sh"
source "resume-script.sh"
source "kill-script.sh"
Make sure you set the executable bit on each script (chmod +x /home/user/scripts/*.sh). Then, use the following for shortcut keybindings:
/home/user/scripts/suspend-script.sh thing_to_suspend
/home/user/scripts/resume-script.sh thing_to_resume
/home/user/scripts/kill-script.sh thing_to_kill
Since it's a shortcut to a function, you have to pick one script to suspend/resume/kill (the thing* above).
For example, if you wanted to suspend/resume/kill the top process, you would use the following:
/home/user/scripts/suspend-script.sh top
/home/user/scripts/resume-script.sh top
/home/user/scripts/kill-script.sh top
Open Automator, choose Application, add a Run Shell Script action and put in your Shell command between quotes (if you have a file, you can just drag and drop it).
Other than playing it, now you can save it (as an app anywhere) and even set the icon. Then you got an app and then you can define a keyboard shortcut for it in system-preferences.app
This should work, at least it does for me.
make sure your script is executable: sudo chmod u+x <your-scriptname>
and also add the filename extension .command to your filename. So the name of the script should be filename.command.
When you then click the script in Finder, it gets executed.
I hope that works!

How do you run programs in Windows terminal?

What is the windows equivalent to "./filename"
So for example I would usually compile by doing something like:
gcc -c homework1.c
gcc -o homework1 homework1.o
This would give me the executable names homework1
And for me to run the program, I would type: ( ./homework1 ) <-- ignore the parenthesis.
Usually I was write all my code in my schools Unix Shell thingy and I also compile it and run it there, but recently I think I took up all the disc space (because it says "disc quota exceeded").
Run cmd.exe
Go to where the program is example : cd C:\foder1\
Then type the program name with extension, for example : test1.exe or "test1.exe"
In windows (as in Linux) you can either run a program though a GUI interface or from a shell environment.
The GUI option is a program called Explorer, you navigate through the file system and double click executable files to run then. Executable typically have the extension '.exe' or '.bat', but there are others.
The shell environment in windows is called the 'command prompt', you can run it by going to the start menu and selecting 'run' or simply press the windows key and 'r' simultaneously. A box will popup, type 'cmd' (without the quotes) and hit enter - the command prompt should open. From there you can navigate the file system using commands like 'cd'. To run your executable type the name of the file (it should work with or without the '.exe').
A nice shortcut to open the command prompt already at a particular path, is to browse to the folder in Explorer, hold shift and then right-click the folder - the resulting context menu that pops up should have an option like 'open in command prompt'.

Running raco on the Windows command line

I am trying to make a stand-alone Racket executable on the Windows platform. How do I go about running raco from the windows command line? I'm not familiar with it.
If I use the documentation and enter the following command into cmd.exe:
raco exe --gui main.rkt
cmd.exe tells me:
'raco' is not recognized as an internal or external command, operable program or batch file.
Substituting in raco.exe tells me the same thing.
I also tried typing:
'C:\Program Files\Racket\raco.exe' exe --gui .\main.rkt
into powershell and it gave me an Unexpected token 'exe' in expression or statement error.
For the first problem: you need to add to windows' %PATH% (an environment variable) the path to the executable. For the second problem: check the correct syntax for the exe command, and/or the "--gui" modifier, they're being misused. For instance, try this after solving the first problem:
$ raco.exe exe main.rkt
The above will create an executable main.exe file.
Download and install Racket, which includes DrRacket. (Of course, you’re welcome to use your preferred text editor, but the tutorials will assume you’re using DrRacket.)
Update the PATH envi­ron­ment vari­able on your system to include the direc­tory that holds the racket appli­ca­tion. On Mac OS and Linux, this path will be some­thing like "/path/to/racket/bin". On Windows, it’ll be some­thing like "C:\Program Files\Racket". Then, from the terminal, you’ll be able to run racket and raco (see raco: Racket Command-Line Tools).
Windows users who haven’t altered your PATH before: don’t panic. To add the Racket command-line programs to your Windows 10 PATH, click the Windows search box, type the word path, and then click on Edit the system environment variables. Click on the Environment Variables button. In the top window, which contains your user variables, find Path and double-click it to open. Click the New button and either use the Browse button to select your Racket directory, or manually enter its path. Restart your Windows terminal (either the Command Prompt or PowerShell) and now racket and raco should work.

Emacs init file won't load at start up

I'm trying to run Emacs v22.2 on a Windows 7 computer. However, the init file isn't loaded at start up (loading it manually with M-x load-file works fine).
I've tried using both:
~\.emacs, ~\_emacs
~\.emacs.d.init.el
but the problem persists.
Evaluating (insert (getenv "HOME")) returns the expected value.
Depending on how you open emacs in Windows 7, it will look in different places for the .emacs file. If call it from within a shell (in cygwin, Msys, etc) it will look in the $HOME (~) location, if you run it form the installation directory by clicking on the icon, it looks for this file in the %APPDATA% location ( usually C:\Users\your user name\AppData\Roaming ). This can lead you in a merry chase all over the place. I found that it is best to determine where you want the file to be stored, and the create symbolic links ( using windows mklink utility ) to all other possible locations.
I faced a similar issue(on windows 10). The problem was that Emacs was reading ~\.emacs instead of ~\.emacs.d\init.el on startup. Shifting the contents of ~\.emacs to ~\.emacs.d\init.el and deleting ~\.emacs solved the issue.
I just installed the patched version of Emacs on Windows 7, specified the environment variable HOME=c:\klang, checked out my decade old configuration files from github and added
(and (= emacs-major-version 23)
(defun server-ensure-safe-dir (dir) "Noop" t))
to ~/.emacs.d/init.el and was up and running.
What you are missing is some component from mule.el, just install the new version of emacs to fix it.
If HOME is not set in the genereal environment, (getenv "APPDATA") will probably take over and emacs will try to read your init file somewhere under that directory.
I've just managed to solve the same problem (lcollado and klang's answer gave me a small hint on how to fix it).
I had set up a configuration file at C:\Users\Edwin\.emacs.d\init.el and when I tried to get Emacs to load it, it didn't. My initial thought was to make an init.el file at C:\Users\Edwin\AppData\Roaming\.emacs.d\init.el which would load my original configuration file. However, I wanted a simpler solution.
Then I remembered that Symbolic Links exist. So I did a few searches on how to make a symbolic link in Windows and the difference between Hard Links and Soft Links.
My first attempt was to make a soft link that pointed to my init.el. But Emacs ignored the link and started without the init.el.
The next attempt that did work was a hard link. The steps that I did was as follows:
Open Command Prompt with Administrative Privileges.
a. Press Windows + R.
b. Type "cmd.exe" and press Shift + Enter.
c. Tap "Yes" when Windows asks for Administrative Privileges.
Go to your home directory.
a. Type cd C:\Users\<your name>.
Run the following command to make a hard link in C:\Users\<your name>\AppData\Roaming\.emacs.d\init.el:
:: mklink /h Destination Source
:: Destination - Where do you want the hard link to be and what will be it's name?
:: Source - What file do you want to link?
mklink /h AppData\Roaming\.emacs.d\init.el .emacs.d\init.el

Where can I find my .emacs file for Emacs running on Windows?

I tried looking for the .emacs file for my Windows installation for Emacs, but I could not find it. Does it have the same filename under Windows as in Unix?
Do I have to create it myself? If so, under what specific directory does it go?
Copy and pasted from the Emacs FAQ, http://www.gnu.org/software/emacs/windows/:
Where do I put my init file?
On Windows, the .emacs file may be called _emacs for backward compatibility with DOS and FAT filesystems where filenames could not start with a dot. Some users prefer to continue using such a name, because Windows Explorer cannot create a file with a name starting with a dot, even though the filesystem and most other programs can handle it. In Emacs 22 and later, the init file may also be called .emacs.d/init.el. Many of the other files that are created by Lisp packages are now stored in the .emacs.d directory too, so this keeps all your Emacs related files in one place.
All the files mentioned above should go in your HOME directory. The HOME directory is determined by following the steps below:
If the environment variable HOME is set, use the directory it indicates.
If the registry entry HKCU\SOFTWARE\GNU\Emacs\HOME is set, use the directory it indicates.
If the registry entry HKLM\SOFTWARE\GNU\Emacs\HOME is set, use the directory it indicates. Not recommended, as it results in users sharing the same HOME directory.
If C:\.emacs exists, then use C:/. This is for backward compatibility, as previous versions defaulted to C:/ if HOME was not set.
Use the user's AppData directory, usually a directory called Application Data under the user's profile directory, the location of which varies according to Windows version and whether the computer is part of a domain.
Within Emacs, ~ at the beginning of a file name is expanded to your HOME directory, so you can always find your .emacs file with C-x C-f ~/.emacs.
There's further information at HOME and Startup Directories on MS-Windows.
It should be stored in the variable user-init-file. Use C-H v user-init-file RET to check. You can also open it directly by using M-x eval-expression RET (find-file user-init-file) RET
Open the file like this in Emacs for Windows:
C-x C-f ~/.emacs
More information in the Emacs Wiki
On my Vista box it's in C:\Users\<USER>\AppData\Roaming\
Note that it may NOT be enough to just type Ctrl-x Ctrl-f ~/.emacs and create the file.
It may be that your Emacs application uses a different place to store your init file, and if so, then creating the file ~/.emacs simply creates a useless file which your Emacs application ignores.
Also, you may want to do more than just access the .emacs init file, but you may want to know where it is, i.e., its pathname.
To get at this there are two methods:
Easy way: type Ctrl + H V user-init-file Return
Slightly trickier way:
You can find out where your system is storing its own .emacs file by:
Click options and scroll down to "Set Default Font..."
Change the font setting and click okay
On the options menu, go down to "Save Options"
When the options are saved, the system saves its .emacs file,
and you can read the file path in the minibuffer at the bottom of the Emacs screen
In Windows 7 put your init.el file in C:\Users\user-name\AppData\Roaming\.emacs.d\, where user-name is your user/login folder.
Take care so your init.el file won't be named init.el.txt. This is something Windows does if you create your file with some editor like Notepad.
On versions of Emacs on Windows above 22, it seems to have moved to
~/.emacs.d/init.el
, ~ being the value of your environment variable HOME (see Control Panel → System → Advanced → Environment variables).
The file itself might not exist. In that case just create it.
You must create an emacs initialization file. One is not automatically created.
I had a similar issue and this answer tracks down what I did.
My issue was my ~/.emacs.el file was not loading. Strange because this has always worked for me.
This question/answer helped me but I had to put my init file in the %USERPROFILE%\AppData\Roaming\.emacs.d\init.el because this is apparently the default behavior on Windows.
To troubleshoot this, I ran the following in the emacs *scratch* buffer.
user-emacs-directory
"~/.emacs.d/"
When I saw user-emacs-directory was ~/.emacs.d, I simply moved my .emacs.el file to %USERPROFILE%\.emacs.d\init.el. But this still didn't work.
I continued with expand-file-name as shown below:
(expand-file-name user-emacs-directory)
"c:/Users/pats/AppData/Roaming/.emacs.d/"
Got to love how Windows works. (not) So I moved my emacs.el file to the %USERPROFILE%\AppData\Roaming\.emacs.d\init.el and this worked. The file was now being read. But I got other errors because my initialization file loaded other (personal emacs) files (in ~/myenv/emacs/*.el.
Warning (initialization): An error occurred while loading ‘c:/Users/pats/AppData/Roaming/.emacs.d/init.el’:
Hum... Seems like all my files ~/myenv/emacs/*.el would need to be moved in order for this to work but I didn't want to do that. Then I realized that because the HOME environment variable was not set, emacs was performing its default behavior.
SOLUTION
Once I set my windows HOME environment variable to %USERPROFILE% everything began to work like it has for the past 25 years. :-)
To set the HOME environment variable, I typed WindowsKey+"edit environment variables for your account" to open the Environment Variables dialog box, and entered HOME=%USERPROFILE%.
Now my emacs initialization file .emacs.el is is back to its rightful place $HOME/.emacs.el and not in %USERPROFILE%\AppData\Roaming\.emacs.d\init.el
To be fair, if Windows had just one place to put files for user installed packages the solution of making HOME=%USERPROFILE\AppData\Roaming might be acceptable, but because some applications use %USERPROFILE%, some use %USERPROFILE\AppData\Roaming and others use %USERPROFILE\AppData\Local it just makes it difficult to know where to find your configuration files.
I prefer having everything in my %USERPROFILE% or $HOME directory.
Another similar question was here:
Emacs init.el file doesn't load
As kanja answered, the path to this file is stored in the user-init-file variable (or if no init file exists, the variable contains the default value for where to create it).
So regardless of which of the possible init file names you are using, and which directory it is in, you should be able to visit your init file with:
M-: (find-file user-init-file) RET
Or display its full path in the echo area with:
M-: (expand-file-name user-init-file) RET
On Emacs 23 and Windows 7 it only works if you set:
HKCU\SOFTWARE\GNU\Emacs\HOME
After Emacs 27.1, emacs has started respecting the $XDG_CONFIG_HOME. The init file or the init directory can now be found in $XDG_CONFIG_HOME/emacs/init.el.
In Windows $XDG_CONFIG_HOME could translate to %LOCALAPPDATA%.
In any case you can use the following emacs variables to find out the location of the your initialization file by M-x eval-expression
user-init-file
or the emacs configuration directory
user-emacs-directory
I've found that Emacs 22 will occasionally open either "C:\Documents and Settings\username\Application Data\.emacs", or just "C:\Documents and Settings\username\.emacs" on my XP machine. I haven't found an explanation for why it occasionally changes it's mind.
~ will always point to whatever the current instance of emacs thinks is HOME, but kanja's tip (C-h v user-init-file) will always tell you what ~/.emacs actually maps to.
On Windows 8.1, if Emacs is started from Windows Explorer, a shortcut or from cmd console it uses C:\Users\<USER>\AppData\Roaming.emacs init file. When I start Emacs from PowerShell, Emacs looks for its init file in C:\Users\<USER> folder. The fix to this issue was to set the HOME user environment variable (Control Panel\System and Security\System->Advanced system settings->Advanced->Environment variables) to C:\Users\<USER>. After this change, no matter how I start Emacs, it uses the same init file (see the accepted answer of this question)
On Windows XP it's:
C:\Documents and Settings\yourusernamehere\Application Data\
There is a list of directories based on your Windows version and extra information:
http://www.gnu.org/software/emacs/manual/html_node/emacs/Windows-HOME.html
For WIndows7& Emacs26.3:
if HOME environment is set, then the .emacs file should be in that folder.
otherwise, it should be in c:\.
In both cases, if .emacs is not there, _emacs should be used.
This is because we cannot create .emacs file according to the windows file naming rules.(but we can download or copy it from somewhere else).

Resources