In emacs paredit-mode one can kill all following expressions which are inside the current line by hitting Ctrl+k:
(1 |(2 3) 4 5)
(1)
How can you do the same thing, but just affecting the next expression:
(1 |(2 3) 4 5)
(1 4 5)
try C-M-k (ctrl+alt+k)
It is not the paredit feature, but emacs native editing feature:
http://www.cliki.net/Editing+Lisp+Code+with+Emacs
Related
Is there some way to collapse Lisp functions in Emacs? I found these answers:
emacs collapsing functions in class using outline-minor-mode
Is it possible to collapse a function in emacs?
But they use add-hook, which gives me an "undefined function" error. What am I missing?
You can try origami-mode.
You can find detailed information about it in the Github page, but in short, just install it and call origami-recursively-toggle-node to fold and unfold a function.
There's no need for adding hooks or what not. You probably have something wrong with your config, possibly a require missing somewhere.
I use hs-minor-mode, as recommended in a comment by Rorschach in the question. The hs-minor-mode came with my emacs install and it works well.
To determine the useful commands, I typed Ctrl-h v hs-minor-mode-map and examined the keyboard map when hs-minor-mode is active (I added the comments so that the actual key sequences are easily read):
(3 keymap ; Ctrl-C
(64 keymap ; # (at-sign)
(5 . hs-toggle-hiding) ; Ctrl-E
(4 . hs-hide-block) ; Ctrl-D
(20 . hs-hide-all) ; SPACE
(1 . hs-show-all) ; Ctrl-A
(3 . hs-toggle-hiding) ; Ctrl-C
(12 . hs-hide-level) ; Ctrl-L
(27 keymap ; ESC
(19 . hs-show-all) ; Ctrl-S
(8 . hs-hide-all)) ; Ctrl-H
(19 . hs-show-block) ; Ctrl-S
(8 . hs-hide-block)))) ; Ctrl-H
The recommended keystrokes are difficult to type. I decided to assign Alt-H to the following elisp (it's not exactly what I want yet, but I do like to assign one keystroke to do multiple things):
(lambda () (interactive)
(hs-minor-mode 1)
(if (hs-find-block-beginning)
(hs-toggle-hiding)
(let ((from (point-min))
(to (point-max)))
(while (and
(not (hs-overlay-at from))
(setq from (next-overlay-change from))
(not (= to from)))) ; locate first hs-overlay
(if (= to from) ; hs-overlay-was-not-found
(hs-hide-all)
(hs-show-all))))))
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).
Here's what I'm talking about:
$ racket
> (list 1 2 3)
'(1 2 3)
It's an annoyance very similar to constructor-style printing. What it's showing is basically that (list 1 2 3) evaluates to (quote (1 2 3)).
The gracket REPL doesn't do that (yet):
$ gracket -z
> (list 1 2 3)
(1 2 3)
In DrRacket it's possible to get rid of the tick marks through a menu option pertaining to "output syntax", but command-line Racket has no menus.
The default Racket printer is controlled by the print-as-expression parameter. If you set this to #f, it will disable expression-style printing.
> (print-as-expression #f)
> (list 1 2 3)
(1 2 3)
If you really do hate this behavior, you could add the (print-as-expression #f) line to your init file (~/.racketrc on Linux and OS X, ~/racketrc.rktl on Windows), which will get loaded at startup.
This issue ( https://github.com/nex3/sass-mode/issues/5 ) already explained what I want to say, please have a look at it. Is there any way to fix it.
When I hit backspace, for instance after c-j, which always goes to one
level deeper instead of the same level, nothing happens except for the
control buffer giving the message "Mark set". When I check I see it's
bound to haml-electric-backspace. Thus the only two ways I can create
a new line and move my cursor to the same level of indentation as the
last line is ret tab tab or c-j followed by c-b c-b or tab tab. I
think c-j should default to the same level and that backspace should
have the default emacs behavior.
I found a bunch of bugs with sass-mode.
Here's some code I keep in my .emacs that might help:
(defconst sass-line-keywords
'(("#\\(\\w+\\)" 0 font-lock-keyword-face sass-highlight-directive)
("/[/*].*" 0 font-lock-comment-face)
("[=+]\\w+" 0 font-lock-variable-name-face sass-highlight-script-after-match)
("!\\w+" 0 font-lock-variable-name-face sass-highlight-script-after-match)
(":\\w+" 0 font-lock-variable-name-face)
("\\w+\s*:" 0 font-lock-variable-name-face)
("\\(\\w+\\)\s*=" 1 font-lock-variable-name-face sass-highlight-script-after-match)
("\\(:\\w+\\)\s*=" 1 font-lock-variable-name-face sass-highlight-script-after-match)
(".*" sass-highlight-selector)))
(defconst sass-selector-font-lock-keywords
'( ;; Attribute selectors (e.g. p[foo=bar])
("\\[\\([^]=]+\\)" (1 font-lock-variable-name-face)
("[~|$^*]?=\\([^]=]+\\)" nil nil (1 font-lock-string-face)))
("&" 0 font-lock-constant-face)
("\\.\\w+" 0 font-lock-type-face)
("#\\w+" 0 font-lock-keyword-face)
;; Pseudo-selectors, optionally with arguments (e.g. :first, :nth-child(12))
("\\(::?\\w+\\)" (1 font-lock-variable-name-face)
("(\\([^)]+\\))" nil nil (1 font-lock-string-face)))))
(defconst sass-non-block-openers
'("^.*,$" ;; Continued selectors
"^ *#\\(extend\\|debug\\|warn\\|include\\|import\\)" ;; Single-line mixins
"^ *[$!]" ;; Variables
".*[^\s-]+: [^\s-]" ;; a setting of some sort
))
I think the problem was particularly with sass-non-block-openers. I'm sorry that I'm just dumping some code on you. Hope this helps somehow anyway.
when finished, call RET, not C-j
backspace will step down level
everything okay AFAIS, no bug
Imagine I've got the following in a text file opened under Emacs:
some 34
word 30
another 38
thing 59
to 39
say 10
here 47
and I want to turn into this, adding 1 to every number made of 2 digits:
some 35
word 31
another 39
thing 60
to 40
say 11
here 48
(this is a short example, my actual need is on a much bigger list, not my call)
How can I do this from Emacs?
I don't mind calling some external Perl/sed/whatever magic as long as the call is made directly from Emacs and operates only on the marked region I want.
How would you automate this from Emacs?
I think the answer I'm thinking of consist in calling shell-command-on-region and replace the region by the output... But I'm not sure as to how to concretely do this.
This can be solved by using the command query-replace-regexp (bound to C-M-%):
C-M-%
\b[0-9][0-9]\b
return
\,(1+ \#&)
The expression that follows \, would be evaluated as a Lisp expression, the result of which used as the replacement string. In the Lisp expression, \#& would be replaced by the matched string, interpreted as a number.
By default, this works on the whole document, starting from the cursor. To have this work on the region, there are several posibilities:
If transient-mark-mode is turned on, you just need to select the region normally (using point and mark);
If for some reason you don't like transient-mark-mode, you may use narrow-to-region to restrict the changes to a specific region: select a region using point and mark, C-x n n to narrow, perform query-replace-regexp as described above, and finally C-x n w to widen. (Thanks to Justin Smith for this hint.)
Use the mouse to select the region.
See section Regexp Replacement of the Emacs Manual for more details.
Emacs' column editing mode is what you need.
Activate it typing M-x cua-mode.
Go to the beginning of the rectangle (leave cursor on character 3) and press C-RET.
Go to the end of the rectangle (leave cursor on character 7). You will be operating on the highlighted region.
Now press M-i which increments all values in the region.
You're done.! remove dead ImageShack links
It doesn't protect against 99->100.
(defun add-1-to-2-digits (b e)
"add 1 to every 2 digit number in the region"
(interactive "r")
(goto-char b)
(while (re-search-forward "\\b[0-9][0-9]\\b" e t)
(replace-match (number-to-string (+ 1 (string-to-int (match-string 0)))))))
Oh, and it operates on the region. If you want the entire file, then you replace b and e with (point-min) and nil.
Moderately tested; use M-: and issue the following command:
(while (re-search-forward "\\<[0-9][0-9]\\>" nil t) (let ((x (match-string 0))) (delete-backward-char 2) (insert (format "%d" (1+ (string-to-int x))))))
I managed to get it working in a different way using the following (my awk-fu ain't strong so it probably can be done in a simpler way):
C-u M-x shell-command-on-region RET awk '$2>=0&&$2<=99 {$2++} {print}' RET
but I lost my indentation in the process : )
Seeing all these answers, I can't help but have a lot of respect for Emacs...