One of my basic usage for C-u under emacs is to quickly generate separator comments.
For instance C-u C-u C-u = generates 64 chars =
================================================================
C-u C-u / generates 16 chars / and so on....
////////////////
My problem is that it does not work with the minus sign -.
When typing C-u C-u C-u - Emacs waits for some digits (to form a negative number) and does not print the line of 64 - char.
----------------------------------------------------------------
My question: is there a workaround, some kind of trick to 'escape' the minus sign when using the C-u command?
You can use quoted-insert to prevent the - being captured:
C-uC-uC-uC-q-
Or enter the number manually:
C-u64-
M-6M-4-
Related
I'm in Emacs' *scratch* and I try
(describe-key "C-x C-f")
and I get back
"C - x SPC C - f is undefined"
Obviously, it doesn't recognize my C-x C-f shorthand. What does it expect? The documentation
Display documentation of the function invoked by KEY. KEY can be any
kind of a key sequence; it can include keyboard events, mouse events,
and/or menu events. When calling from a program, pass KEY as a string
or a vector.
doesn't elaborate on how that string or vector should be entered. What do I need to give it to work?
You need to specify it as (describe-key "\C-x\C-f") or (describe-key (kbd "C-x C-f")).
if you call C-h f kbd, you'll get:
(kbd KEYS)
Convert KEYS to the internal Emacs key representation. KEYS should be a string in the format returned by commands such as ‘C-h k’ (‘describe-key’).
This is the same format used for saving keyboard macros (see ‘edmacro-mode’).
You need to specify the keys exactly. "C-x C-f" is a string consisting of the capital letter C, a dash, a lower case x, a space etc.: it's a description of what you press.
What you really press is [24 6] a vector of two chars (chars are represented as integers internally and 24 and 6 are the integers representing Control-x and Control-f resp). Another way to describe the vector of these two chars is as a string consisting of two chars: "\C-x\C-f" is one way, as Alex Ott points out in his answer; equivalently "\x18\x6" uses the hexadecimal values of 24 and 6 resp; or "\030\006" uses the octal values.
But we'd rather use the description "C-x C-f", so (as Alex Ott points out in his answer again) kbd translates from the description to the actual sequence of chars:
(kbd "C-x C-f") --> "^X^F"
which is actually two characters: Control-x and Control-f. The lisp printer uses the caret representation to indicate them, so it looks like four characters in between the quotes, but that is just how they appear: if you do the above in your *scratch* buffer, put the cursor on the left quote and press right arrow or C-f on them, you'll see that there are only two characters in between the quotes.
All of this and more is explained in the GNU Emacs Lisp manual, in the Character Type section and its subsections.
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.
In older Emacs versions C-x 8 " a used to insert ä (LATIN CAPITAL LETTER A WITH DIAERESIS). Now, there is only C-x 8 RET to insert a Unicode character.
C-x 8 C-h tells me that there is only this single binding to ucs-insert with prefix C-x 8.
Is there a mode to get the olde combinations, too?
I am running 23.3.1 and have no choice to upgrade.
To locate the problem, I first C-x r t ;xyx;ed about half of the .emacs.el. It soon boiled down to an old library that happened to have:
(setq key-translation-map)
whose value was originally
(24 keymap
(56 . iso-transl-ctl-x-8-map)))
24 is also known as ^X and 56 as ?8.
What I still do not understand about this is this: After C-x C-eing (setq key-tranlation-map) all the C-x 8 combinations are undefined except C-x 8 RET. (Warning: don't try it with your working, loaded Emacs, take a fresh -qed one).
I love the Emacs keyboard-macro functionality and I am using it a lot.
Sometimes, I don't want to just statically enter certain keyboard macros, but there should be a value there that will get changed in between. There is the feature of Emacs Macro counters (Macro counters in Emacs Manual).
The problem is that this counter always just counts up by one. Is there a way to specify the stepping size (i.e. move forward by 4 in each step)?
Thanks in advance for your help!
You can use kmacro-add-counter, bound to C-x C-k C-a.
For example to add 3 to the counter, use M-3 C-x C-k C-a.
Small full example: <f3> <f3> RET M-3 C-x C-k C-a <f4> <f4> <f4> <f4> will produce:
0
4
8
12
Alternative to kmacro
Sometimes, you can use tiny to do
what kmacro does in fewer keystrokes and with better undo context.
The above example can generated by entering:
m\n3*x4
and pressing the shortcut for tiny-expand. I bind it like this:
(global-set-key (kbd "C-;") 'tiny-expand)
Here, m\n3 basically means 4 repetitions (index starts from 0) joined by the newline character (\n). And *x4 is a shorthand for Elisp (* x 4).
I am trying to replace a character - say ; - with a new line using replace-string and/or replace-regexp in Emacs.
I have tried the following commands:
M-x replace-string RET ; RET \n
This will replace ; with two characters: \n.
M-x replace-regex RET ; RET \n
This results in the following error (shown in the minibuffer):
Invalid use of `' in replacement text.
What's wrong with using replace-string for this task? Is there another way to do it?
M-x replace-string RET ; RET C-q C-j.
C-q for quoted-insert,
C-j is a newline.
There are four ways I've found to put a newline into the minibuffer.
C-o
C-q C-j
C-q 12 (12 is the octal value of newline)
C-x o to the main window, kill a newline with C-k, then C-x o back to the minibuffer, yank it with C-y
Don't forget that you can always cut and paste into the minibuffer.
So you can just copy a newline character (or any string) from your buffer, then yank it when prompted for the replacement text.
More explicitly:
To replace the semicolon character (;) with a newline, follow these exact steps.
locate the cursor at the upper left of buffer the containing text you want to change
Type m-x replace-string and hit Return
The mini-buffer will display something like this:
Replace string (default ^ -> ):
Type in the character you want to replace. In this case, ;
and hit Return
The mini-buffer will display something like this:
string ; with:
Now execute C-q C-j
All instances of semicolon will be replaced a newline (from the cursor location to the end of the buffer will now appear)
There is a bit more to it than the original explanation says.
Switch to text mode:
M-x text-mode
Highlight the block to indent.
Indent: Ctrl + M </kbd>
Switch back to whatever mode...
Inline just:
C-M-S-% (if the binding keys are still the default) and then
replace-string ^J.