Reputation: 4166
there's a lot of noise about super awesome features of Emacs. Be it org-mode, Gnus or C-x M-c butterfly. I've used Emacs for years, so no need to convince me.
What I personally found is that nobody talks about...actually editing text. You know that
Emacs is a nice OS but it lacks an decent editor.
joke? I didn't really get it until I grokked Vim.
Now, I wrote my thesis using Emacs as my text editor, so my heart is somehow still attached to it. I'm also super envious about org-mode. So I wondered whether there maybe actually are features I've been missing all the time?
I'm going to give you a few examples I use all the time:
o
/O
opens a new line above/below the current and puts me into insert mode no matter at which position my cursor is.ci'
deletes the contents of current/following '-string and puts me into insert mode.dat
deletes the current HTML tag. cit
would delete the contents and put me into insert mode. cstt
would let me edit the tag (eg make a <span>
into a <div>
)cs([
changes the surrounding "(" and ")" into "[" and "]".dai
would delete the current indented block, \\ai
would comment it out (\\ is an extension).Are there some super secret shortcuts but easily accessible shortcuts for stuff like this in Emacs? Sublime Text 2 has a few "expand selection" functions that go a similar way albeit not that precise--but still it's very useful.
P.S. Please don't bring up any vi emulation, just don't.
P.P.S. Also for god's sake, no Emacs vs. Vim vs. Whatever warfare, I beg you.
Upvotes: 21
Views: 6617
Reputation: 4804
You may have a look at
thing-at-point-utils.el
It provides stuff like that:
ar-brace-or-copy-atpt
"If region is highlighted, provide THING at point with brace(s), otherwise copy brace(ed) at point. With NO-DELIMITERS, copy brace(ed) without delimiters. With negative argument kill brace(ed) at point. "
ar-bracket-or-copy-atpt
"If region is highlighted, provide THING at point with bracket(s), otherwise copy bracket(ed) at point. With NO-DELIMITERS, copy bracket(ed) without delimiters. With negative argument kill bracket(ed) at point. "
ar-lesser-angle-or-copy-atpt
"If region is highlighted, provide THING at point with lesser-angle(s), otherwise copy lesser-angle(ed) at point. With NO-DELIMITERS, copy lesser-angle(ed) without delimiters. With negative argument kill lesser-angle(ed) at point. "
Keys in use here:
(global-set-key [(control c)(<)] 'ar-lesser-angle-or-copy-atpt)
(global-set-key [(control c)(>)] 'ar-greater-angle-or-copy-atpt)
(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-parentize-or-copy-atpt)
(global-set-key [(control c)(\\)] 'ar-backslash-or-copy-atpt)
(global-set-key [(control c)(\])] 'ar-bracket-or-copy-atpt)
(global-set-key [(control c)(\})] 'ar-brace-or-copy-atpt)
(global-set-key [(control c)(_)] 'ar-underscore-or-copy-atpt)
(global-set-key [(control c)(super \})] 'ar-brace-or-copy-atpt)
(global-set-key [(control c)(\/)] 'ar-slash-or-copy-atpt)
Source:
https://launchpad.net/s-x-emacs-werkstatt
Upvotes: 2
Reputation: 2380
OK, this probably isn't super secret, but I use it constantly.
Its the occur command
<Show all lines in the current buffer containing a match for REGular EXPression>
For example, it's very useful for showing all the lines where a particular variable is used.
M-x occur <Enter> <type in your search target> then <Enter>
This'll open up a new buffer showing the matched lines.
You can move the cursor to each occurrence using the command:
next-error
For me, they aren't convenient at all unless you re-define keys to run the commands. So, I map control-o to the occur command, and control-n to the next-error command.
To do that (or your own key(s))
put this in your .emacs file:
(global-set-key "\C-o" 'occur)
(global-set-key "\C-n" 'next-error)
Upvotes: 0
Reputation: 147
o/O
command is a two key command in emacs. For o
, C-e <return>
(end of line, return to create new line). For O
, C-a C-o
(beginning of line, open line).ffap-copy-string-as-kill
(not bound to a key by default, but easily fixed) does something similar to your ci'
. In emacs, there is no concept of "insert mode".I don't change "(" ")" to "[" "]" enough to learn a key sequence for it. If I had to do it I would turn to query-replace-regexp
(C-M-%
) first.
I don't edit python or HTML much, but I'm sure there are clever tricks in appropriate modes for your needs.
Edited in response to comments.
More advanced editing features that I use frequently:
enum
in C and creating a switch statement out of it quickly. See http://www.emacswiki.org/emacs/KeyboardMacros and http://www.emacswiki.org/emacs/KeyboardMacrosTricks for details.string-insert-rectangle
and rectangle-delete
are good friends of mine.It is hard to know what you will find useful. Every user will have a different favorite feature. The feature that made me start using emacs was M-x compile
. Use M-g n
to jump to the file location of the next error or warning in the compilation output.
Upvotes: 5
Reputation: 4336
Since no one has said so directly, I will.
The reason emacs lacks a "decent editor" is because a large part of the point of emacs is to allow you to make the ideal editor for yourself. This is not a trivial undertaking, and there are many things that other people have written and enough people have found useful to be included in the standard distributions of it.
It's not meant to be used at the default settings, really. Really. It's not meant to be a piece of software that you poke and prod at and tweak a bit here and there so much as an editor that you can tear apart the behavior of to make into whatever you want it to be.
So, yes, yes there is either an existing equivalent of $feature
from $editor
, or assuming no one has written one yet you can be safe in the knowledge that you can implement it.
Upvotes: 13
Reputation: 925
I know you said don't mention any vi emulation modes, but if you haven't you should really give Evil a look since it really does give you basically the fully power of vi's text manipulation inside Emacs. The best of both worlds really - Emacs awesome extensibility with vi's powerful text manipulation.
Upvotes: 0
Reputation: 2335
Look for this wiki page. It have a lot and lot of tricks. Most of them I found(suddenly) for myself before reading, but this article worth reading.
Try C-h k
every C-<key> M-<key>
. Something like M-c, M-l, M-u
proves useful for me, M-z
for zap-to-char
. Platinum answer would be "learn to use i-search". C-w, C-M-y, M-r
are super-useful while in i-search mode.
I don't know any packages, which helps me for editing something, that surround my point. One day I wrote one for myself, but it proved useless. If I will mind write something again, first thing I'll do - think, what should I write. What about their behavior? Shold it be N separate commands for every type of brackets, or one, but smart? How should it work? Kill for keyring use? (Discussions are welcome).
For ~lisp, I use sexp editing commands.
There is something I wrote for myself.
(defun cm-fast-step-upward ()
"Step 3 lines up, recenteres the screen."
(interactive)
(forward-line -3)
(recenter))
(defun cm-fast-step-downward ()
"Step 3 lines down, recenteres the screen."
(interactive)
(forward-line 3)
(recenter))
(defun cm-find-any-bracket-forward()
"Search for \"_}_]_'_>_)_end forward"
(interactive)
(re-search-forward "[\"}'>)]\\|\\(end\\)\\|\\]"))
(defun cm-find-any-bracket-backward()
"Search for \"_{_[_'_<_(_begin forward"
(interactive)
(re-search-backward "[\"{'<(]\\|\\(begin\\)\\|\\["))
(defun cm-find-nonspace-backward()
"Search for non tab, space, newline symbol backward."
(interactive)
(if (and
(search-backward-regexp "[
]" (point-min) t)
(search-backward-regexp "[^
]" (point-min) t))
(forward-char)
(goto-char (point-min))))
(defun cm-find-nonspace-forward()
"Search for non tab, space, newline symbol backward."
(interactive)
(if (and
(search-forward-regexp "[
]" (point-max) t)
(search-forward-regexp "[^
]" (point-max) t))
(backward-char)
(goto-char (point-max))))
(defun cm-backward-delete-word(&optional arg)
"DELETE word before cursor. Warning, this word cannot be yanked."
(interactive "p")
(let ((beg (point)))
(backward-word arg)
(delete-region beg (point))) )
(defun cm-delete-word(&optional arg)
"DELETE word after cursor. Warning, this word cannot be yanked."
(interactive "p")
(let ((beg (point)))
(forward-word arg)
(delete-region beg (point))) )
(defun cm-delete-line(&optional arg)
"DELETE line after cursor. Warning, this line cannot be yanked."
(interactive "P")
(if (equal arg '-) (setq arg -1))
(let ((beg (point)))
(if arg (forward-line arg)
(goto-char (point-at-eol)))
(if (= beg (point))
(delete-char 1 nil)
(delete-region beg (point)))))
(defun cm-reverse-region (&optional arg)
"Reverse current region, like this: \"a(bc) d\" -> \"d )cb(a\"."
(interactive "P")
(let ((reversed (apply 'string (reverse (string-to-list (buffer-substring-no-properties (region-beginning) (region-end)))))))
(delete-region (region-beginning) (region-end))
(insert reversed)))
(provide 'cmmove)
I never shared(should I?) this thing before, you are welcome to leave comments.
Default bindings use hyper-key
(global-set-key [?\H-f] 'cm-find-nonspace-forward)
(global-set-key [?\H-b] 'cm-find-nonspace-backward)
(global-set-key [?\H-\]] 'cm-find-any-bracket-forward)
(global-set-key [?\H-\[] 'cm-find-any-bracket-backward)
(global-set-key [?\H-p] 'cm-fast-step-upward)
(global-set-key [?\H-n] 'cm-fast-step-downward)
(global-set-key (kbd "H-t") 'cm-reverse-region)
I wish also to find out, which behavior do you like for this commands. If they fail to find something, should they message or throw error?
Upvotes: 10
Reputation: 74480
There is no answer to your question.
It is a continuous journey, one that you can begin by typing M-x help, and doing this frequently.
Everyone has a different set of features they can't live without. So, other than just listing all the text editing features available, try (re-)reading the tour of Emacs, the FAQ, and the Emacs manual. The most active places to find plugin information is the Emacs Wiki and the Emacs Lisp Package Archive (to be distributed with Emacs 24).
99% of the features you don't know about will be found in the above links.
Of course, you can also look at the standard SO question/answer: what is the single most useful feature....
Upvotes: 7
Reputation: 141
I'm not an Emacs expert, but https://sites.google.com/site/steveyegge2/effective-emacs seems to cover some useful examples. Especially items 6 (buffer control) and 7 (lose the UI) seem worth memorizing. Also, the comment section covers some readers' tips.
Upvotes: 4