I am a new user of emacs.
I found some useful options that allows me to insert new line before or after cursor: C-j (before cursor), C-o (after cursor).
I found this very convenient in doing formatting text across lines.
Now, are there methods to insert space after cursor for in-line formatting?
Currently I have to insert space before cursor using Space then C-b multiple times just to return to the original position when doing formatting within one line.
I don't think there is such a function, but it is easy to write:
(defun my-insert-space-after-point ()
(interactive)
(save-excursion (insert " ")))
(global-set-key (kbd "C-.") 'my-insert-space-after-point)
This binds the function to C-.; adjust to preference.
Another way to do this is to record a macro, save it, and bind it to a key. The steps to do that are described in this answer.
Related
I works in programming mode such as javascript-mode and usually need to push some lines down for formatting purpose.
I find C-o convenient to insert a newline after cursor point. However the line pushed down loses its indentation.
I find RET convenient to insert newline. And the line pushed down is nicely indented. However, the inserted new-line comes after the current cursor point. (EDIT: ) I find it is convenient to keep the cursor position by inserting the newline after it, because sometimes I still need to modify the current line.
Probably something like this:
(defun open-line-and-indent ()
"Like `newline-and-indent', but do not move the point."
(interactive)
(save-excursion
(newline-and-indent)))
(global-set-key (kbd "C-o") #'open-line-and-indent)
;; (define-key javascript-mode-map (kbd "C-o") #'open-line-and-indent)
I want to modify different parts of my file with the same string. So I installed multiple-cursors in emacs. But unfortunately, I cannot do the (simple?) thing of marking different parts of the text and start to edit.
Each command I have checked seems not useful to do what I want. For instance consider I need to edit the beginning of line 10 and a character in the middle of line 34. How do I do that?
It sounds like you are looking for the command mc/add-cursor-on-click, which I personally have bound to C-S-<mouse-1>. With this setup, I can hold Ctrl and Shift and click the beginning of line 10, and then click the middle of line 34, and I will have a cursor in both positions.
You can bind this command the way I have like this in your init:
(global-set-key (kbd "C-S-<mouse-1>") 'mc/add-cursor-on-click)
You can replace the kbd with another combination too if you want.
The following is derived from https://github.com/magnars/multiple-cursors.el/issues/44 and seems like a reasonable keyboard-driven solution.
(require 'multiple-cursors)
(defun mc/toggle-cursor-at-point ()
"Add or remove a cursor at point."
(interactive)
(if multiple-cursors-mode
(message "Cannot toggle cursor at point while `multiple-cursors-mode' is active.")
(let ((existing (mc/fake-cursor-at-point)))
(if existing
(mc/remove-fake-cursor existing)
(mc/create-fake-cursor-at-point)))))
(add-to-list 'mc/cmds-to-run-once 'mc/toggle-cursor-at-point)
(add-to-list 'mc/cmds-to-run-once 'multiple-cursors-mode)
(global-set-key (kbd "C-S-SPC") 'mc/toggle-cursor-at-point)
(global-set-key (kbd "<C-S-return>") 'multiple-cursors-mode)
Firstly use mc/toggle-cursor-at-point to mark each position where you want a cursor, and then invoke multiple-cursors-mode to activate them.
I want the following behavior:
Append the word under cursor into a file(~/vocabulary.txt, for example)
Better still to bind a key for it.
Could anyone show me how to do it?
Should I put those code into .emacs ?
Try the following function:
(defun my-write-to-file ()
"Save word at point to file"
(interactive)
(write-region (concat (thing-at-point 'word) "\n") nil "~/vocabulary.txt" 'append))
When called, this function will save the word at point (the word the cursor is on or the word right before the cursor) to ~/vocabulary.txt.
You can bind it to a key (C-c w in this case, but you can change it to whatever you like) like this:
(global-set-key (kbd "C-c w") 'my-write-to-file)
To use, simply put the function and the keybinding assignment in your .emacs.
#Elethan wrote you a command that does just what you ask for, and bound it to a key.
It might also help to mention some general commands that you can use for this kind of thing. M-x append-to-file appends the region contents to a file, and M-x write-region prepends.
The manual is your friend for things like this. See nodes Misc File Ops and Accumulating Text.
Be aware too that for the two commands just mentioned, as the manual says about append-to-file (it should say it about both):
You should use append-to-file only with files that are not being
visited in Emacs. Using it on a file that you are editing in Emacs
would change the file behind Emacs’s back, which can lead to losing some
of your editing.
Accumulating Text also tells you about commands for adding text to a buffer, including the case of adding to a buffer for a file that you are visiting (as opposed to what the above quote warns you about for append-to-file). These include commands append-to-buffer and prepend-to-buffer.
In vim, you can do this by vi", vi[, vi( ...
For example, if you have a line like this:
x = "difference between vim and emacs"
and the cursor is anywhere between those quotes and you hit vi", then the string will be visually
selected.
The package expand-region is convenient for this. Calling er/expand-region with the point inside the quotes will mark the nearest word, and then calling it again will mark all the words inside the quotes. (Calling it a third time will expand the region to include the quotes.)
I have it bound to C-;.
(global-set-key (kbd "C-;") 'er/expand-region)
With this binding, pressng C-; C-; will highlight the text between the quotes.
From Emacs Documentation
Nearly all modes support “(,)” as parentheses, and most also support
square brackets “[,]” and curly brackets “{,}”. However, you can make
any pair of characters a parenthesis-pair, by using the following
command:
(modify-syntax-entry ?^ "($")
(modify-syntax-entry ?$ ")^")
Also, take a look at this post How to mark the text between the parentheses in Emacs?. key combination given per this post
Try the key sequence C-M-u C-M-SPC (i.e., while holding the Control and Meta keys, press u and Space in sequence), which executes the commands backward-up-sexp and mark-sexp
Late to the party, but you can also use Evil mode, which does a bang-up job of Vim emulation, including the motion commands you mentioned.
(defun select-text-in-delimiters ()
"Select text between the nearest left and right delimiters."
(interactive)
(let (start end)
(skip-chars-backward "^<>([{\"'")
(setq start (point))
(skip-chars-forward "^<>)]}\"'")
(setq end (point))
(set-mark start)))
On top of the toolkit
https://launchpad.net/s-x-emacs-werkstatt/+download
the following keys/commands are delivered:
(global-set-key [(super \))] 'ar-parentized-atpt)
(global-set-key [(super \])] 'ar-bracketed-atpt)
(global-set-key [(super \})] 'ar-braced-atpt)
(global-set-key [(super \")] 'ar-doublequoted-atpt)
(global-set-key [(super \')] 'ar-singlequoted-atpt)
That way with a couple of more chars known as delimiters will constitute commands.
ar-delimited-atpt will return the string around point found by nearest delimiter.
A group of more powerful commands allows re-using keys like that
(global-set-key [(control c)(\")] 'ar-doublequote-or-copy-atpt)
(global-set-key [(control c)(\')] 'ar-singlequote-or-copy-atpt)
(global-set-key [(control c)(<)] 'ar-lesser-angle-or-copy-atpt)
(global-set-key [(control c)(>)] 'ar-greater-angle-or-copy-atpt)
Here a doctring given as example:
ar-doublequote-or-copy-atpt is an interactive Lisp function in
`thing-at-point-utils.el'.
It is bound to C-c ".
(ar-doublequote-or-copy-atpt &optional NO-DELIMITERS)
If region is highlighted, provide THING at point with doublequote(s),
otherwise copy doublequote(ed) at point.
With C-u, copy doublequote(ed) without delimiters.
With negative argument kill doublequote(ed) at point.
Use libraries thingatpt+.el and thing-cmds.el.
You will find there commands such as thing-region, which selects a thing at point as the region. Since string-contents and list-contents are defined as things (in thingatpt+.el), these select the string/list contents as the region:
(thing-region "string-contents") ; Select the contents of the string at point.
(thing-region "list-contents") ; Select the contents of the list at point.
Or interactively:
M-x thing-region RET string-contents RET
Other, related commands include:
C-M-U (aka C-M-S-u) -- mark-enclosing-list: Select enclosing list. Repeat to expand list levels. Works with any balanced-parenthesis expressions (e.g., vectors): whatever the current syntax table defines as having balanced-delimiter syntax.
mark-thing -- Select successive things of a given kind (repeating).
next-visible-thing -- Move to the next visible thing of a given kind (repeating).
I want to select the whole word connected with dot.
Is there any function in Emacs doing this.
Example:
123.456.[7]89
Cursor is on "7".
Apply "???function".
Region "123.456.789" is selected.
[123.456.789]
Is there "???function" in Emacs?
I use expand-region:
(require 'expand-region)
(global-set-key (kbd "C-=") 'er/expand-region)
so I press C-= once and it selects 789; I press it a second time and it selects 123.456.789. It works nice with strings, lines, statements from different languages.
Home: https://github.com/magnars/expand-region.el
Install it with ELPA (M-x list-packages).
ps: http://wikemacs.org/index.php/Elpa
Here's a solution:
(defun mark-whole-word ()
(interactive)
(let ((table (syntax-table)))
(modify-syntax-entry ?. "w" table)
(with-syntax-table table
(backward-word)
(set-mark (point))
(forward-word))))
The key here is modifying the syntax.
So if you replace ?. with ?- above, you can mark similarly
123-456-789.
#abo-abo's answer is good.
Just as another data point, you can also use command thing-region (from library thing-cmds.el -- Thing-At-Point Commands) for this. It prompts you for a type of THING to select. Just accept the default THING type, sexp, and you get what you requested in this case.