emacs command in Linux with examples

Introduction to Emacs Editor in Linux/Unix Systems: The Emacs is referred to a family of editors, which means it has many versions or flavors or iterations. The most commonly used version of Emacs editor is GNU Emacs and was created by Richard Stallman. The main difference between text editors like vi, vim, nano, and the Emacs is that is faster, powerful, and simple in terms of usage because of its simple user interface. Unlike the vi editor, the Emacs editor does not use an insert mode, and it is by default in editing mode, i.e., whatever you type will directly be written to the buffer, unless you manually enter command mode by using keyboard shortcuts.

Installing the Emacs Editor:

  • Ubuntu / Debian:
     sudo apt-get install emacs 
  • Redhat / CentOS and Derivatives:
     yum install emacs 

If the above method doesn’t work for you or you want to manually compile emacs, follow these steps:



  • STEP 1: Download the latest version (26.1) of source code from the gnu server with following command:
    curl https://ftp.gnu.org/pub/gnu/emacs/emacs-26.1.tar.gz /emacs/emacs-26.1.tar.gz
  • STEP 2: Extract the tar.gz file.
    tar -zxvf emacs-26.1.tar.gz
  • STEP 3: Install Prerequisites.
    sudo apt-get update
    sudo apt-get install build-essential libgnutls28-dev libncurses-dev
    
  • STEP 4: Install Emacs.
    cd /emacs/emacs-26.1/
    ./configure          #Configure Emacs
    make                 #build components using makefile
    sudo make install    #Install Emacs
    

The above steps will install Emacs into your system. To confirm the install, you can check using terminal using the following command:

emacs --version 

Using Emacs Editor

To use emacs editor, use command – “emacs [-option] [file name]” (without quotation marks) :

Example:

emacs new.txt

Explanation: This command creates a file called new.txt if it doesn’t already exist. If the file with that name already exists, it’s content is copied to the memory buffer and shown at the editing buffer area.

Note: Using the emacs command with no filename opens the default interface of the emacs editor, as shown in the below image. This screen is user-friendly and you can navigate using the link options highlighted in the screen, like the option visit new file creates a new file buffer for you to start writing.

Emacs Common Options:

  1. –file file_name, –find-file file_name, –visit file_name
    This option is used to provide file name to edit. However, in most cases, this is not required and directly file name ca be mentioned.
  2. +number
    The number here specifies the line number in the file which is followed in the command, and the cursor is moved to that line. There should be no space between the number and the + sign.
  3. +line:column
    Here line represents the line number or row and the column represents the number of characters. The cursor is automatically placed to this
    position in the file that is followed.
  4. -q, –no-init-file
    This option prevents Emacs from loading an initialization or init file.
  5. –no-splash
    This option prevents Emacs from showing splash screen at startup.
  6. -u user, –user user
    Load user’s init file.
  7. –version
    To display version and license information.
  8. –help
    Display help.

Note: For more options, you can type “man emacs” or “emacs --help” without the quotation marks.

Emacs – Common Keyboard Shortcuts

  1. General Shortcuts:
    • ctrl-x ctrl-f : Find file or Open a file. This command prompts for a file name and opens it in buffer for editing. Also, it creates a new file if it doesn’t already exist.
    • ctrl-x ctrl-s : Save File. This saves the current buffer content to the file.
    • ctrl-x ctrl-w : Write to file. This command prompts for a file name to save buffer.
  2. Copy, cut and paste shortcuts:
    • ctrl-d : Cut the character at the position of cursor.
    • ESC d : Cut the word till next blank space from the current position.
    • ctrl-k : Cut till end of the line from current position.
    • ctrl-@ : Mark the current position as beginning for copy.
    • ESC w : copy area between mark and cursor to paste.
    • ctrl-y : Yank or Paste the recently copied or cut characters at the current position of cursor.
  3. Search and Replace:

    • ctrl-s : Search forward- prompts for a search terms and search it in the buffer from current cursor position to the end of the buffer.
    • ctrl-r : Search backwards/reverse- prompts for a search term and search from current position to the beginning of the buffer.
    • ESC % : Replace- prompts for a search term and a replacement term and replaces the first occurrence of the word in buffer after cursor.
  4. Moving cursor:
    • ctrl-a : Beginning of the line.
    • ctrl-e : End of line.
    • ctrl-f : Move forward by one character.
    • ctrl-b : Move back by one character.
    • ctrl-n : Move cursor to next line.
    • ctrl-p : Cursor to previous line.
    • ESC > : End of the buffer.
    • ESC < : Starting of the buffer.
    • ESC f : Move forward by one word.
    • ESC b : Move back by one word.
  5. Miscellaneous:
    • ctrl-z : Stop Emacs and quit immediately without confirmation(All changes in buffer are lost).
    • ctrl-g : Cencel current command and revert back from command mode.
    • ctrl-x u : undo the last command.
    • ctrl-x ctrl-c : Save and quit.
    • ctrl-h i : Help in Emacs- describes emacs shortcuts and commands.

Help page inside emacs:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.