How to specify a window in emacs to be used to open new file/buffer for?
When I'm opening a file, say in dired using o command, it is opened in a new window. But I want this file to be opened in a window exactly I've specified.
(defun dired-window () (window-at (frame-width) 1))
(eval-after-load 'dired
'(define-key dired-mode-map (kbd "o")
(lambda ()
(interactive)
(let ((dired-window (dired-window)))
(set-window-buffer dired-window
(find-file-noselect
(dired-get-file-for-visit)))
(select-window dired-window)))))
Related
Windows 7
Emacs 24.5
1.Open shell, by M-x shell.
2.Do some commands.
As result I get screen:
OK. Now I want to clear screen (in Windows consele this is a command "cls").
I want to get the next screen:
How I can do this in Emacs shell?
Run the emacs function "erase-buffer" to clear the buffer.
You could bind a function key to clear your buffer:
(global-set-key (kbd "<f10>")
(lambda ()
(interactive)
(erase-buffer)
(process-send-string nil "\n")))
This also work:
;; clear content of buffer
(defun clear-buffer-permenantly ()
"Clear whole buffer, contents is not added to the kill ring"
(interactive)
(delete-region (point-min) (point-max))
)
(global-set-key (kbd "<f12>")
(lambda ()
(interactive)
(clear-buffer-permenantly)
(process-send-string nil "\n")))
I'm using the Emacs Neotree plugin that provides a filebrowser sidebar for Emacs, and I have this function to close all buffers but the current one:
(defun kill-other-buffers ()
"Close all other buffers."
(interactive)
(mapc 'kill-buffer (delq (current-buffer) (buffer-list))))
It works ok, but it closes the neotree navigation too and I want it to remain open. Any idea on how I can get this done?
Since neo-buffer-name is defined as *NeoTree* in neotreel.el, the following change should do the trick:
(mapc 'kill-buffer (delq neo-buffer-name (delq (current-buffer) (buffer-list))))
I use emacs for notes mainly. All my notes are in:
~/Dropbox/Uni/Notes
I want to tie a keyboard shortcut (e.g C-f12) to do a helm-find that always starts in the above dir irrelevant of the source buffer.
I have tried:
(global-set-key (kbd "C-<f2>") (lambda () (interactive) (helm-find "~/Dropbox/Uni/Notes/")))
But when I run it, it still prompts me for 'DefaultDirectory' which is usually the same as the current buffer.
?
[edit]
I made a hack-around:
(global-set-key (kbd "<C-f2>")
(lambda ()
(interactive)
(find-file "~/Dropbox/Uni/Notes/leo.org")
(helm-find nil)))
That opens a file and then when I do a helm-find, it's relative to leo.org's location. But a better solution would be preferred.
[edit]
Below solution works perfectly.
Here you go:
(defmacro helm-find-note (dir)
`(defun ,(intern (format "helm-find-note-%s" dir)) ()
(interactive)
(let ((default-directory ,dir))
(helm-find nil))))
(global-set-key (kbd "C-M-3") (helm-find-note "~/Downloads"))
Suppose I have two text files A.txt and B.txt. I choose to open both from Emacs using
emacs -no-splash -mm A.txt B.txt
Now the frame is split in two parts vertically, and file A is shown in the left window and file B in the right window. However file B is automatically selected by Emacs. I can move the point to the other window by entering C-x o or ESC-: (other-window 1). But I would like to do this automatically, for instance at the command line or in the .emacs file.
I tried
emacs -no-splash -mm --eval `(other-window 1)` A.txt B.txt
but it did not work..
The following seems to work: Enter in .emacs :
(add-hook 'emacs-startup-hook '(lambda () (other-window 1)))
Put the following in your ~/.emacs file:
(add-hook 'emacs-startup-hook '(lambda () (select-first-buffer-in-list command-line-args)))
(defun select-first-buffer-in-list (list)
(let (buffer)
(while list
(if (setq buffer (get-file-buffer (car list)))
(progn (select-window (get-buffer-window buffer))
(setq list nil))
(setq list (cdr list))))))
It will check which of the command line parameters correspond to a buffer. It selects a window displaying the buffer of the first such parameter.
Independent of the order of the command line parameters, this approach finds the left-most, upper-most window and selects it. Put the following code in your .emacs file:
(add-hook 'emacs-startup-hook '(lambda () (select-upper-left-window)))
(defun select-upper-left-window ()
(let (window)
(while (setq window (window-in-direction 'above nil t))
(select-window window))
(while (setq window (window-in-direction 'left nil t))
(select-window window))))
This would keep a single window with "B.txt"
emacs --find-file A.txt B.txt -Q -eval "(delete-other-windows)"
Is there a reason you don't want to just reverse the order of the file names on the command line?
I use the following code in .emacs:
(require 'dired+)
(toggle-diredp-find-file-reuse-dir 1)
So it won't create a buffer for every dir I visit. Then I decided to add some ergonomics:
(add-hook 'dired-mode-hook
(lambda ()
(define-key dired-mode-map (kbd "C-<up>") 'dired-up-directory)))
So when I click Ctrl-<up> it will move to the parent directory. But it opens the parent dir in a new buffer.
How to make it open in the same buffer?
The solution can be found there:
(add-hook 'dired-mode-hook
(lambda ()
(define-key dired-mode-map (kbd "C-<up>")
(lambda () (interactive) (find-alternate-file "..")))
; was dired-up-directory
))