4

I have this problem in JavaScript, but I understand the solution to it could me more general. My cursor (point) is on the name, or on the body, of a function, like this :

function test() { // My cursor is here or anywhere between "function" and "}"
console.log("test");
}

I would like to quickly select function test, in order to move it somewhere else in the code, or remove it completely. Is there a function, in js2-mode or anywhere else, which would allow me to do that?

2

See this part of the manual. Marking with C-M-h should work just fine. This runs mark-defun and marks the complete current function definition around point.

Some nifty details:

  • If your mark was already active, it extends the region until the end of the next defun.
  • A negative argument reverses the direction of traver for all subsequent calls. So by default without previous calls to mark-defun this selects the previous defun.
| improve this answer | |
  • Thanks for the useful manual link! – kotchwane Oct 29 '19 at 8:16
  • Please explain what that keybinding does to make this a complete answer. – Dan Oct 29 '19 at 20:50
3

Emacs calls a function definition a “defun”, because defun is the keyword¹ that starts a function definition starts in Lisp. Commands to move by defuns use the modifiers Ctrl+Alt:

  • C-M-a and C-M-e to move to the beginning/end of the current function definition;
  • C-M-h to select the current function definition.

This is somewhat similar, but not strictly parallel, to M-a and M-e to move to the beginning/end of the current sentence, and M-h to select the current paragraph.

The exact behavior of these commands depend on the programming language. The default rule is that a defun starts with a line with 0 indentation, but many programming language modes change this, either by tuning the way the default function works or by binding the usual keyboard shortcuts to a different function.

On a related note, Emacs calls a subexpression a “sexp”. Generally a sexp is delimited by balanced parentheses, brackets or braces, or is a single identifer. Again the exact rules depend on the programming language. The main commands for sexps are:

  • C-M-b and C-M-f to move back/forward to the previous/next sexp.
  • C-M-p and C-M-n to move back/forward to the previous/next expression delimited by parentheses (the exact rules depend on the language, but typically this stops in fewer places than C-M-b and C-M-f).
  • C-M-u (M-x backward-up-list) and M-x up-list (no default binding) to move out of the surrounding parentheses/brackets/braces.
  • C-M-SPC or C-M-@ to select the following sexp.

¹ Not a keyword, strictly speaking, but close enough.

| improve this answer | |
1

I think I found it. Use C-M h, which is bound to mark-defun.

The beauty of it is that it works not only on JavaScript, but also on Python, Elisp, or seemingly any other language. I found the solution on this post which is specific to C.

| improve this answer | |
0

Install evil, evil-matchit, then press %. It supports many programming language, not just javascript. It can be extended easily by user to support new syntax and new languages.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.