r/emacs•
kovak

What are some advanced editing techniques that save you a lot of time?

I was looking at the demo gif on Sublime Website. I didn't even know multiple cursors feature exists. That lead me to its multiple-cursors.el.

I'm sure some of you guys here are using modules/shortcuts/macros you take for granted, but some of us haven't even heard of them. Would be great if you can share them.

Posted Mar 25, 2017, 2:14 AM UTC

3143
into_lexicons•

multiple cursors is pretty dope, but every time i use it i get this feeling like i'm never quite sure if it's saving me more time than defining and repeating a quick keyboard macro would have.

nearly everybody knows about search forward / backward but i didn't really appreciate using them as cursor movement commands to jump around in the file until i started seeing other folks using search commands as cursor movement.

the thing that saves me a ton of time that i rarely see other people use is text registers. for a long time i was in the bad habit of just using the regular kill ring for these things, but registers is way faster when you're juggling several snippets and you don't want to have to sit there and M-y a bunch of times to get to the thing you wanted to yank back.

21
qotob•

Someday, text registers. Someday.

10
Skywalker8921•

Text registers... So useful when grading programming assignments... One register for "Please make sure the program names and interfaces match the question", one for "This does not compile", one for "This does not pass the tests", one for "Someone please kill me now"...

4
TarMil•

nearly everybody knows about search forward / backward but i didn't really appreciate using them as cursor movement commands to jump around in the file until i started seeing other folks using search commands as cursor movement.

And to navigate to the previous/next instance of the identifier at point even more quickly, the package highlight-symbol provides highlight-symbol-next/prev.

8
drearyneonclouds•

Isn't that just *?

3
Taikal•

For me it's just M-n (and M-p for the previous symbol).

2
into_lexicons•

cool tip, thanks for this!

1
kookjr•

Text registers and C-x e macros are what always make me give up on IDEs after a few days, no matter what other features they have.

5
larsga•

I use text registers a lot, but haven't been a macro user because I never really learned it. I just read this, and now I'm definitely going to start using macros.

5
Claitu•

You might try elmacro-mode which makes macros more comfortable. I wrote a couple of wrappers which I use everyday. With this you can: press F6 start start recording, press it again when done. Now you're asked if you want to repeat the macro (once per <return> or repeatetly. Also the generated code will be written to a config file including a repeater-wrapper, so that you can M-x macro-repeat anytime later. Or press C-F6 anytime to execute a macro (with completion).

2
larsga•

I tried ordinary macros earlier today. Worked like a charm. I don't think I need anything more, tbh. But thank you anyway.

1
kookjr•

The best thing is if you repeat things multiple times do C-x e the first time then just e the rest.

1
attrigh•

If text registers are too much for you, you might prefer something like helm-show-kill-ring as part of helm this let's you search through recent kills (with fuzzy searching) and paste them.

Obviously this is less precise and requires more keypresses that text registers - but potentially uses less mental state.

5
handbasket_rider•

requires more keypresses that text registers - but potentially uses less mental state

Definitely. (counsel-yank-pop for me). My fingers just type a fragment of the one I want without me having to engage much brain power, and you don't have to plan ahead.

Of course some tasks would be better with registers, especially when working with macros, but in general I like solutions where I don't have to think much, and being a touch typist, there's minimal friction to typing fragments of text.

1
holgerschurig•

M-y

It's bound to helm-show-kill-ring and so it's very easy (and fast) for me to narrow to previous kill ring entries.

For the registers, I always thought that their calling key stroke sequence is like in an old adventure: "You're follow a long, dark and winding path ..." :-)

1
sauntcartas•

For the very common case of changing a single word throughout a file, I have this workflow:

  • Start with the cursor on one instance
  • C-= to call er/expand-region from the expand-region package, to mark the word at point
  • C-; to call iedit-mode from the iedit package, to highlight every other occurrence of the region in the buffer
  • Change the text, even replacing it completely
  • C-; again to deactivate iedit-mode

But in general multiple-cursors is just awesome. At my previous job I regularly had to write code to parse JSON into the fields of a Scala case class, and I would start with the lines that define the class's fields, eg:

foo: Int,
bar: String,
baz: Double,
bletch: Boolean

So I would start by putting a cursor at the start of each line, and then, with electric-pair-mode active:

  • C-M-space (mark-sexp) to mark each name
  • M-w (kill-ring-save) to copy each name
  • C-M-f (forward-sexp) to skip over each name
  • C-d (delete-char) to remove the colons
  • <- (json \ "
  • C-y (yank) to insert a copy of the saved names
  • C-f C-f C-d to move forward two characters, and delete the space
  • .validate
  • C-d to delete the spaces
  • C-M-space (mark-sexp) to mark the type
  • [ to wrap the type with square brackets, courtesy of electric-pair-mode

And I'm left with the body of a for comprehension that parses and validates the input JSON:

foo <- (json \ "foo").validate[Int]
bar <- (json \ "bar").validate[String]
baz <- (json \ "baz").validate[Double]
bletch <- (json \ "bletch").validate[Boolean]

Watching this complex transformtion being repeated across twenty or more lines is awe-inspiring.

8
flarkis•

Expand region is amazing

2
raxod502•

See also visual-regexp to accomplish the same task in a more general way: http://i.imgur.com/O2keiw9.png

2
goldfather8•

Using avy as an evil motion. Don't need relative line numbers to delete to arbitrary line. Can delete to some word later in the sentence/next lines without guessing at the distance.

Evil-surround for surrounding a motion with arbitrary characters.

Getting better with expand-region for selection.

Evil-exchange for exchanging any two motions, particularly useful for arguments.

All of lisp state. Things like wrapping, traversing the AST work in various capacities outside just lisp. For instance I wrap python statements in parenthesis with 'SPC k k w'. If the wrap was more complex, using evil surround it would be 'y s motion ('.

7
joncol•

Thanks for mentioning evil-exchange. I have been looking for something like that!

1
agumonkey•

Go through all emacsrocks video if not already the case. Magnars is quite talented at using the editor in very effective ways.

If you're editing lisp code, invest some time in paredit. Maybe do three passes. One month to get the simplest things in. Then dont think about paredit; then another month to read a few more of paredit bindings... etc etc. I say this because it took me 3 attempts to master it, and it's one the most useful thing I've ever learned.

ps: multiple-cursors, expand-region, yasnippets, even vanilla navigation like prev/next paragraphs, zap-to, occurs

6
catern•

Narrowing to defun, region, or other mode-specific things (subtrees in org). Allows you to focus in on a specific region, which can augment the usefulness of other commands.

C-x n d, C-x n n, C-x n w.

Try C-x n ? to see all the commands under that prefix.

5
jwindre•

Here's a quick & dirty list of things that help me move around in emacs.

isearch-forward-regexp (and backward), undo-tree-visualize, expand-region, multiple-cursors (mc/edit-lines acts like text-rectangle), hideshow, yasnippet, flycheck, magit, projectile

helm. helm-projectile, helm-projectile-ag, helm-swoop, helm-register, helm-bookmark, helm-occur, helm-do-grep-ag, helm-semantic-or-imenu, helm-company, helm-dabbrev, helm-top, etc. The list here goes on.

org mode. Aside from time management, I use org-babel heavily to run & keep a history of things I've executed (shell scripts, sql statements, anything). This works great for commands I don't run often, allowing me to revisit them in my org file and easily re-run them.

Furthermore on org mode, I also like to timestamp everything I run that is executed on a remote server (eg. on a server at work). This way, I can provide an audit of everything I've done in case something doesn't go according to plan. I know this information can be found in history files, but I like my org files.

3
R6stuckinsd•

Maybe you already know about them, but I find two very useful features are M-x picture-mode and emacs rectangles.

2
attrigh•

"I find that micro-optimisations aren't worthwhile, and the most important thing is to work on the right thing. I use notepad".

So I love yasnippet. I've tweaked yas so that if a snippet is not defined then it starts defining it (see https://gist.github.com/talwrii/1025e35439e0a7fbc1175564b905d969) (I've made/ adapted a similar thing for zsh as well https://github.com/facetframer/zshnip). This means that defining a snippet only takes a couple of keystrokes, which in turn allows you have to have very small snippets.

2
uint64•

I quite like passing regions through the shell. For example using grep to filter things, column to align things and awk to do stuff that might take a bit longer with a macro. C-u M-S-\

2
KappaDistributive•

As someone who types mathematics on a daily basis and uses a ton of complicated notation over and over both a healthy collection of yasnippets and registers save me a ton of time. With my current setup I actually am slightly faster typing LaTex than writing by hand (assuming that no diagrams show up).

2
goldfather8•

How long did it take you to reach that point? Do you take notes in latex or still handwrite first?

2
KappaDistributive•

Since I finished my master's I no longer take lecture notes. When I still did, I just typed them and drew diagrams by hand to later insert them into my notes.

How long did it take me to be able to type my lectures notes? I don't know. You have to be able to type fast enough and also comfortable enough with your setup that complicated notation doesn't slow you down too much. For me there was no chance to type my notes before I switched to a powerful editor (in my case emacs) but a fellow student managed to type his notes with a plain text editor, since he types significantly faster than me.

If you want to type your lectures notes but don't know whether you can, why don't you just visit a lecture you usually don't attend and have a trial run?

2
Taikal•

F3 and F4. Keyboard macros, that is. I use them even for small changes, sort of like the . command of Vim.

2
otatew•

Can you explain further please. I am new to emacs macros.

1
Taikal•

Sorry for the late reply. The . command in Vim repeats the last edit. An edit can be a deletion or a text replacement. This can be emulated with keyboard macros. For example, you could delete some text and optionally type some other text in its place. If you record your edits as a keyboard macro, you will be able to repeat them elsewhere. So, for our example: F3 M-@ newtext F4 will replace the word under point with newtextand now you can do the same with other words by just pressing F4.

2
otatew•

Thank you. Your example helps.

1
dagit•

The big two for me:

  • rectangular regions
  • recording macros that are composable. Usually this means starting the macro with a C-a, doing the edit, and ending the macro with C-n. This makes it so that repeating the macro for a set of lines is now quite easy.
2
Grue•

Ending with C-n doesn't work if the line wraps around (this was changed in one of recent-ish major Emacs versions), so I made a habit to end per-line macros with C-e C-f.

1
dagit•

That's unfortunate. I haven't run into that yet.

1
[deleted]•

[deleted]

1
attrigh•

Also helm-swoop speeds up searching quite a lot.

It's basically the same as an interactive search with some additions:

  • Line based
  • Fuzzy matching (a search for "hello" becomes .*h.*e.*l.*l.*o.* , reduced key presses)
  • All matches asre shown on a separate screen reducing eye motion to look for search matches. Also allows you to quickly skip to the match you want.
1
attrigh•

Okay enough answers (I'm posting them separately so that people can comment on distinct topics).

One final thing is that the killer emacs editing feature is how easy it to define new features, or find other people's.

list-packages can install addons in seconds.

As an illustration of how easy it is to define custom behaviours here is a function that finds a line in your file and pastes it at the current point.

(defun hf-paste-search  ()
  (interactive)
  (let (line)
        (save-excursion
          (helm-swoop-empty)
          (setq line (thing-at-point 'line)))
        (insert line)))
1
groundhogmeat•

string-insert-rectangle and whatever the command is that removes it.

1
phenguin•

These are normal vim things, but work equally well in evil.

:g/<regex>/<ex command> :v/<regex>/<ex command>

Those apply the given ex command to lines matching / not matching the regex. The most often used ex commands with these are "d" to delete lines, and norm.

e.g. :g/^Fish.*Bird$/norm A fish plus bird<ret>

appends " fish plus bird" to each line starting with Fish and ending with Bird.

1
caeciliusinhorto•

markdown has eaten your code.

:g/^Fish.*Bird$/norm A fish plus bird<ret>

is the correct invocation.

The use of :g/re/ I make use of most often is to run macros on lines which match a regular expression:

:g/^*/norm@q<ret>

runs the macro in register q on every line which begins with a *.

You can also use ranges in a similar way:

:.,$norm@q<ret>

runs the macro recorded in register q on every line from the one the cursor is on to the end of the file.

1
fgunbin•

History and future history in minibuffer: M-p, M-n. Isearch on them: like `C-x b C-s <letters>'.

1
holgerschurig•

Macros. I bound kmacro-end-or-call-macro to F4.

1
peter-salazar•
  1. goto-last-change
  2. expand-region
  3. palimpsest-mode
1