0% found this document useful (0 votes)
17 views21 pages

CLI Editors 2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views21 pages

CLI Editors 2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

CLI Editors

Type 📒 Lecture
Date @February 22, 2022

Lecture # ?

Lecture
URL

Notion https://21f1003586.notion.site/CLI-Editors-
URL 77dfbc4e33a9426498d94207eaea1962

Week # 5

Command line editors


Working with text files in the terminal

CLI Editors 1
Features
Scrolling, view modes, current position in the file

Navigation (char, word, line, pattern)

Insert, Replace, Delete

Cut-Copy-Paste

Search-Replace

Language-aware syntax highlighting

Key-maps, init scripts, macros

Plugins

ed

CLI Editors 2
Command Format → [starting-address[,ending-address]command[command-parameters]]

To locate the cursor on any particular line of the file ...

We can use the line number itself

press 2 → in the 2nd line of the text file

press . (dot) → referring to the current line the cursor is

press $ → refers to the last line

press % → refers to all the lines, i.e. any action we are doing applies to all the
lines

press + → line after the cursor

press - (minus sign) → line before the cursor

press , (comma) → represent the entire buffer, i.e. the whole file

press ; (semicolon) → refers the end of the text file from the current position

/RE/ → To match a specific Regular Expression in the file

A way to invoke the ed editor


ed file.txt

and it shows the number of bytes in the file, instead of the contents of the file ... ayo
wut

CLI Editors 3
CLI Editors 4
CLI Editors 5
Run a bash command, read the bash command’s output and write it to the file

Delete the last line of the file and write the changes to the file

CLI Editors 6
Append a line to the file (press . then enter to exit)

Search and Replace

File name

CLI Editors 7
Print the current line

Append to the current line

Join line 5 and 6 (The command is 5,6j )

Move the current line after the given line # (We are moving the current line below
line 1 here)

Undo the previous operation

CLI Editors 8
Add a prefix to all the lines and then modify the prefix on some lines

ed/ex commands

CLI Editors 9
readlink

Prints the resolved symbolic links or canonical file names, but what does that mean?
If we have file which is a symbolic link to file which in turn is another symbolic link to
a file and so on ...

The readlink command will display the actual file the initial symbolic link is referring
to

Example usage
readlink -f /usr/bin/pico

Output
/usr/bin/nano

.bashrc

It is a config file that is read by the bash shell everytime it opens

nano

nano is a text editor, example syntax is nano filename

It also does syntax highlighting

CLI Editors 10
CLI Editors 11
CLI Editors 12
CLI Editors 13
CLI Editors 14
CLI Editors 15
Source: https://www.nano-editor.org/dist/latest/cheatsheet.html

Modes in vi editor

i will insert the characters from the current position of the cursor

o will insert a new line

CLI Editors 16
a will append the text

vi help
These work only in the Command mode

Press Esc to enter this mode

To exit out of vi

:w → write out

:x → write out and quit

:wq → write out and quit

:q → quit (if write out is over)

:q! → ignore changes and quit

If nothing else works, cry and smash your keyboard, and then rage quit

vi command mode
Screen manipulation

Ctrl + F → Scroll forward one screen

Ctrl + B → Scroll backward one screen

CLI Editors 17
Ctrl + D → Scroll down half screen

Ctrl + U → Scroll up half screen

Ctrl + L → Redraw screen

Ctrl + R → Redraw screen removing deleted stuff

Moving around

0 → Start of the current line

$ → End of the current line

w → Beginning of the next word

b → Beginning of the preceding word

:0 → First line in the file

1G → First line in the file

:n → n-th line in the file

nG → n-th line in the file

:$ → Last line in the file

G → Last line in the file

Changing text

r → Replace a single character under the cursor

R → Replace characters from the cursor till Esc

cw → Change word under the cursor, from the current character till Esc

cNw → Change N words, from the current character till Esc

C (CAPITAL C) → Change characters in the current line till Esc

cc (small c) → Change the line till Esc

Ncc → Change the next N lines, starting from the current till Esc

Deleting text

x → Delete a single character under the cursor

Nx → Delete N characters from the cursor

dw → Delete one word, from the character under the cursor

CLI Editors 18
dNw → Delete N words, from the character under the cursor

D → Delete the rest of the line, from the character under the cursor

dd → Delete the current line

Ndd → Delete the next N lines, starting from the current one

Copy and Paste text

yy (small y) → Copy the current line to the buffer

Nyy → Copy the next N lines, including the current, into the buffer

p → Paste buffer into the file after the current line

u → Undo the previous action

Searching text

/string → Search forward for the given string

?string → Search backward for the given string

n → Move the cursor to the next occurance of the string

N → Move the cursor to the previous occurrance of the string

:se nu → Set line numbers

:se nonu → Unset the line numbers

To copy a file using secure copy scp

Syntax → scp <username>@<IP_ADDRESS>:<path/to/file/on/that/machine> <where/to/save>

Example → scp [email protected]:Documents/code3d.tar .

To untar (extract) a .tar file

Syntax → tar -xvf filename.tar

emacs

C-x means Ctrl + x

M-x means Alt + x

Moving around

C-p → Move up by one line

C-b → Move left by one character

C-f → Move right by one character

CLI Editors 19
C-n → Move down by one line

C-a → Go to the beginning of the current line

C-e → Go to the end of the current line

C-v → Move forward one screen

M-< → Move to the first line of the file

M-b → Move left to the previous word

M-f → Move right to the next word

M-> → Move to the last line of the file

M-a → Move to the beginning of the current sentence

M-e → Move to the end of the current sentence

M-v → Move back one screen

Source: https://www.gnu.org/software/emacs/refcards/pdf/refcard.pdf

emacs commands
Exiting emacs

C-x C-s → Save buffer to the file

C-z → Exit emacs but keep it running

C-x C-c → Exit emacs and stop it

Searching a text

C-s → Search forward

C-r → Search backward

M-x → Replace string

Copy and Paste

M-backspace → Cut the word before the cursor

M-d → Cut the word after the cursor

C-k → Cut from the cursor to the end of the line

M-k → Cut from the cursor to the end of the sentence

C-y → Paste the content at the cursor

CLI Editors 20
Syntax for emacs → emacs -nw filename

Make sure to pass the -nw flag to make it open in terminal mode instead of the GUI

CLI Editors 21

You might also like