5

I have this problem in JavaScript, but I understand the solution to it could be 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?

kotchwane
  • 481
  • 2
  • 12

4 Answers4

4

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.

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.
fpiper
  • 451
  • 2
  • 2
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.

kotchwane
  • 481
  • 2
  • 12
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.

chen bin
  • 4,781
  • 18
  • 36