emacs command in Linux with examples - GeeksforGeeks

15 May, 2019
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 
 sudo apt-get install emacs 
Redhat / CentOS and Derivatives: yum install emacs 
 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
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
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

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

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 
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:
–file file_name, –find-file file_name, –visit file_nameThis option is used to provide file name to edit. However, in most cases, this is not required and directly file name ca be mentioned.+numberThe 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.+line:columnHere line represents the line number or row and the column represents the number of characters. The cursor is automatically placed to thisposition in the file that is followed.-q, –no-init-fileThis option prevents Emacs from loading an initialization or init file.–no-splashThis option prevents Emacs from showing splash screen at startup.-u user, –user userLoad user’s init file.–versionTo display version and license information.–helpDisplay help.
–file file_name, –find-file file_name, –visit file_nameThis option is used to provide file name to edit. However, in most cases, this is not required and directly file name ca be mentioned.
+numberThe 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.
+line:columnHere line represents the line number or row and the column represents the number of characters. The cursor is automatically placed to thisposition in the file that is followed.
-q, –no-init-fileThis option prevents Emacs from loading an initialization or init file.
–no-splashThis option prevents Emacs from showing splash screen at startup.
-u user, –user userLoad user’s init file.
–versionTo display version and license information.
–helpDisplay help.
Note: For more options, you can type “man emacs” or “emacs --help” without the quotation marks.
Emacs – Common Keyboard Shortcuts
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.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.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.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.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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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:
linux-command
Linux-misc-commands
Picked
Linux-Unix
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
TCP Server-Client implementation in C
ZIP command in Linux with examples
tar command in Linux with examples
curl command in Linux with Examples
UDP Server-Client implementation in C
Conditional Statements | Shell Script
Cat command in Linux with examples
Tail command in Linux with examples
touch command in Linux with Examples
Mutex lock for Linux Thread Synchronization