Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moving to the start of a code line: Emacs

Tags:

emacs

I use emacs for development and very often need to move to the start of a line (C-a). However if the line is indented, I'd like to move to the point at which code starts.

So while browsing code: ( ) for x in xy|z:. On typing C-a we get this: |( ) for x in xyz:. But instead, I would like this:( ) |for x in xyz:

Here | indicates cursor and () indicate spaces or tabs.

How can I make make this happen?

like image 630
GeneralBecos Avatar asked May 17 '11 19:05

GeneralBecos


People also ask

How do you go to the beginning of a line in Emacs?

To go to a specific line: Type M-x goto-line <Enter>, then type the line number followed by <Enter>. There are also shortcuts. Type the command "help -q emacs goto" in a local window to find out about it.

How do I move around in Emacs?

The keybindings for movement by word in Emacs is almost the same as that of movement by character, but instead of the prefix C- it is M- . To move forward one word use M-f ; and to move backward one word use M-b . Movement by word will make up the bulk of your intra-line movement.

What does C in Emacs stand for?

Summary of essential emacs commands In the list below, C- means "control key", M- means "meta key" (escape). For meta commands, press the meta key, then the other key. Thus M-f stands for the keyboard sequence "press meta key", " press f".

How do you delete a line in Emacs?

Move your cursor directly before the region you want to delete. Set a mark by pressing Ctrl-6 or Ctrl-^ . Move the cursor to the end of the region you want to delete and press Ctrl-k .


2 Answers

Meta-m

like image 54
Svante Avatar answered Oct 20 '22 01:10

Svante


A favorite way for me to handle this is to have C-a toggle between the beginning of the line and the beginning of the code. You can do so with this function:

(defun beginning-of-line-or-indentation ()   "move to beginning of line, or indentation"   (interactive)   (if (bolp)       (back-to-indentation)     (beginning-of-line))) 

And add the appropriate binding to your favorite mode map:

(eval-after-load "cc-mode"       '(define-key c-mode-base-map (kbd "C-a") 'beginning-of-line-or-indentation)) 
like image 30
Trey Jackson Avatar answered Oct 20 '22 01:10

Trey Jackson