I've got in .emacs file:
(defun poc ()
(interactive)
(insert (char-from-name "DOUBLE LOW-9 QUOTATION MARK"))
(global-set-key [f12] 'poc))
(defun konc ()
(interactive)
(insert (char-from-name "LEFT DOUBLE QUOTATION MARK"))
(global-set-key [(shift f12)] 'konc))
Entering F12 key I get
<f12> is undefined
If I enter M-x poc, DOUBLE LOW-9 QUOTATION MARK is inserted. But from this moment on if I enter F12 key again, the quotation mark is correctly inserted.
And this repeats after restarting emacs.
Is something wrong with this code?
emacs-version is 26.1
You're binding the keys in the functions, so you need to run the functions first. This is not recommended. Bind the keys outside the functions, like so:
(defun poc ()
(interactive)
(insert (char-from-name "DOUBLE LOW-9 QUOTATION MARK")))
(global-set-key (kbd "<f12>") #'poc)
Since these are so simple (just inserting characters), you actually don't even need functions.
(global-set-key (kbd "<f12>") (string (char-from-name "DOUBLE LOW-9 QUOTATION MARK")))
(global-set-key (kbd "S-<f12>") (string (char-from-name "LEFT DOUBLE QUOTATION MARK")))
or if you use an encoding like utf-8 (which you should), you can put the characters directly in a string.
(global-set-key (kbd "<f12>") "„")
(global-set-key (kbd "S-<f12>") "“")
Related
I want to type a single keystroke to insert unicode brackets #xab and #xbb.
This works (for a different unicode bracket):
(global-set-key (kbd "C-c [") "⟨")
But this doesn't work:
(global-set-key (kbd "C-c [") "«")
How do I coax emacs into inserting "«" with a single keystroke?
(I happen to be on MacOS, but intend to use ubuntu in Parallels)
I`m not sure what is expected, double ⟨ or one «, but hope this can help
check function and loop
(defun my-insert ()
"test something"
(interactive)
(dotimes (i 2)
(insert "⟨"))
)
(defun my-insert2 ()
"test something"
(interactive)
(insert "«")
)
(global-set-key (kbd "C-c [") 'my-insert)
(global-set-key (kbd "C-c ]") 'my-insert2)
There is a more general solution which works everywhere, making it more convenient (as you may not want to open an emacs instance each time you need a particular character), which is the compose key. Compose key is specific to Linux, but there is a way to install an equivalent solution on MacOS, and you can even convert existing Compose Key bindings, which is useful because there are a lot of predefined sequences out there in the wild, for instance the default ones.
E.g. on Ubuntu you probably just need to enable it in the settings, and then with the default configuration typing Compose < < will produce «.
I'm completely new to both Lisp and Emacs. In Emacs, when coding in Java for example, I want to be able to type "{" then hit "ENTER" and have the next line be ready for whatever is nested in the braces. For example, if I have the following line:
public void method()
and I type "{" then hit return I should get this:
public void method() {
// indentation applied, no additional tabbing necessary
}
I'm already able to insert by pairs, for example, typing "{" gives "{}" with my cursor between the braces. I did this by adding these lines to the emacs init file:
;; insert by pairs (parens, quotes, brackets, braces)
(defun insert-pair (leftChar rightChar)
(if (region-active-p)
(let (
(p1 (region-beginning))
(p2 (region-end))
)
(goto-char p2)
(insert rightChar)
(goto-char p1)
(insert leftChar)
(goto-char (+ p2 2))
)
(progn
(insert leftChar rightChar)
(backward-char 1) ) )
)
(defun insert-pair-brace () (interactive) (insert-pair "{" "}") )
(global-set-key (kbd "{") 'insert-pair-brace)
To get the auto-nesting I described above, I added these lines:
;; automatically nest next line
(defun auto-nest ()
(insert "\n\n")
(backward-char 1)
(insert "\t")
)
(defun auto-nest-brace () (interactive) (auto-nest) )
(global-set-key (kbd "{ RET") 'auto-nest-brace)
When I start up Emacs, however, I get this message:
error: Key sequence { RET starts with non-prefix key {
What am I doing wrong, and what can I do to fix it? I don't want to use a different key combination to do this. There are a lot of text editors in which this auto-nesting is standard, and it should be easy enough to code up in ELisp.
It's great that you are trying to add this functionality to Emacs yourself, but there's no need to reinvent the wheel here. Emacs already has a command for the purpose of auto-indenting; it's called newline-and-indent. It is bound to C-j by default, but you can rebind it to RET
globally:
(global-set-key (kbd "RET") 'newline-and-indent)
for a specific mode only:
(require 'cc-mode)
(define-key java-mode-map (kbd "RET") 'newline-and-indent)
java-mode-map is defined in cc-mode.el and not available by default, that's why you have to require cc-mode before you can modify java-mode-map.
Note that newline-and-indent indents according to major mode. That is, if you're e.g. in java-mode and press RET in some random location that's not meaningful w/r/t Java syntax, it won't insert additional whitespace at the beginning of the new line.
To read all there is to know about newline-and-indent do
C-h f newline-and-indent RET
I have something similar in my emacs config which I have been using for a while. It calls 'newline-and-indent twice then moves the point one line up before indenting correctly. Here is the snippet of code to do this from my config file:
;; auto indent on opening brace
(require 'cc-mode)
(defun av/auto-indent-method ()
"Automatically indent a method by adding two newlines.
Puts point in the middle line as well as indent it by correct amount."
(interactive)
(newline-and-indent)
(newline-and-indent)
(forward-line -1)
(c-indent-line-or-region))
(defun av/auto-indent-method-maybe ()
"Check if point is at a closing brace then auto indent."
(interactive)
(let ((char-at-point (char-after (point))))
(if (char-equal ?} char-at-point)
(av/auto-indent-method)
(newline-and-indent))))
(define-key java-mode-map (kbd "RET") 'av/auto-indent-method-maybe)
Pretty straightforward as you can see. Hopefully it will work for you. I have not used it in any other modes except java.
You want a combination of auto pairs (or alternatives) plus auto indentation. Check out the emacswiki on the former: http://www.emacswiki.org/emacs/AutoPairs
And on the latter:
http://www.emacswiki.org/emacs/AutoIndentation
Suppose that I have defined a macro in emacs (24.2), say xyz.
I would like to associate the shortcut $ to this macro, i.e. to run the macro xyz whenever I type $.
How I can do it?
I tried all of the following without success:
(global-set-key [$] 'xyz)
(global-set-key ["$"] 'xyz)
(global-set-key [?$] 'xyz)
(global-set-key [s-4] 'xyz)
(global-set-key "$" 'xyz)
(global-set-key (kbd "$") 'xyz)
(The last three ones were suggested by bleeding-fingers, abo-abo and Chris).
From your comments, it is now clear that you've defined a macro that includes using the key $. If you do this, you can't then bind the macro to the $, because that makes it recursive - every time you get to the $ in your macro, you are in effect calling the macro again.
You could, however, define the actions you want performed as an elisp function, which you could then bind to $. If we knew what you were actually doing with your macro we might be able to show you how.
EDIT: how about this:
(global-set-key (kbd "$") #'(lambda () (interactive) (insert " $")))
That should work, but lambdas can be a bit confusing. A little clearer for elisp beginners might be:
(defun my-dollars ()
"Insert a dollar sign with a space in front."
(interactive)
(insert " $"))
(global-set-key (kbd "$") 'my-dollars)
I'd like to have M-u to insert an underscore when I am in isearch (isearch-regexp and also the reverse variants).
Neither
(define-key isearch-mode-map (kbd "M-u") 'insert-underscore)
nor
(add-hook 'isearch-mode-hook
(lambda ()
(local-set-key (kbd "M-u") 'insert-underscore)
))
insert-underscore is my function that simply inserts "_". It works in the main frame and also in minibuffer, but I can't get it working in isearch...
Thank you!
Isearch doesn't use regular commands. (kbd "_") along with every other
printable character is bound to a special command in isearch-mode-map. It's
not obvious, but a lot of things happen in "isearch-mode" when you press a
key. Display is refreshed with new results, wrapping is a possibility, etc, etc,
You'd have to manipulate raw keyboard events to get this to work.
(defun underscore ()
(interactive)
(isearch-unread-key-sequence (list ?_)))
(define-key isearch-mode-map (kbd "M-u") 'underscore)
Note that this code is not robust; for example, numeric prefix does not work.
EDIT: After letting percolate in my mind for a while, it occured to me that this is the exact use-case for translation keymaps
(define-key key-translation-map (kbd "M-u") (kbd "_"))
Ain't Emacs grand?
I am trying to make a keyboard macro that prints a LaTeX macro and places the cursor inside it.
For example, I have the following placed in my .emacs file:
(global-set-key (kbd "C-c v") "\\bibleverse{}()")
I would like to set the cursor inside the curly brackets
(global-set-key (kbd "C-c v") "\\bibleverse{<cursor position>}()")
How would I do this? Is there a macro for cursor position in emacs lisp?
(defun latex-bibleverse-snippet ()
(interactive)
(insert "\\bibleverse{}()")
(backward-char 3))
(global-set-key (kbd "C-c v") 'latex-bibleverse-snippet)
Maybe a quick and dirty answer. Or you can take a look at YASnippet, or Predictive mode(provide IntelliSense features for some major modes (currently: LaTeX, Texinfo, HTML). ) :-)