Change the default directory of emacs with 'cocoa emacs' - macos

As explained in here, putting (setq default-directory "~/Desktop/mag" ) in .emacs is supposed to change the default directory.
When I do that with the emacs on my mac, it doesn't work. C-x C-f still shows ~/ not ~/Desktop/mag.
(cd "Users/smcho/Desktop/mag") also gives me this error - Error: No such directory found via CDPATH environment variable
What's wrong with them?

The directory that appears in the prompt for C-x C-f ('find-file') comes from the value of default-directory, which is a buffer-local variable. When you first start Emacs, the initial buffer displayed is the GNU Emacs buffer. That buffer's default-directory is set from the variable command-line-default-directory.
So, try this:
(setq command-line-default-directory "~/Desktop/mag")

The straight-forward answer to your question is:
(setq-default default-directory "~/Desktop/mag")
Reading the documentation for the variable (C-h v default-directory RET) you'll see:
Automatically becomes buffer-local
when set in any fashion. This
variable is safe as a file local
variable if its value satisfies the
predicate `stringp'.
That said, opening a file automatically sets the default-directory to the path of the file...
So, if you always want find-file to start at that directory, you can use this:
(global-set-key (kbd "C-x C-f") 'my-find-file)
(defun my-find-file ()
"force a starting path"
(interactive)
(let ((default-directory "~/scratch/"))
(call-interactively 'find-file)))
This question may be a duplicate of Preventing automatic change of default-directory. Though it's difficult to tell.

In addition to the notes above regarding default-directory, I had to also prevent the emacs splash screen from starting in order to make subsequent commands like dired actually show their buffer when invoked from .emacs on startup:
(setq inhibit-splash-screen t)

Related

gtags very slow in Emacs

I am using gnu global in emacs, everything is ok, but command "gtags-find-tag" search is too slow that cannot bear. only this command is very slow, the others like "gtags-find-rtag" is normal I think.
I delete other config such as cedet, so that exclude these effect, but this issue is still exist.
Do I miss some config steps of global or emacs? or anyone in other platform(linux) has this issue?
===
my Emacs is emacs for Mac OSX version is 24.3, GNU global is 6.2.8, and my config file is
(add-to-list 'load-path (expand-file-name "~/.emacs.d"))
(setq load-path (cons "/Users/xxx/.emacs.d/gtags.el" load-path))
(autoload 'gtags-mode "gtags" "" t)
(add-hook 'c-mode-hook
(lambda ()
(gtags-mode t)
))
I don't use Global (I do keep meaning to try it), but looking at the code, gtags-find-tag and gtags-find-rtag are very similar functions.
I presume the difference must either be in the completing-read phase, or the find phase, both of which are calling Global.
In the completing-read, 'gtags' passes the option -c, while 'grtags' passes -cr
In the find phase, 'gtags' passes -x, while 'grtags' passes -xr.
I would firstly try to establish whether it is faster outside of Emacs than it is inside it. The above might be sufficient for you to test this, but you should be able to figure out the exact commands being used by using M-x debug-on-entry RET call-process RET (which you can subsequently cancel with M-x cancel-debug-on-entry), and then running the gtags functions and seeing which arguments appear in the stack trace for that function call.
If you can't make heads or tails of the stack trace, try copying and pasting it into your question.
Edit:
Based on the information from the comments, it's appears that it's the case-insensitivity argument which is, for some reason, slowing things down to an unreasonable degree.
You should be able to disable that behaviour here:
M-x customize-variable RET gtags-ignore-case RET

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)

Emacs M-x shell and the overriding of bash keyboard bindings

If I understand correctly, when running bash under M-x shell, Emacs overrides some of the bash keyboard bindings, and can make some bash commands and features not work as in "native bash" (for an example see this thread)
One of the commands that I use frequently in bash is clear-screen (bounded to Ctrl-l by default ).
Is there an easy way of asking Emacs to reproduce the same behavior in shell-mode so that I can clear my bash screen within Emacs?
I know I can always use M-x term to get a real terminal, but I would like to work out a solution for M-x shell
EDIT: When I type C-l C-l, Emacs moves the current line to the top of the window, but as soon as I enter a new command in bash, the window is scrolled again to the original position.
Thanks
Just type M-> to go to the bottom of the buffer (if necessary), then C-l C-l to move the current line to the top of the window. It looks as if you've cleared your screen, but all of the content that would have been erased is still present above the top of the window.
EDIT:
To keep Emacs from scrolling the window as you described, put this in your .emacs file:
(remove-hook 'comint-output-filter-functions
'comint-postoutput-scroll-to-bottom)
I found that behavior irritating as well.
You could re-bind C-l in shell mode to make it function in the manner you want:
(define-key shell-mode-map (kbd "C-l")
(lambda ()
(interactive)
(previous-line)
(end-of-line)
(let ((start (point))
(end (goto-char (point-min))))
(delete-region start end)
(kill-line)
(end-of-buffer))))

Creating a Cygwin emacs macro

I have been researching how to get Cygwin to work under emacs. I have it working, but now I want to write a macro that will do the following:
Launch by typing M-x cygwin
Have the script stored in some obvious place (probably my .emacs file)
M-x shell
Rename the buffer to cygwin (or cygwin1, cygwin2, cygwin3, ... if cygwin exists) probably using M-x rename-buffer
M-x ansi-color-for-comint-mode-on
M-x set-buffer-process-coding-system 'undecided-unix 'undecided-unix
Open and run cygwin
It will basically do all of the above steps. I think most of the organization for this little project is done. How do I tie it all together so I can just type M-x cygwin and see a happy new cygwin buffer? What exactly needs to be added to .emacs? Also, where exactly is .emacs in Windows?
I still want the ability to run M-x shell for the windows shell for now. I may also make similar macros for MSYS and ssh'ing to my Linux boxes. How do I get started?
Many questions, here are some answers:
Your .emacs can exist many places, it depends, read here. In short try C-x C-f .emacs, or check the value of the variable 'user-init-file (C-h v user-init-file).
I think the command you want is something along the lines of this:
(require 'comint) ; this does require comint
(defun cygwin ()
"do what i want for cygwin"
(interactive)
(let ((buffer (get-buffer-create (generate-new-buffer-name "cygwin"))))
(pop-to-buffer buffer)
(unless (comint-check-proc buffer)
(apply 'make-comint-in-buffer (buffer-name buffer) buffer "c:/cygwin/Cygwin.bat"
nil
nil)
(ansi-color-for-comint-mode-on)
(set-buffer-process-coding-system 'undecided-unix 'undecided-unix))))
Note: I directly ran the process Cygwin.bat instead of running shell and then starting that batch program. I believe the effect is the same, and more straight forward. I did choose the easy way out for naming the buffer (using 'generate-new-buffer-name) - you'll want to customize to what you want.
You can dump the above command in your .emacs easiest by doing the C-x C-f .emacs and pasting it in the buffer that gets opened up. Save it and restart (or do M-x eval-defun when your cursor is in the body of that command. Then M-x cygwin will run the command.

Emacs remote shell

I tend to run my shell in emacs, but when I use it to ssh into another computer this breaks tab-completion. Is there a way to fix this?
Try:
M-x cd /hostname:/current/path/in/the/shell
That should set up ange-ftp (or tramp), and then TAB completion for paths should work properly for that shell - until you log into a different machine.
You could set up a comint process filter to recognize when you type ssh to do that for you automatically, but that's difficult to get right as it should revert when you exit the ssh session, but not be tricked by other uses of exit.
For an automated solution, I'd suggest augmenting the approach I personally use to keep Emacs synchronized with the current working directory of the shell buffer. Just add an an extra bit of information with the hostname, and use that to set the hostname and path like shown above.
in another thread, someone mentioned eshell which I've never used but I tried it with SSH and all sorts of nice integration is happening. Time to learn eshell.
You could try M-x ansi-term to host your shell if your getting unexpected behavior with key mappings. Having said that I couldn't re-produce the problem your describing on your set-up.
I had a similar problem I think and solved it by editing my ~/.bash_login on the remove machine and append
export TERM=xterm
I use OS X and had problems when connecting to a Linux (Debian Lenny)
I just wrote a little function to open a shell on a remote host. The cd call before shell gets the tab completion working.
This may be different than you want, since it opens a new shell instead of ssh'ing in a local shell. Beyond that, you could look into hacking emacs Directory Tracking (or see who else has).
(defun remote-shell (&optional host)
"Open a remote shell to a host."
(interactive)
(with-temp-buffer
(let ((host (if host host (read-string "Host: "))))
(cd (concat "/scp:" host ":"))
(shell (concat "*" host "*")))))
(defun myserver-shell () (interactive) (remote-shell "myserver"))
M-x shell invoked in buffer A will switch to a shell buffer B; usually shell. Unsurprisingly it creates B if necessary. A prefix arg will cause it to ask for the name of B. If A is viewing something on a remote host then the shell will be run on the remote host. But only if it can't find an existing B. At that point file name completion will work.
There are some notes about how to tinker with this in the emacs wiki. See for example the function my-shell in this section, which will extend the default name for B so remote files get remote shells. I do that same thing by advising the shell function.
The filename auto completion will work fine. Command autocomplete? Less so. For me it blocks emacs and then doesn't actually work.
I use dired to access the remote machine and open a shell there.
Here is the function I use, taken and modified from Tikhon Jelviss' emacs configuration:
(defun anr-shell (buffer)
"Opens a new shell buffer where the given buffer is located."
(interactive "sBuffer: ")
(pop-to-buffer (concat "*" buffer "*"))
(unless (eq major-mode 'shell-mode)
(dired buffer)
(shell buffer)
(sleep-for 0 200)
(delete-region (point-min) (point-max))
(comint-simple-send (get-buffer-process (current-buffer))
(concat "export PS1=\"\033[33m" buffer "\033[0m:\033[35m\\W\033[0m>\""))))
Example:
(anr-shell "/vagrant#localhost#2222:/vagrant/")

Resources