Emacs: Enter commands like in gedit
Asked Answered
D

3

5

in gedit it's possible to define so-called "snippets" for simpler input.

For example, there is a snippet while. This means: If you type while -> (-> stands for tab key). And gedit automatically converts it to the following (including correct indentation):

while (condition){

}

In vim (in conjunction with latex-suite) I saw the following: If you type (, vim inserts just a (. If you type ( a second time, vim automatically converts it to \left( \right).

I found abbrev-mode but this mode doesn't place the cursor properly (i.e. between parentheses or inside the while loop).

I managed to create custom emacs keybindings/macros that do just the same (without having to press the tab key), so I know it's possible.

However, is there already and package where you can define such "snippets" without much effort? Or are there even any serious reasons not to use such things?

Daguerre answered 27/7, 2010 at 14:34 Comment(0)
K
8

See yasnippet. It provides snippets for most major languages, and it is easy to add new ones or modify the old ones.

Kastner answered 27/7, 2010 at 14:49 Comment(1)
Thank you. I'll have a look at it.Daguerre
B
3

Yes, yasnippet is probably the way to go. But make sure you learn the major mode you're using for your editing - when writing in LaTeX, learn auctex. Major modes can contain functionality that makes some snippets pointless, and do the same thing even better. So instead of using a begin/end-snippet in a LaTeX buffer, try C-c C-e in auctex. Etc :)

Bridgetbridgetown answered 27/7, 2010 at 17:45 Comment(1)
+1 for mentioning how much functionality the major modes already have that make some snippets more work than just learning the relevant key chords/combinations.Hegemony
H
1

Don't forget abbrev-mode.

Hamster answered 29/7, 2010 at 2:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.