I'm using Paredit in Emacs to edit Lisp code. How do I delete an extra pair of parens that I accidentally inserted? Paredit does not allow this without extra commands.
((square 5)) into (square 5)
This is called paredit-splice-sexp, keybinding is M-s.
Take a look at the paredit cheat sheet here.
Read up on barfage and slurpage.
You can also installed "paredit-menu.el" which provides a menu of actions, with tooltips that show you their effects.
Paredit occasionally gets you into a situation when the easiest thing to do is to turn it off, fix everything and turn it on again. However, it is worth the effort.
If I realized accidental extra parens right after inserting those extra parens, I would invoke the undo command. If I saw the extra parens much later, then there are paredit commands for fixing it as other answers specified, but if I did not know those commands, then I would have put point somewhere within the inner form, and then typed the following, provided that CUA mode is on.
C-M-u C-M-SPC C-x <timeout> <left> C-M-SPC C-v
The paredit wont let us deleting parens while there is any elements inside. My trick is to cut the the right code and paste it to other place adjacent. It will leave the extra parens empty, then now I can delete it. Actually the right way is using M-s to reindent it. Conversely we can insert single paren using C-q ( or C-q }.
Related
Occasionally I yank code into a paredit-mode buffer that is missing a closing parenthesis or bracket. paredit-mode then detects the unclosed expression and tries to add the missing paren in the wrong place. This is frustrating when I move the cursor to the point where I want the close paren but I can't put it there. I end up having to switch off paredit-mode.
Here's an example:
I've just yanked
[["https://github.com/aconbere/yesql.git"
"aea69ebd4a7788a66fc8689fea7e806f1463c347"]
but paredit-mode sees a missing ) at the very end (in red). I'm not even sure why it wants to use a close parens when the opening is a bracket. Typing ] at the point makes the cursor jump to the end and try to fill in the red paren. Slurping or barfing don't have the intended effects and simply move the ) around.
Is there any way to fix this without temporarily disabling paredit-mode?
kill-region (bound by default to C-w) and quoted-insert (bound by default to C-q) are unaffected by paredit mode. You can delete regions or insert matching delimiters using these keybindings, bypassing paredit rules. Remember that you can also turn off paredit, clean something up, and then turn it back on again.
Others have already mentioned C-q for quoted-insert. You can also use C-u DEL or C-u C-d to override paredit's normal balanced behavior of DEL and C-d for a single deletion:
(foo)|)
C-u DEL
(foo|)
Trust me. By far the easiest is to paste a bracket or parentheses.
You can do:
; ]
and then copy it
When you have unbalanced parenthesis, navigate to the character position you wish to place the balancing parenthesis, issue M-x quoted-insert (bound by default to C-q), and then enter the balancing parenthesis. quoted-insert will not prevent parenthesis from being issued.
Same idea as noisesmith said differently. Turning paredit on and off is not an option :)
I am new to Emacs. I find it is powerful and very convenient IF one can master its set of commands. I also believe anyone can do this with patience, and the eagerness to learn and USE the software. However it will be much nicer for beginners, and seasonal users to have some command helper plugin that do following thing:
When the user start typing any CONTROL or META, COMMAND, or any special keys, a "ajax-like" or "smart search" window appear below or in the minibuffer showing help text like, for example: If I hold "CONTROL", command helper windows shows:
C-p Up one line
C-n Down one line
C-f Forward one character
C-b Backward one character
C-a Beginning of line
C-e End of line
C-x ...
Then if I continue to type "x" while holding "CONTROL", the window will update and show something like:
C-x C-a add-mode-abbrev
C-x C-b list-buffers
C-x C-c save-buffers-kill-emacs
C-x C-d list-directory
C-x C-e eval-last-sexp
C-x C-f find-file
C-x C-h inverse-add-mode-abbrev
C-x TAB indent-rigidly
C-x C-l downcase-region
C-x C-n set-goal-column
C-x C-o delete-blank-lines
I have tried googling it, but it doesn't seem exist yet. So I guess this is more a feature request for EMACS rather then a question. But it is great if somebody can write a plugin/module to do this.
I do not believe this is out there, but I also believe that it is not really needed. It would just clutter the screen, though, maybe for a rookie it might be indeed useful.
For now, you might consider printing a cheatsheet.
Do not lose hope, I learned it without any sort of help, the key is indeed to just use it often.
Also, there is quite a good logic behind it all, so if you read up the structure in a cheatsheet or anywhere else, then it might help you "guess" what is the right key combo sooner than later.
EDIT: I actually myself asked a similar question, at least the same functionality was required. I accepted an answer which apparently mentions that Icicles offers this functionality.
Also, one advise is that after you type the prefix key, e.g. C-c, typing C-h afterwards will show you the possible completions.
This leads me to the following:
I believe we could use code like:
"If last key is a prefix key, run C-h*, switch back to previous buffer and rehit the last prefix key"
*this is a general method, working even for my own poorly designed minor mode
I also don't know an existing package that would do that, but here's a couple of tips.
To get an overview of all available bindings, press C-h b. The displayed buffer looks fairly raw, because it includes all ways used to enter special characters (and they are usually at the top), but you can search it with C-s if you remember at least a part of the command name.
If you know the prefix (e.g. C-x) but don't remember what comes next, you can follow it with C-h instead (type C-x C-h). That will bring up a similar buffer that only contains bindings that start with the same prefix. It's one of the more obscure features, I think.
Speaking of "if I hold Ctrl", though, showing hints when you've just pressed Ctrl and nothing else if currently impossible to implement at the Lisp level. That would require some lower-level changes.
There are too many keybindings in emacs for this to be practically useful.
I agree with the other responses and just wanted to add that I found the built-in tutorial very useful for getting past the basics. You can reach the tutorial with C-h t.
You'll probably end up changing several of the default bindings, but learning them will help you find your way around new modes (assuming that they work in an "emacsy" way).
I come back to this be cause I found a perfect solution and can't live without it: The emacs-helm and which-key
I am learning org mode, and just found out about sparse trees (C-c / t and its kin). How can I go back to the original, unsparse, view of my org document?
I found out by trial and error that TAB-cycling the top node works, is there a better way?
C-c C-c should clear out the sparse-tree hiding and highlighting, but as far as I know, you can't just go back to the "last view" you had of it. If you want to go back to the full-view, use Shift-Tab to cycle all entries.
So, it's now 2018 and (AFAIK) this feature still doesn't exist.
The best workaround I've found so far, is to create an indirect buffer (C-x 4 c) and then run org-sparse-tree in there. The original window remains unaffected, so you keep your view, and changes to the indirect buffer will update the original buffer (and vice-versa). When you're done, you just close the indirect buffer.
I usually just run the org-mode command which seems to get me back to square one.
Ben K. was on the right track. Indirect buffers are one of emacs' most powerful features.
This function does what I would have expected org-show-todo-tree to do: create a new buffer showing undone TODO items, don't screw up my org file's tree state, and clear the unnecessary occur highlighting.
(defun org-todo-buffer ()
"Create new indirect buffer with sparse tree of undone TODO items"
(interactive)
(clone-indirect-buffer "*org TODO undone*" t)
(org-show-todo-tree nil) ; mimics interactive usage
(org-remove-occur-highlights)
)
In this new buffer you can change TODO item states which are reflected in your org file, and you can simply kill the indirect buffer when you are done with it.
Coming to this very late, I noticed that selecting all tags then un-highlighting/un-narrowing seems to do the right thing.
C-c \ *
C-c C-c
TAB-cycling anywhere only hides the entries highlighted by org-sparse-tree.
To remove the overlays, you need to actually edit the buffer.
As you said, you can there by visibility cycling with S-TAB, but I personally don't like visibility cycling because I'm never sure where I am in the cycle.
So I just created this simple org-agenda-custom-command that shows everything without highlighting. Just add it to your .emacs file.
(setq org-agenda-custom-commands
; ... other commands
`(("z" "All" occur-tree "."
((org-show-entry-below t)
(org-highlight-sparse-tree-matches nil)))))
There probably is a better way to do this, and the beauty of SO is someone will tell us :).
What I usually do to work around this is to use C-x C-v RET (find-alternate-file) or M-x revert-buffer. This works only if you don't have unsaved edits.
I found that the (setq org-agenda-custom-commands.. answer works the best for me.
Use with (sorry, it wasn't obvious to me):
C-c a z
In Emacs I'm editing some source code, and I hit <tab>. Emacs indents the line to n spaces. I'd like to change the amount that indents for that kind of line. How do I figure out what rule emacs applied to indent that line by n spaces?
I want to change n, but I need to figure out which of the many indentation-related variables Emacs just used.
A generic answer is difficult. Some modes will make this more apparent than others, but in the general case (as they are free to implement indentation however they wish) I don't think you'll get away from needing to read some elisp.
Starting with the binding for TAB will work, but might be slightly time-consuming depending on how many layers of indirection are involved.
If you know that the major mode in question implements its own indentation, then one (non-rigorous, but fast) approach that you could try to help track down the functions being called is to use ELP, the built in elisp profiler. elp-instrument-package will instrument for profiling all functions with names matching the prefix string argument you specify. Therefore you might do something like the following in a PHP file (noting that php-mode tells you that it is derived from c-mode)
M-x elp-instrument-package RET php- RET
M-x elp-instrument-package RET c- RET
M-x elp-instrument-package RET indent RET
Now type TAB in your source code, and run M-x elp-results to see which of those instrumented functions were called.
At this point you're on your own -- look for the likely suspects, and see what the code is doing -- but it can be a handy way to filter the search.
Once you've finished, use M-x elp-restore-all to prevent any further profiling.
If you're using a mode based on cc-mode (e.g. c-mode, c++-mode, java-mode, etc.), you can hit C-c C-s and it'll tell you what syntactic category the line is. If you want to change it, hit C-c C-o and you'll be guided through the process. Check out the cc-mode docs on customization for more details: https://www.gnu.org/s/emacs/manual/html_node/ccmode/Customizing-Indentation.html
If you happen to enjoy getting your hands really dirty, there's always the elisp debugger to tell you just what Emacs is up to.
If you hit C-h k TAB you'll find the function that Emacs is running (e.g. indent-for-tab-command) then you can do M-x debug-on-entry RET indent-for-tab-command RET. Now whenever you hit TAB you'll pop up a debugger and can watch the execution step by step.
Depending on your taste for debugging, it's either a maddening or enlightening experience. Either way, don't forget to M-x cancel-debug-on-entry when you're done.
Vim completes words and lines with CTRL-X P and CTRL-L. There's a Emacs plugin called Company mode but this plugin interfere and cause conflicts with lots of things within Emacs (with global linum and yasnippets). I know that I can complete words with CTRL-/ in Emacs. But it is possible to take previously written lines to complete code?
Maybe you're looking for hippie-expand? From that web page (as of this writing, anyway):
HippieExpand looks at the word before
point and tries to expand it in
various ways including expanding from
a fixed list (like expand-abbrev),
expanding from matching text found in
a buffer (like dabbrev-expand) or
expanding in ways defined by your own
functions. Which of these it tries and
in what order is controlled by a
configurable list of functions.
For a comprehensive list of completion options visit the emacs wiki page on completion.
There are a gazillion ways to do completion in Emacs. Some are mode specific, some inline, some configurable and what not. Here is a list of modes that might help you.
Use numberic argument to complete by line, say M-5 M-/ will complete by line, while M-/ alone still complete the normal way.
hippe-expend function has a very useful feature which is :
With a positive numeric argument, jumps directly to the ARG next function in this list. With a negative argument or just C-u, undoes the expansion.
Customize the expansion functions in hippie-expand-try-functions-list and put the function try-expand-line as 5th list element, then you could use M-5 M-/ to complete by line.
This tip is very handy and useful and I highly recommend it.
Also worth noting: if your window manager does not steal Alt-tab, emacs will auto-complete with Alt-tab (I set up my window manager to user the "windows key" instead of alt for this very reason).
If you are using evil, this is the most vim-like solution I use:
(defun my-expand-lines ()
(interactive)
(let ((hippie-expand-try-functions-list
'(try-expand-line-all-buffers)))
(call-interactively 'hippie-expand)))
(define-key evil-insert-state-map (kbd "C-x C-l") 'my-expand-lines)
This way you can use our old friend C-x C-l in insert mode for whole line all buffers completion.
Thanks #ymln for the suggestion of using try-expand-line-all-buffers.