If I press C-u C-n the cursor goes down of 4 lines.
Can I make the default universal argument to be another number greater than 4?
There might be a better way to do this, but one possibility is to create your own universal argument prefix function. Here is the original function (as you can see 4 is hardcoded in the function):
(defun universal-argument ()
"Begin a numeric argument for the following command.
Digits or minus sign following \\[universal-argument] make up the numeric argument.
\\[universal-argument] following the digits or minus sign ends the argument.
\\[universal-argument] without digits or minus sign provides 4 as argument.
Repeating \\[universal-argument] without digits or minus sign
multiplies the argument by 4 each time.
For some commands, just \\[universal-argument] by itself serves as a flag
which is different in effect from any particular numeric argument.
These commands include \\[set-mark-command] and \\[start-kbd-macro]."
(interactive)
(setq prefix-arg (list 4))
(universal-argument--mode))
In your init file, you can create your own custom version of this, and bind it to C-u:
(defun my-universal-argument ()
(interactive)
(setq prefix-arg (list 10))
(universal-argument--mode))
(global-set-key (kbd "C-u") 'my-universal-argument)
However, see #Drew's comment below about why this might not be a great idea, and may have undesired/unexpected consequences.
Also, keep in mind that you can press C-u multiple times as a prefix argument to multiply the repetitions by 4. For example, using the original default of 4, C-u C-u C-n will move down 16 (4*4) lines, and so on.
Related
How can I find the number of universal-arguments used to invoke a command prior to a numeric-argument.
For example, how could I differentiate between arg1 and arg2 in the following (wrong) function?
(defun my-func (&optional arg1 arg2)
(interactive "P\nP")
(message "arg1: %S, arg2: %S, value: %S"
arg1 arg2 (prefix-numeric-value current-prefix-arg)))
Use case, transparently passing the numeric prefix value to an advised command depending on the number of prior universal-arguments, eg. dired rename following 2 files in current dired buffer with C-u 2 or to other dired buffer with C-u C-u 2.
Something along the lines of the following advice, but that actually passes the 2 properly to dired-do-rename instead of either 1 or 4.
(define-advice dired-do-rename (:around (fun &rest args) "defer-dwim")
(let ((dired-dwim-target (equal '(4) current-prefix-arg)))
(apply fun (list (max 1 (prefix-numeric-value current-prefix-arg))))))
I see there is a prefix-command-preserve-state-hook, but it seems like significant additional effort to make that work.
Sorry, but your question is quite unclear to me - so this might not answer it.
You say "How can I find the number of universal-arguments used to invoke a command prior to a numeric-argument." I'll answer that.
It doesn't matter how many times you write "P" in an interactive string spec. The value is the same each time, and multiple such occurrences do NOT correspond to multiple uses of C-u.
So (interactive "P\nP\nP") just produces three arguments that all have the same value - the value that comes from a single use of a prefix argument key sequence, whether that sequence is C-u, C-9, M--3, C-u 360, or C-u C-u C-u.
If you want your function to distinguish, say, C-u, C-u C-u, and C-u C-u C-u then it needs to test the raw prefix argument (what "P" specifies for interactive).
C-u corresponds to the raw prefix arg (4), that is, a list with just the number 4.
C-u C-u corresponds to the raw prefix arg (16).
C-u C-u C-u corresponds to the raw prefix arg (64).
(prefix-numeric-value '(4)) returns 4; (prefix-numeric-value '(16)) returns 16; and (prefix-numeric-value '(64)) returns 64.
See the Elisp manual, node Prefix Command Arguments.
You can practice with something like this command, to see what the resulting raw and numeric prefix arguments are when you type different prefix-arg key sequences:
(defun foo (arg narg)
(interactive "P\np")
(message "ARG: %S, NARG: %S" arg narg))
The rest of your question is completely unclear to me. I don't understand what you're trying to do with "dired-do-rename. If I'm right, please consider posting two separate questions: one asking how to distinguish C-u from C-u C-u and the other asking something (what?) about dired-do-rename.
Using emacs-24.
Some unicode names are quite long. Some characters have more than one name depending on the context. I would like to add some abbreviations/synonyms. How?
This approach is not so bad, but I have problems with shorter names that alias with longer ones, and it is non-standard, i.e. not consistent with the way other names are entered:
(global-set-key (kbd "C-x g all") "∀")
The approach of putting characters on keys has problems in Emacs, partly because the keymap is already overloaded:
(define-key key-translation-map (kbd "C-~") (kbd "¬"))
As a secondary question, I am curious as to why this confuses emacs (give it a try):
(global-set-key (kbd "C-x g neg") "¬")
What I would like is to hook the abbreviations into the current emacs method for entering unicode characters by name. (I've been using C-x 8 RET name RET - though wish there was a method to do this in fewer key strokes.)
You can easily define a command that inserts a given character (or that chooses from some small set of characters rather than from the entire universe of Unicode characters).
Library ucs-cmds.el can help with this. When you use C-x 8 RET with a negative prefix arg (e.g. C--), it not only inserts the char you choose but it creates a command to insert the char - the command name is the same as the char name. And you can quickly create such commands for whole ranges or other sets of characters (e.g. by matching a regexp). You can of course rename commands to whatever you like, including shorter versions.
But you already know how to bind a key to a keyboard macro that inserts a given character, as you have shown. If it helps to provide a named command for that then ucs-cmds.el can help.
You can also just do that yourself individually, using, for example:
(defun neg (&optional n)
"Insert \"¬\". With prefix arg N, insert N times."
(interactive "p")
(dotimes (ii n) (insert "¬")))
(global-set-key (kbd "C-x g neg") 'neg)
But you apparently are not very interested in dedicated commands that insert particular characters, and you want to be able to use C-x 8 RET but to type an abbreviation for a character name when it prompts you, instead of trying to match the real character name.
For that, Icicles can help. When you use C-x 8 RET you can match the character name or its code point (or the character itself - useful when the char is easy to type and you want to know its name or code point). You can match any combination of these at the same time.
Matching can be substring, regexp, pcompletion or any of several kinds of fuzzy matching, and you can change the matching behavior on the fly. So you can get the effect of the abbreviations you are asking for, provided you abbreviate in a way that corresponds to matching.
As for your question about (global-set-key (kbd "C-x g neg") "¬"): I think it is a bug. Consider reporting it: M-x report-emacs-bug. This is the error that it raises:
After 0 kbd macro iterations: user-error: No M-x tags-search or M-x tags-query-replace in progress
There are several modes around which provide simplified input for symbols needed by math and logic. For example agda2-mode. http://wiki.portal.chalmers.se/agda
OP:
What I would like is to hook the abbreviations into the current emacs method for entering unicode characters by name. (I've been using C-x 8 RET name RET - though wish there was a method to do this in fewer key strokes.)
What the OP is asking for is:
a) To use the emacs function 'insert-char' with its built-in shortcut 'C-x 8 RET', and
b) To use an alias for completion in the interactive minibuffer for 'insert-char' input.
The issue is that the minibuffer for 'insert-char' has its own TAB completion. If you want to insert the greek small letter epsilon (ε) using TAB completion, you have to input a minimum number of keystrokes like this: "greek" TAB "sm" TAB "l" TAB "ep". Even if you have an alias for epsilon in your 'init.el' configuration file like this: '("eps" "GREEK SMALL LETTER EPSILON")', the minibuffer will not automatically recognize it.
You can still use the alias for epsilon you have in your 'init.el' file using a second function 'expand-abbrev'. Using the method described in the OP, you can get an 'ε' by using "C-x 8 RET" (or "M-x insert-char"), entering your alias "eps", then call 'expand-abbrev' ("M-x expand abbrev") and return. This will expand your alias for the 'insert-char' function. (There is also a 'C-x' shortcut for 'M-x expand-abbrev'.)
Like the OP, I prefer this method over (or in addition to) automatic alias replacement. If you have something in your config file like this:
;; a quick way to insert unicode characters by code point or name
(global-set-key [f8] 'insert-char)
;; call 'expand-abbrev', especially in the 'insert-char' input minibuffer
(global-set-key [f9] 'expand-abbrev)
;; abbreviate unicode names
(define-abbrev-table 'global-abbrev-table '(
("ueps" "GREEK SMALL LETTER EPSILON")
("Ueps" "ε")
("ugsl" "GREEK SMALL LETTER ")
("uforall" "FOR ALL")
("Uforall" "∀")
))
;; see .emacs.d/abbrev_defs
;; M-x edit-abbrevs
;; turn on abbrev mode by default
(setq-default abbrev-mode t)
, you have two ways to type an epsilon. You can let emacs replace the alias "Ueps" automatically, or you can use seven keystrokes "[f8]ueps[f9]RET". (Actually there are four ways here.)
As the OP suggests, it is somewhat impractical to have aliases (or key-bindings) for every single special character. That is why it makes sense to use 'insert-char' with 'expand-abbrev'. If you want to insert a less commonly used greek letter like omicron 'ο', for instance, you do not need a special alias; you can expand an alias like 'ugsl' to "GREEK SMALL LETTER ", and enter "omicron" (or "omi" + TAB).
This question already has answers here:
How to write a key bindings in emacs for easy repeat?
(5 answers)
Closed 8 years ago.
I'm still very new to EMACS, but are getting familiar when i'm going through the emacs and elisp manual. But right now i'm stuck on this:
Is there a simple way to bind input sequences in regexp style?
eg: the default binding for function enlarge-window-horizontally is "C-x {", is it possible to rebind it to something like "C-x ({)+" so that enlarge-window-horizontally can be called repeatedly by repeating "{" character, instead of release Ctrl key multiple times?
There is another way to archive what you desire:
The first time you want to repeat the last command, press C-x z, afterwards you may repeat your command as often as desired by just pressing z.
The advantage of this approach is that it works with every command you use and not just for a specific one.
For additional reference here is the output of C-h f
8.11 Repeating a Command
Many simple commands, such as those invoked with a single key or with
M-x COMMAND-NAME , can be repeated by invoking them with a
numeric argument that serves as a repeat count (*note Arguments::).
However, if the command you want to repeat prompts for input, or uses
a numeric argument in another way, that method won't work.
The command C-x z (`repeat') provides another way to repeat an
Emacs command many times. This command repeats the previous Emacs
command, whatever that was. Repeating a command uses the same
arguments that were used before; it does not read new arguments each
time.
To repeat the command more than once, type additional z's: each
z repeats the command one more time. Repetition ends when you type
a character other than z, or press a mouse button.
For example, suppose you type C-u 2 0 C-d to delete 20
characters. You can repeat that command (including its argument) three
additional times, to delete a total of 80 characters, by typing C-x z
z z. The first C-x z repeats the command once, and each subsequent
z repeats it once again.
The "Emacs way" is to use C-u as a prefix key. E.g. C-u20C-x{.
Having said that, it's possible to do what you ask for. However, it would require you to bind C-x { and { separately. The former would be defined like it is today, but the latter would have to look something like:
(defun my-open-brace ()
(interactive)
(if (eq last-command 'shrink-window-horizontally)
(progn
(setq this-command 'shrink-window-horizontally)
(call-interactively 'shrink-window-horizontally))
(call-interactively 'self-insert-command)))
Unfortunately, if you have many sequences ending in {, you would have to write one function to handle them all.
You can also define your own repeatable command and bind it to C-x {. You can then use it exactly as you requested: C-x { { { {..., instead of having to use C-x { C-x z z z z...
Here is what you do:
(defun your-repeat-command (command)
"Repeat COMMAND."
(let ((repeat-message-function 'ignore))
(setq last-repeatable-command command)
(repeat nil)))
(defun your-shrink-window-horizontally ()
"Shrink window horizontally.
You can repeat this by hitting the last key again..."
(interactive)
(require 'repeat nil t)
(my-repeat-command 'shrink-window-horizontally))
(define-key ctl-x-map "{" 'your-shrink-window-horizontally)
You can do this with any command you like --- use my-repeat-command to make a repeatable version of it. I do this all the time, in several of my libraries.
Write a multi repeat command for emacs by using minor mode. I name it smart-repeat-mode
https://github.com/zhsfei/emacs-ext
I have a single line text file of csv values
I would like to able to 'pretty-print' the file to span multiple lines to make it more readable
The 1st no. represents the no. of csv values in the next section and so on
e.g.
3,1,2,3,3,4,5,6
would be converted to:
3,1,2,3
3,4,5,6
I know a little about making macros, e.g.
C-x (
C-s RET ,
C-x )
using this I can do:
C-u 3 C-x e to move 3 csv values along
My sticking point is how to use the value from file to paste into the arg to C-u
maybe I should be using an e-lisp function instead as its a function I would like to 'save' for continual use across emacs sessions. Is it possible to save macros as such?
any ideas gratefully received
I find elisp easier to think about than keyboard macros. How about this:
(defun csv-line-breaks ()
(interactive)
(while (search-forward "," nil t
(1+ (string-to-number (thing-at-point 'word))))
(delete-char -1)
(insert "\n")))
(global-set-key (kbd "C-c b") 'csv-line-breaks)
With this in your .emacs (or just evaluate the code in your scratch buffer), you put point at the beginning of the line, then hit C-c b to break the line up into the chunks you want.
What this does:
Looping over the buffer until it runs out of values, and for each loop:
Read the first value. (thing-at-point 'word) grabs anything it finds between whitespace of punctuation (more or less).
Convert the value, which is actually a string, into a number
Add one to that number, and move forward that many commas
Delete the previous comma
Insert a new line
You might want to take a look at csv-mode for Emacs: http://emacswiki.org/emacs/CsvMode
Although it might not do exactly what you're looking for, it has a feature for formatting for readability, as well as other features for munging csv files in a variety of ways.
Imagine I've got the following in a text file opened under Emacs:
some 34
word 30
another 38
thing 59
to 39
say 10
here 47
and I want to turn into this, adding 1 to every number made of 2 digits:
some 35
word 31
another 39
thing 60
to 40
say 11
here 48
(this is a short example, my actual need is on a much bigger list, not my call)
How can I do this from Emacs?
I don't mind calling some external Perl/sed/whatever magic as long as the call is made directly from Emacs and operates only on the marked region I want.
How would you automate this from Emacs?
I think the answer I'm thinking of consist in calling shell-command-on-region and replace the region by the output... But I'm not sure as to how to concretely do this.
This can be solved by using the command query-replace-regexp (bound to C-M-%):
C-M-%
\b[0-9][0-9]\b
return
\,(1+ \#&)
The expression that follows \, would be evaluated as a Lisp expression, the result of which used as the replacement string. In the Lisp expression, \#& would be replaced by the matched string, interpreted as a number.
By default, this works on the whole document, starting from the cursor. To have this work on the region, there are several posibilities:
If transient-mark-mode is turned on, you just need to select the region normally (using point and mark);
If for some reason you don't like transient-mark-mode, you may use narrow-to-region to restrict the changes to a specific region: select a region using point and mark, C-x n n to narrow, perform query-replace-regexp as described above, and finally C-x n w to widen. (Thanks to Justin Smith for this hint.)
Use the mouse to select the region.
See section Regexp Replacement of the Emacs Manual for more details.
Emacs' column editing mode is what you need.
Activate it typing M-x cua-mode.
Go to the beginning of the rectangle (leave cursor on character 3) and press C-RET.
Go to the end of the rectangle (leave cursor on character 7). You will be operating on the highlighted region.
Now press M-i which increments all values in the region.
You're done.! remove dead ImageShack links
It doesn't protect against 99->100.
(defun add-1-to-2-digits (b e)
"add 1 to every 2 digit number in the region"
(interactive "r")
(goto-char b)
(while (re-search-forward "\\b[0-9][0-9]\\b" e t)
(replace-match (number-to-string (+ 1 (string-to-int (match-string 0)))))))
Oh, and it operates on the region. If you want the entire file, then you replace b and e with (point-min) and nil.
Moderately tested; use M-: and issue the following command:
(while (re-search-forward "\\<[0-9][0-9]\\>" nil t) (let ((x (match-string 0))) (delete-backward-char 2) (insert (format "%d" (1+ (string-to-int x))))))
I managed to get it working in a different way using the following (my awk-fu ain't strong so it probably can be done in a simpler way):
C-u M-x shell-command-on-region RET awk '$2>=0&&$2<=99 {$2++} {print}' RET
but I lost my indentation in the process : )
Seeing all these answers, I can't help but have a lot of respect for Emacs...