How to overwrite text by yank in Emacs? - emacs

I want to overwrite text by yank as following. Is there any way to do this?
kill-ring:
text-i-want-to-paste
Before:
abcdefghijklmnopqrstuvwxyz
^
corsor
After:
text-i-want-to-pasteuvwxyz
Thanks.

Turn on delete-selection-mode. Then select the text to replace. Then hit C-y. With delete-selection-mode enabled, you just type to replace selected text, as is usual outside Emacs. And C-y also replaces it.

You can also use defadvice. Then this will only work when overwrite-mode is on:
(defadvice yank (before yank-if-overwrite)
(if (bound-and-true-p overwrite-mode)
(delete-char (length (current-kill 0))))
)
(ad-activate 'yank)

Here:
(defun crazy-yank ()
(interactive)
(delete-char (length (current-kill 0)))
(yank))
(global-set-key (kbd "C-M-y") 'crazy-yank)

Related

How to highlight a particular line in emacs?

I need to highlight facility for emacs in order to mark some lines in file while working with it.
It should be smth like M-s h l but should work based on line number, not on a regexp. I want to highlight a current line, but the hl-line-mode is not suitable, as I need to highlight many lines, every time I press a specific key on each of them.
I just quickly wrote the following:
(defun find-overlays-specifying (prop pos)
(let ((overlays (overlays-at pos))
found)
(while overlays
(let ((overlay (car overlays)))
(if (overlay-get overlay prop)
(setq found (cons overlay found))))
(setq overlays (cdr overlays)))
found))
(defun highlight-or-dehighlight-line ()
(interactive)
(if (find-overlays-specifying
'line-highlight-overlay-marker
(line-beginning-position))
(remove-overlays (line-beginning-position) (+ 1 (line-end-position)))
(let ((overlay-highlight (make-overlay
(line-beginning-position)
(+ 1 (line-end-position)))))
(overlay-put overlay-highlight 'face '(:background "lightgreen"))
(overlay-put overlay-highlight 'line-highlight-overlay-marker t))))
(global-set-key [f8] 'highlight-or-dehighlight-line)
(Here find-overlays-specifying came from the manual page)
It will highlight current line, and when used again it will remove it.
Maybe the following could be useful as well: removing all your highlight from the buffer (could be dangerous, you might not want it if you highlight important things)
(defun remove-all-highlight ()
(interactive)
(remove-overlays (point-min) (point-max))
)
(global-set-key [f9] 'remove-all-highlight)
You can use bm.el. You can install bm.el from MELPA.
bm.el provides bm-toggle to highlight and unhighlight current line.
bm.el also provides bm-bookmark-regexp which highlights only matched lines.
And you can jump between highlighted lines by bm-previous and bm-next
Following is sample configuration of bm.el
(require 'bm)
(global-set-key (kbd "<f5>") 'bm-toggle)
(global-set-key (kbd "<f6>") 'bm-previous)
(global-set-key (kbd "<f7>") 'bm-next)
(global-set-key (kbd "<f8>") 'bm-bookmark-regexp)
Bookmark+ does what you are asking for. Use C-x p RET (by default) to set a bookmark at point. And you can configure the kind of highlighting you want for such bookmarks. This is similar to what bm.el offers (syohex's answer), but more flexible.

How to bind C-k to kill-region if region selected; else kill-line

In emacs, I want to bind C-k to kill-region if region selected; else kill-line as normal.
How to configure it?
Put this in your .emacs
(defun kill-line-or-region ()
"kill region if active only or kill line normally"
(interactive)
(if (region-active-p)
(call-interactively 'kill-region)
(call-interactively 'kill-line)))
(global-set-key (kbd "C-k") 'kill-line-or-region)
This sounds like a job for advice!
(defadvice kill-line (around kill-region-if-active activate)
(if (and (called-interactively-p) (region-active-p))
(kill-region (region-beginning) (region-end))
ad-do-it))
EDIT: Added called-interactively-p check.
This is not really a direct answer but rather some hints.
You could enable delete-selection-mode with (delete-selection-mode 1). Then if a region is marked, when you start typing some text the region will be deleted. But in your case you could simply use C-w to cut the current region, this will add the region to the kill-ring but kill-line will do it too.

Is it possible to auto-complete parentheses or quotation marks in emacs?

I've used XCode and Netbeans, and I've noticed that they have a feature to automatically complete quotation marks or parentheses (and I assume that other IDEs often do this also). I don't know what the name of this feature is, but is there any way to do this in Emacs?
For example, when I type
printf("
I would like it to automatically input
printf("")
placing the cursor in between the quotation marks.
Thank you.
The basic variant would be AutoPairs. The same effect but a little more sophisticated can also be achieved with YASnippet.
If you type M-(, that will insert both a ( and a ), and leave point in between; if you then type M-), that will move point across the closing ). I use this all the time.
There is also a mode called "paredit" (available from http://mumble.net/~campbell/emacs/paredit.el) which does this sort of thing for quotes as well, and probably other stuff.
Paredit-mode inserts matching closing elements by default, so the while typing you'll see something like printf() then printf("") and the cursor would be positioned inside quotes.
I'm using code from http://cmarcelo.wordpress.com/2008/04/26/a-little-emacs-experiment/ to do "electric pairs". As I descibe in my blog other modes have problems with Python's triple quoted strings. (A Python peculiarity)
My 5 cents here as well.
(setq skeleton-pair t)
(defvar skeletons-alist
'((?\( . ?\))
(?\" . ?\")
(?[ . ?])
(?{ . ?})
(?$ . ?$)))
(global-set-key (kbd "(") 'skeleton-pair-insert-maybe)
(global-set-key (kbd "[") 'skeleton-pair-insert-maybe)
(global-set-key (kbd "\"") 'skeleton-pair-insert-maybe)
(global-set-key (kbd "\'") 'skeleton-pair-insert-maybe)
Next advice will enable the backspace to deletes the pairs: a(|)b -> ab
(defadvice delete-backward-char (before delete-empty-pair activate)
(if (eq (cdr (assq (char-before) skeletons-alist)) (char-after))
(and (char-after) (delete-char 1))))
Next advice will make backward-kill-word (for me is M-backspace) to delete matching par even if it separated by other text; very handy.
(defadvice backward-kill-word (around delete-pair activate)
(if (eq (char-syntax (char-before)) ?\()
(progn
(backward-char 1)
(save-excursion
(forward-sexp 1)
(delete-char -1))
(forward-char 1)
(append-next-kill)
(kill-backward-chars 1))
ad-do-it))
I am trying to move now to paredit, though.
The autopair minor mode does exactly what you ask for.

Is it possible to evaluate entire buffer in Emacs?

Currently, in order to evaluate elist in Emacs, I need to position the cursor on the last parenthesis and emit C-x e.
Is it possible to evaluate the entire buffer as a single elisp program without a need to position cursor?
M-x eval-buffer
or Alt+x and then type 'eval-buffer' or just type part of it and tab to autocomplete.
I placed this in my .emacs!
It allows you to eval a region if there is one or the entire buffer.
I bound it to C-xE.
(defun eval-region-or-buffer ()
(interactive)
(let ((debug-on-error t))
(cond
(mark-active
(call-interactively 'eval-region)
(message "Region evaluated!")
(setq deactivate-mark t))
(t
(eval-buffer)
(message "Buffer evaluated!")))))
(add-hook 'emacs-lisp-mode-hook
(lambda ()
(local-set-key (kbd "C-x E") 'eval-region-or-buffer)))
C-c C-l to load the entire file at once.

.emacs global-set-key and calling interactive function with argument

In my .emacs i have the following function that transposes a line
(defun move-line (n)
"Move the current line up or down by N lines."
(interactive "p")
(let ((col (current-column))
start
end)
(beginning-of-line)
(setq start (point))
(end-of-line)
(forward-char)
(setq end (point))
(let ((line-text (delete-and-extract-region start end)))
(forward-line n)
(insert line-text)
;; restore point to original column in moved line
(forward-line -1)
(forward-char col))))
And I bind a key to it like this
(global-set-key (kbd "M-<down>") 'move-line)
;; this is the same as M-x global-set-key <return>
However, I want to bind M-up to move-line (-1) But I cant seem to be able to do it correctly:
;; M-- M-1 M-x global-set-key <return>
How do I define the above using global-set-key to call move-line -1?
Not minutes after asking the question I figured it out by copy+pasting code. However I have no clue how it works.
(global-set-key (kbd "M-<up>") (lambda () (interactive) (move-line -1)))
global-set-key only takes 2 arguments: the key sequence and the command you want to bind to it. So
(global-set-key (kbd "M-<down>") 'move-line)
works fine. But if you want to use move-line with an argument you need to wrap it in an anonymous (aka lamba) function so that it presents itself to global-set-key as one value.
You can simply ask for the number of lines you want and convert the input string into an integer:
(global-set-key (kbd "M-<up>")
(lambda ()
(interactive)
(move-line (string-to-int (read-string "Lines: ")))))
I found this when I had the same problem, but I ended up solving it in another way.
(global-set-key (kbd "M-<down>") 'move-line)
(global-set-key (kbd "M-<up>") (kbd "C-u -1 M-<down>"))
Definitely not a perfect solution, since M-<down> could be reassigned and C-u -1 might not make sense on it, but since it's just my local init file, it should be no problem.
Also this obvious only works well for keyboard commands that you want to have reversed.
You might want to check out the "transpose-lines" built-in function.