Tag Archive: Emacs Lisp



This is the next  part i.e part-2  of my article which got published in the International Magazine “Linux For You” also popularly know as LFY. The article was published  under the name of “Let’s Play With Emacs  CLI”  in the August 2010 edition. All the work done is under the creative common license as specified in the widgets also :) If you are new to Emacs and want to learn it ,You must visit the Part-1 first.

Modes in Emacs

The vi/vim editor has two modes: command mode and insertion mode, which you have to switch between based on whether you are inputting text or commands to vi. However, in Emacs, this is not the case; you can run commands by typing a key sequence that’s bound to some function, as we saw above—and while not doing that, you can modify the text in the active buffer.

Emacs’ modes are different; they are editing modes, which extend Emacs’ capabilities, or change the way some features work, when they are invoked. There are several modes available already in Emacs, for editing each of a certain type or class of data, such as:

  • Regular (text) documents
  • Source code in a particular computer programming language (C, FORTRAN, Lisp, etc.)
  • Text formatted in a certain way (outlines, e-mail messages, Usenet articles, character-based illustrations, etc.)
  • Text with mark-up (like Hyper-Text Markup Language, HTML, for example).
  • There’s even a mode for editing non-text (binary) data!

Modes are classified as major or minor. A major mode dictates the main editing behaviour, and is applied only to the active buffer in the current editing session. There can only be one major mode active at a time, though you can switch between different major modes. Minor modes offer some capabilities that are not associated with any particular major mode. Multiple minor modes can be active at a time.We’ll get to some of the available major and minor modes soon, but before that:

How do you find out the currently active mode? Well, remember the Emacs mode-line bar we mentioned earlier, which is just below the buffer? This bar displays (among other information) your currently active modes, in parentheses, toward the right side of the mode line. Below is the screenshot of the same

Mode-Line Displaying Fundamental Major Mode

The major mode that’s active by default (as shown in the above screenshot) is Fundamental mode. The simplest of all Emacs modes, it has the fewest special key bindings and settings. Here, the Tab key is not bound to a function, and works as you’d expect it to (inserts a Tab into the buffer content). (To see some of the functions the Tab key invokes in other modes, search the Key-Index page for “TAB: Completion Commands” and view the next few entries.)

As an example of invoking a minor mode: suppose you want to overwrite text that’s already entered in the buffer—then you want to go into the overwrite minor mode). The function to invoke this is “overwrite-mode”; to activate this minor mode, either press the bound key, “Ins” (Insert), or type Alt+x overwrite-mode. The mode-line bar changes to show the new minor mode, as shown in the screenshot below:

Overwrite Minor Mode As Shown On Mode-Line

Note: Not all minor modes display an indicator on the mode-line; some minor modes are self-evident, such as the Tool Bar mode, which displays the graphical tool bar at the top of the Emacs frame. You can get a description of the mode by typing Ctrl-h m. You will get something like: To get rid of the help window, use Ctrl-x 1.

Important major modes

  1. Fundamental mode (function name: fundamental-mode): Emacs’s default mode with minimal settings and bindings, which we’ve already mentioned.
  2. Text mode (function name: text-mode): a basic mode for text editing.
  3. C mode (function name: c-mode): for editing C programs, a favourite of C programmers.
  4. Wordstar mode (function name: wordstar-mode): this special mode provides you the key-bindings of the old but popular WordStar editor.
  5. Paragraph-Indent Text (function name: paragraph-indent-text-mode): This mode is a special variation of the text mode, where the paragraph-movement commands work for paragraphs whose first lines are indented, and not just for paragraphs separated by blank lines.

Besides these, there are also TeX mode, for editing TeX docs, and lisp-interaction mode, for editing and compiling Lisp code. You can activate any major mode by typing Alt+x followed by the mode’s function name.

Fun with minor modes

As mentioned earlier, minor modes are not restricted to a particular major mode, but can be applied to any major mode. Let’s explore minor modes, with some scenarios to make it interesting.

Scenario 1: If you’re a Java programmer, you have to be familiar with the line System.out.println “text” that’s used to print text to standard output. Tired of typing the same line over and over again?

The solution is a minor mode named Abbrev (function name: abbrev-mode). After you invoke this mode, to assign the abbreviation “sop” for the string “System.out.println”, go to the buffer and type “sop”. Next, type Alt+x inverse-add-global-abbrev. Emacs will ask you to define the expansion for “sop” (screenshot below). Type “System.out.println” and hit Enter. The next time you enter sop in the buffer, it will automatically change it to System.out.println. Finally, it will ask you whether or not to save the defined abbreviation for future sessions.To do away with all the defined abbreviations, use the command Alt+x kill-all-abbrevs.

Defining The Expansion For "sop"

Scenario 2: You want spell-check in the editor

Ispell, an interactive UNIX spelling checker, is built into Emacs, and is a powerful and convenient way to check buffers for misspelled words. To spell-check a word at the cursor, use the ispell-word function, or its key binding, M-$). To spell-check a region, use the ispell-region function. To spell-check a whole buffer, use the ispell-buffer function.

ispell-buffer

Flyspell-mode

Note: ispell gives you a word-by-word spell check . To highlight all the spelling mistakes in the buffer at one time, use the flyspell-mode function.

Scenario 3: You want to automate repetitive invocation of an Emacs command. Suppose you have selected a region that you want to move through 10 spaces. To move it a single space, I’d use Ctrl-x Ctrl-i—but I need to do that 10 times. Hard on the hands and patience!

Emacs provides a way to repeat a command n number of times; in this scenario, I use Ctrl-u 10 Ctrl-x Ctrl-i. This results in the move-region binding being invoked 10 times, doing my job. Ctrl-u is a “universal” binding, and can be used with other key combination to repeated it for a given number of times.

Scenario 4: You want to find a string in the buffer.

Use the various search functions/bindings: isearch-forward will incrementally search the buffer forwards from the cursor. isearch-backward will search backward from the cursor. Ctrl-s or Ctrl-r will show all the matching words in the buffer (performs a non-incremental search)

Well that’s it for my basic introduction to Emacs. I hope you liked it, and found it worth giving Emacs a try. Suggestions and questions are always welcome; feel free to contact me!

Pages : Follow ankurtwi on Twitter      


This is a part of my article which got published in the International Magazine “Linux For You” also popularly know as LFY. The article was published  under the name of “Let’s Play With Emacs  CLI”  in the August 2010 edition. All the work done is under the creative common license as specified in the widgets also :)

Text Editors are very important tool for everyone, especially for the programmers. So let’s play with them :D   I am going to discuss the basics of Emacs text editor in this post.

Emacs was developed by Richard Stallman under the GNU project and he released his Emacs source code under a similar free copyleft software license called the EMACS Public License. C and Emacs Lisp  are the programming languages behind this editor. This editor is also known as “The King Of Editors”. Believe me, the title matches exactly with its unique features. The Emacs environment doesn’t seem intuitive at first glance and it doesn’t work like other editors and word processors but learning Emacs is very easy. The latest version of Emacs is 23.2 . It comes pre installed in most of the distributions. You can install it by typing “sudo apt-get install emacs ” (without quotes) on debian distribution. To install it manually download it from http://ftp.gnu.org/gnu/emacs/ . You can check your version by typing “emacs –version”  in the terminal. Emacs is a cross platform text editor and I have tried it on windows as well. Emcas was designed by keeping in mind that users don’t have to waste their time in clicking mouse buttons (start using Emacs and you will feel the same).

Emacs has two modes – GUI and CLI . Type “emacs” in the terminal to get the graphical mode. Below is the snapshot of the graphical mode :

GUI Mode Of Emacs

Graphical mode can be useful if you are not much familiar with the command line of GNU/Linux. Our main focus in this article is on the CLI mode. You can start the CLI mode by typing “emacs -nw” in the terminal. You should get something like this :

Welcome to CLI mode of Emacs

Start a new file in Emacs by typing “emacs -nw filename”  in the terminal and you should see a blank buffer. Emacs works on buffer system. Any thing you write will be there on your buffer. When you save the buffer, it gets permanently written on your disk. Default buffer in it is known as SCRATCH BUFFER. New file buffer should look something like the screenshot shown below :

New File

At the top is the menu bar like in the GUI interfaces but you hardly use it in the CLI. Below that is your buffer area where you can write your contents and data. Mode line is below the buffer. You can call it as the status bar of the editor. It gives you important information about your Emacs session and about the current buffer displayed in the window above it, including whether your latest changes were saved to disk or not , on what line number the cursor is at, how far into the buffer the bottom of the screen is (in percentage of the whole) and the current active features and settings of Emacs. Mini Buffer is below the modeline where Emacs displays the message relevant to any operations.
You don’t need to use the mouse in Emacs as there are many key bindings (combination of keys) in this editor. Key bindings starts with two most important and common key that are Ctrl and Meta keys. Meta key in case is Alt key or Esc key. I have already told you how to start the Emacs and now going to tell how to exit it because it doesn’t have a normal exit. To exit the Emacs, key combination used is Ctrl-x Ctrl-c. It means press Ctrl key then press x while still holding the Ctrl key and then press c while holding the Ctrl key. It exits the Emacs command line and you will again be in the terminal. In the mini buffer you will see the progress of your command while you are typing it. I am listing some important commands below that will help you to start your work initially  on  Emacs :

Key Combination          Function
C-x C-s                   Saves the content in the buffer
C-x C-f                    Find a file, Start a file by giving location
C-x k                       Kill buffer
C-x b                       Switch between multiple buffer
C-p                          Move the cursor to previous line
C-n                          Move the cursor to the next line
C-f                          Move the cursor one letter forward
C-b                          Move the cursor one letter backward
M-f                         Move the cursor one word forward
M-b                         Move the cursor one word backward
C-a                          Move to beginning of line
C-e                          Move to end of line
M-a                         Move to beginning of sentence
M-e                         Move to end of sentence
C-_                          Undo the change

C = Ctrl, M = Meta (Alt or Esc)
I hope this will be enough for you to start with the emacs. Believe me guys, it’s worth giving a try! In the next part , we will take a look about the different modes and functions used in Emacs and also their usages. Till then, explore more material on internet about it and have fun in the geeks way with Emacs.

Pages : Follow ankurtwi on Twitter      

Follow

Get every new post delivered to your Inbox.

Join 69 other followers

%d bloggers like this: