logo

Text Editors

The scope of this module is to present a few editors and some basic commands that can be used for command line editing.

Introduction

It is useful to learn a command line editor for quick edits without the need to copy files back and forth. Just do your edits and run the code. After a while you may prefer command line editing as the tools are easily available for any OS. Unfortunately, there are many options which have spawned many arguments on the internet, such as, the Vi/Vim versus Emacs editor war. Realistically, the only right answer is the one that works best for you. I will not go into details about all of the editors but talk about the two main ones that you will come across.

Personally I use [vis](https://github.com/martanne/vis/), because I like the structural regular expression support, but just waiting for language server protocol support (LSP) for larger projects. For larger projects I am using *nvim + lspconfig* via *packer* for code completion and checking. I will post another article on setting this up.
Editor Description
vi A visual modal editor, part of POSIX standard, everywhere
emacs GNU project Emacs editor, very feature rich
jed An easy to use emacs style edtior, has menus for ease of learning

All editors have configuration files that can be tailored to have features enabled and disabled to your liking.

Editor Config Location
vi ~/.exrc or ~/.nexrc
vim ~/.vimrc
nvim ~/.config/nvim/init.vim or for lua configs ~/.config/nvim/init.lua
emacs ~/.emacs, ~/.emacs.el or ~/.emacs.d/init.el

There are many guides online for tailoring your editor to your needs

vi - vi/vim/nvim

I recommmend the Vim/Neovim tutor. In command mode use :Tutor to start. You can use this page as quick command reference.

One editor you will hear about is vi or variants vim, neovim/nvim. It's main advantage is quick navigation and editing with minimal keystrokes. Vi's design is from a time where arrow keys never existed to put that into perspective. It follows the philosophy that the users fingers should not have to travel far from home row to insert commands.

I will assume that users will be using vim or neovim/nvim on the terminal, as these have more modern features available. nvim automatically detects syntax rules for python files and others; it also has most common features enabled by default. For the remaining portion of this document I will use vi to mean vim and nvim interchangably unless explicitly specified.

vi - insert mode

Once in insert mode (i from command mode) you can type text like normal. In traditional vi you cannot use the arrow keys. You must go to command mode to navigate. However, in vim and nvim the arrow keys can be used to navigate. You can return to command mode by the ESC key.

vi - Command mode

Within vi if the user hits ESC they will be put into command mode; where you can input specific keystrokes to perform tasks, e.g., like copy/yank, paste, delete, etc… The commands are typically easy to remember with mnenomics and uses some shortcuts from regex (, $). Listed below are some common commands.

Command Description
:q quit
:q! Force quit
:w write/save
:wq write/save and quit
:help Get help
i insert mode at the cursor, ESC back to command mode
a append/insert mode after the cursor
o open insert mode on new line below
yy yank or copy line
nyy yank n lines from the cursor position downwards
ynw yank n words from the cursor position forwards
y$ yank from current position to end of line
p paste
dd delete/cut line
ndd delete/cut n lines from the cursor position downwards
dnw delete/cut n words from the cursor position forwards
d$ delete/cut from current position to end of line
x delete character under cursor
^ Go to beginning of line
$ Go to end of line
hjkl similar to arrow-keys. hjkl <-> Left,Down,Up,Right
/TEXT Search forward for *TEXT string
?TEXT Search backward for *TEXT string
n find forward matched search item
N find backward matched serarch item
m[a-z] mark cursor positon with bookmark [a-z]
\[a-z]` jump back to mark bookmark [a-z]

Vi's greatest strength is it's intuitive command language with verbs and nouns like d5w which deletes 5 words from the cursor position. This works with y as well.

BACKSPACE doesn't work in original vi, however, it works in vim and nvim. For deleting sections you should look up help :help for the mark command m or use v mode in vim and nvim

To delete/cut sections use m[a-z], e.g., ma marks with the refrence a. Move the cursor to some position and type d\a, this deletes text from the current cursor position to the reference **a**. This also works withy`.

This is more intuitive by visual mode v in vim and nvim—see below. Hit v then using arrow keys move cusor to where you want, then hit d do delete/cut section of text

Some extra shortcuts that can be helpful

Extra Description
I Insert mode at beginnng of line, same as ^i
A Append, insert mode at end of line, same as $a
O Open insert mode on new line above, same as k^o
w move forward next word
b move backward one word
e move forward to end of word
f[char] move cursor forward to first match of character
F[char] move cursor backward to first match of character

You can do string replacements on a line using :s/RE/replacement/flags, where RE is a pattern or regular expression, and flags can be for number of repeats N or gloabal g. Or globally to the file using %s such that :%s/RE/replacement/flags or within certain line numbers :m,ns/RE/replacement/flags, where m and n are line numbers. The :s commands are similar to the sed command seen in the Regular Experssions (regex) module.

Vim and Neovim/nvim

Vim and Neovim add many additional commands to vi and make it more convienient to work in commandline like emacs, the visual mode v makes things easier for selecting, copying/deleting text. Here are two common ones.

Command Description
v visual mode for selecting text, use with y or d
zi invert code folds if applicable nvim default, vim by config
:Tutor a vim tutor to learn vim, highly recommended

Useful configuration options

For regular vi this config file enables the ruler to see line number, column position, displays current mode (COMMAND or INSERT) and extended regular expressions ERE

$ cat ~/.exrc
# For regular vi compatible editors
set ruler
set showmode
set extended

Vim and Neovim can be configured with different languages and different syntax styles and options for their respective configuration files. A useful feature to add to Vim is syntax on for syntax highlighting if you like that. Neovim enables this by default.

Vim and Neovim/nvim have their own definition of regex which is a hybrid of BRE, ERE and functionality similiar to PCRE; they use \( \) and \{ \} like BRE.

Emacs (and jed)

To be honest, I am not familiar with emacs but will give a few commands to make coding easier. Emacs does not use modes to enter commands or insert text. You can always insert text; this is the same as modern text editors you see on the desktop and should be familiar. Jed is a stripped down version of emacs and great for beginners. It however uses it's own set of shortcuts that are not the same as you may be used too. The C- symbol represents holding CTRL on the keyboard and M- represents hold ALT.

Commands Description
C-x C-s Save text file
C-x C-c Quit text file
C-a Go to beginning of line
C-e Go to end of line
M-f Go forward one word
M-b Go backward one word
C-SPACE Select text, use arrow keys to select more
C-w Cut text
M-w Copy text
C-y Paste text
C-s Enter search mode of text ENTER to quit
C-M-s Find forward matching pattern
C-M-r Find backward matching pattern

Some short cut commands like C-a

I think emacs is more analogous to modern text editors and can be easier to pick up. There is a plethora of commands available to use and many features to make coding easier. It also is able to browse the web, check e-mails, play games and read the news—no joke—if you want to learn more man emacs or read the GNU Emacs tour.

As a result, I think that using jed is much more accessible commandline editor. It has menus to make learning the shortcuts easy, or selectable if you forget. I prefer it to the more basic editor nano or pico because you can selectblocks of text more easily.

One reason I don't use emacs is beause my window manager uses ALT keys for commands, which conflict with emacs. I could redefine all my shortcuts but that is a lot of work that I don't want to do just to use a different editor. Plus I am happy with plain old regular vi. Also, vi is part of the POSIX standard and will be on these systems, otherwise you have to manually install the editor. Linux systems tend to put vim on instead of vi.

© 2017–2022 David Kalliecharan