A text editor on Linux, similar to Microsoft Word, Visual Studo Code on Windows, is a must-have application. Most of them are all GUI-based. However, this posts will introduce command line text editor for Linux.

Common Text Viewer (DO NOT Support Edit Function):

  • cat
  • more
  • less
  • head
  • tail

Common Text Editor:

  • nano
  • vi / vim
  • emacs

The posts will only present part of common and useful functions of the editors, please click the related links to learn more!

meme

less Viewer

less

Cursor Movement

Keyboard ShortcutDescription
SPACEforward one window
bbackward one window
jmove on the next line
kmove on the previous line
10jmove on the next 10 line
10kmove on the previous 10 line
Ggo to the end of file
ggo to the start of file
qexit the less pager

Search

Keyboard ShortcutDescription
/search for a pattern which will take you to the next occurrence
nfor next match in forward
Nfor previous match in backward

VI/VIM Editor

Three VI/VIM edit mode:

  • Command Mode (Default)
  • Insert Mode /** Press ESC return to Command Mode **/
  • Visual Mode /** Press ESC return to Command Mode **/

VI/VIM Cheat Sheet

Command Mode

Cursor Movement

Keyboard ShortcutDescription
/ kMove up one line
/ jMove down one line
/ hmove left one character
/ lmove right one character
^Go to the begining of this line
$Go to the end of this line
ggGo to the first line of the document
GGo to the last line of the document
w / eJump forware of a word
(w will jump to the start of a word,ewill jump to the end of a word)
:nJump to line n (n is a specifc integer number)
:set muShow line numbers

Save the file and Exit the Editor

Keyboard ShortcutDescription
:wSave
:wqSave and Quit
:qQuit (Fails if there are unsaved changes)
:q!Force Quit
:wq!Force Save and Quit

Delete/Cut/Undo/Redo

Keyboard ShortcutDescription
xDelete (cut) character
ddDelete (cut) the line
dwDelete (cut) the word
uUndo
5uUndo 5 times
Ctrl + rRedo

Copy/Paste

Keyboard ShortcutDescription
yy复制行
p在光标粘贴
P在光标粘贴

Insert Mode

Enter insert mode

Keyboard ShortcutDescription
iInsert at cursor
IInsert at the begining of the line
aAppend after the cursor
AAppend at the end of the line
oOpen a new line below the current line
OOpen a new line above the current line

Visual Mode

Keyboard ShortcutDescription
vstart visual mode
Vstart linewise visual mode
Ctrl + vstart visual block mode

Some other useful techniques

Select copy contents

vi-select-copy-content

Press v start visual mode, move the cursor to select the contents, then press y copy, move the cursor to where you want to paste, press p.

Comment mutiles lines of code

vi-comment-mutiple-lines

Using Ctrl + v start visial block mode, select all the lines you want to comment. Then, press capitalized I insert the begining of the line, put whatever // or #. Press ESC twice.

Manipulate all characters inside brackets/quotes

Delete all characters inside brackets/quotes:

something="This is a string." --> something=""
(123456) --> ()
  1. Move the cursor inside the brackets/quotes under the normal/command mode
  2. Press keyborad shortcut: d + i + brackets/quotes, for example, remove all characters inside the quotes is d + i + "

Explain: d is for delete, i is define a range, then brackets/quotes is a flag.

Copy all characters inside brackets/quotes:

  1. Move the cursor inside the brackets/quotes under the normal/command mode
  2. Press keyborad shortcut: y + i + brackets/quotes, for example, copy all characters inside the quotes is y + i + "

Customize Vim (Plugins)

Vim editor is already powerful enough out of the box, but we can install some plugins to personalize it.

Notice: Installing to many plugins will effect the editor performance.

vim-plug

vim-plug

vim-plug is a minimalist plugin manager

Unix/Linux Installation

curl -fLo ~/.vim/autoload/plug.vim --create-dirs \
    https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim

For other system environment, please refer vim-plug GitHub readme.

vim-airline

vim-airline

vim-airline is a lean & mean status/tabline for vim that's light as air.

Installing vim-airline (and themes) is adding the following to .vimrc:

call plug#begin('~/.vim/plugged')
Plug 'vim-airline/vim-airline'
Plug 'vim-airline/vim-airline-themes'
call plug#end()

Then, run:

:PlugInstall

Other Configurations:

(Optional: you can put them into .vimrc)

"----------Plug vim-airline/vim-airline configuration----------
let g:airline#extensions#tabline#enabled = 1 " Enable the list of buffers
if !exists('g:airline_symbols')
    let g:airline_symbols = {}
endif
let g:airline_symbols.colnr = ' ' "Fix the status bar text display problem
" ~/full/path-to/file-name.js
let g:airline#extensions#tabline#formatter = 'default'  " f/p/file-name.js
let g:airline#extensions#tabline#formatter = 'jsformatter' " path-to/f
let g:airline#extensions#tabline#formatter = 'unique_tail' " file-name.js
let g:airline#extensions#tabline#formatter = 'unique_tail_improved' " f/p/file-name.js

For more settings, please visit Getting started with vim-airline.

Related Posts

References

Vim Cheat Sheet

边用边学Vim —— 整体操作括号/引号内的字符

Getting started with vim-airline