DEV Community

Angel Oduro-Temeng Twumasi
Angel Oduro-Temeng Twumasi

Posted on • Updated on

💻Mastering Emacs and Vi: A Beginners Guide

Introduction

Emacs and Vi are two editors which are very popular with terminal usage. While Emacs comes preinstalled with MacOS, Vi comes preinstalled with most Linux distros. If you use windows, you might have to download them to be able to use them.

Uses of emacs

  • For text editing and manipulation.
  • Could be used as an IDE for programming languages.
  • It comes with version control systems which makes it versatile for software development.

Uses of Vi

  • It is the preferred editor for terminal based tasked and hence used extensively.
  • Suitable for editing files on remote servers via SSH.
  • Its modal of editing allows for efficient and rapid editing once mastered.

Both Emacs and Vi have two identical modes. These are:

  1. The command mode
  2. The insert mode

Switching between modes

  • Press ESC key to enter into command mode
  • Press i to enter into insert mode ie you can now insert data

Basic commands in Emacs:

Opening files

  • C-x C-f : Open file
  • C-x C-s : Save current buffer
  • C-x C-w : Save current buffer with a different name

Moving commands

  • C-a : Move the cursor to the beginning of the line
  • C-e : Move the cursor to the end of line

Editing commands

  • C-x u : Undo the last change
  • C-y : Paste the most recently cut region
  • C-w : Cut the selected region (to select a region do C- or Ctrl + space then C-f for the highlights)_
  • M-w : Copy the selected region
  • C-d : Delete character at the cursor

Others

  • C-s : Search forward for a pattern
  • C-r : Search backwards for a pattern

  • C-x C-c : Quit Emacs (you'll be prompted for confirmation)

Note that in Emacs, C could be interpreted as Ctrl and M could be interpreted as Alt

Basic commands in Vi

Opening a file

  • Vi <filename> to open the file in Vi

Editing

  • x : Delete the character at the current cursor position
  • yy : Copy the current line
  • dd : Cut the current line
  • u : Undo the last change
  • p : Paste the recently cut or copied item

Moving commands

  • h : Move cursor to the left
  • j : Move cursor one line down
  • k : Move cursor one line up
  • l : Move cursor to the right
  • G : Move cursor to the last line of the file
  • gg : Move cursor to the first line of the file
  • 0 : Move cursor to the start of current line
  • $ : Move cursor to the end of current line

Others

  • :w : Save the file
  • :q : Quit Vi

  • :x : Quit Vi after writing out modified changes. If there are no modified changes, it just quits Vi

  • :wq : Save and Quit

  • :q! : Quit without saving

In conclusion, I hope you found this guide to Emacs and Vi helpful in enhancing your text editing skills. Remember to subscribe to stay updated with more informative content like this. Don't forget to hit the like button if you found it valuable! Share your favorite and most frequently used command or the one you're planning to use in the comments below. Your engagement and feedback are greatly appreciated. Happy editing 😎

Follow me on Twitter and LinkedIn

Top comments (0)