LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-18-2006, 04:54 AM   #1
une
Member
 
Registered: May 2004
Location: Australia
Distribution: Mandrake 10, Puppy Linux 2.13
Posts: 201

Rep: Reputation: 30
Fomatting code text using emacs


When writing code in C++ or Java using emacs I notice that I can get the code all nicely aligned and indented correctly by simply adding a comment at the end of each line of code. As you enter the //, the line of code moves to its correct alignment. What I have been doing is writing my code and periodically tidying it up by adding // at the end of every line of code. My question is, can I speed this aligning and indenting process up by formatting the entire file using one command? Emacs obviously knows how much each line should be indented, but doing it one line at a time by adding // is laborious.
 
Old 11-18-2006, 08:50 AM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
You can press TAB and the line you are currently on should get indented. You can also select a region with C-space to mark the start of the block , moving the cursor to the end of the block and then doing "M-x indent-region".
 
Old 11-18-2006, 09:46 AM   #3
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 41
Even faster, use C-x h to highlight everything, then C-M-\ to re-indent it. Also, C-j normally does a newline-and-indent type function (for most modes; not Emacs Lisp Interaction though). If you want to do a sort of auto-indentation for certain modes, you can bind the RETURN key to do indentation like so:
Code:
;; My new binding to use for <RET> in some modes.

(defmacro taylor-return-key nil
  '(lambda nil (interactive) (progn (indent-according-to-mode) (newline-and-indent))))

;;
;;  Rebind <RET> key to do automatic indentation in certain modes.
;;

(mapcar
 (lambda (mode)
   (let ((mode-hook (intern (concat (symbol-name mode) "-hook"))))
     (add-hook mode-hook (lambda nil (local-set-key (kbd "RET") (taylor-return-key))))))
 '(ada-mode c-mode c++-mode cperl-mode emacs-lisp-mode java-mode html-mode
            lisp-mode php-mode ruby-mode sgml-mode sh-mode tuareg-mode))
Just list whatever modes you want to apply it for in that last list with all the mode names in it. This is what I use for almost everything. When you hit RETURN, it reindents the current line, moves you down to the next line, and auto-indents there. Very useful.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
where is the .emacs file in the emacs source code tarball? aizkorri Programming 2 01-13-2007 03:05 PM
text encoding, emacs and LaTeX. bnj Linux - Newbie 2 10-13-2005 08:56 AM
emacs, code completion phoenix7 Programming 5 09-22-2005 10:51 AM
Copying text from firefox into emacs MDBlueIce Linux - Software 1 05-22-2005 04:58 PM
EMACS-text editor TrippyChik Linux - Software 10 10-17-2003 10:36 AM


All times are GMT -5. The time now is 07:15 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration