Reputation: 40748
Suppose I have I file A.txt
, which I often edit and also, into which I often insert the text "Test". It would then be nice if I could just press e.g. the F5 key to insert the text "Test" into the buffer. However, when I edit any other file I do not want the F5 key to be bound in this way.
It would then be nice if I could avoid messing up my .emacs
init file with special code. Hence, consider defining a new file start.el
located in the same directory as A.txt
. The contents of start.el
could be:
(defun insert-test-text ()
(interactive)
(insert "Test"))
(global-set-key '[f5] 'insert-test-text)
Then I write a bash
script editA
for editing the file A.txt
in Emacs, e.g.
#! /bin/bash
emacs --run-code-on-init start.el A.txt &
The command line option run-code-on-init
is unfortunately not a valid option.. Is there any good solution to this problem?
Upvotes: 2
Views: 341
Reputation: 14065
I think you could get the effect you want by doing
emacs -l start.el a.txt
out of the box. According to the man page, that should start up emacs
editing a.txt
, with the lisp
code in start.el
loaded.
Does that work, or is there a reason the simple solution doesn't work for your use case?
Upvotes: 2
Reputation: 20342
Here's my view of the solution:
(defun insert-test-text ()
(interactive)
(insert "test"))
(add-hook
'text-mode-hook
(lambda ()
(if (string= (file-name-nondirectory (buffer-file-name))
"A.txt")
(local-set-key (kbd "<f5>") 'insert-test-text))))
This will define the shortcut only for files named A.txt
.
You should tweak the code to your needs, of course.
Also, don't forget to bookmark the file if you edit it a lot. I've got a very quick setup (around two key chords) for ~20 files/directories. that I edit a lot. The setup is described here.
If you want the binding for single file, just drop file-name-nondirectory
like so:
(add-hook
'text-mode-hook
(lambda ()
(if (string= (buffer-file-name)
"~/A.txt")
(local-set-key (kbd "<f5>") 'insert-test-text))))
And here's what I do to keep my config manageable - in ~/.emacs
:
(defvar dropbox.d "~/Dropbox/")
(defvar emacs.d (concat dropbox.d "source/site-lisp/"))
(add-to-list 'load-path emacs.d)
(defun add-subdirs-to-load-path (dir)
(let ((default-directory dir))
(normal-top-level-add-subdirs-to-load-path)))
(add-subdirs-to-load-path emacs.d)
(load "init")
;; you can put the code in "source/site-lisp/feature1.el"
(load "feature1")
That's about the whole content of my ~/.emacs
- the rest of the config is
it the respective files in site-lisp
dir.
You can comment out the (load "feature1")
to turn it off temporarily.
Upvotes: 2