Linux Editors¶
Nano¶
- Simple command line text editor
- Not installed by default
- No options for advanced editing

Important shortcuts:
CTRL-n : Next line
CTRL-a : Beginning of the line
CTRL-g : Invoke help
ALT-r : Search and replace
CTRL-o : Save
CTRL-x : Exit
Lab Time¶
- Lab 5 - Use
nanoto edit files
vim or the "VI Improved" Editor¶
vimis the upgraded version of the defaultvieditor- Very powerful and feature rich
- Extensible behavior using popular/well-known plugins
- Context based (Edit mode, Visual mode, Normal mode etc)
Invoked at the command prompt as follows:

Navigation¶
- By default opens in "normal mode"
h,j,kandlkeys are used to move the cursor left, up, down and right- Arrow keys will be discouraged by vim purest, but feel free to safely use.
In the normal mode:
w : moves the cursor forward one word at a time
b: moves the cursor backward one word at a time
$: moves the cursor to the end of the line
^: moves the cursor to the first character of the line
SHIFT-g: moves the cursor to the bottom of the page
Exiting vim¶
-
Type the
ESCkey. This will tell the editor that the context is being changed. -
Now type the
:key. Now your cursor will be blinking at the bottom of the file. -
Type the
qorq!(force) keys to exit the file.
vim has been subjected to ( an unfairly) number of jokes about leaving users stranded after opening a file.

Enabling line numbers¶
-
Type the
ESCkey. This will tell the editor that the context is being changed. -
Now type the
:key. Now your cursor will be blinking at the bottom of the file. -
Type
se nu; short for "set numbers" to enable line numbers.
To go to a particular line you can simply type the line number after steps 1 and 2
Enabling line numbers¶

Editing text¶
-
From normal mode, use the
ikey to enter theINSERTmode. In this mode you can edit text by simply typing the keys. -
To delete a character:
- Exit the
INSERTmode by pressing theESCkey. - Navigate to the character
- Use the
xkey to delete it
- Exit the
-
To undo an edit, from the
normalmode, type theukey. Similarly, toredoan edit, use theCTRL-rkey combination -
To replace an entire word, navigate to the start of the word (or position from which you would like to make the change) and then type
cwfor "Change Word". This will switch the editor automatically to theINSERTmode
From normal mode, type ESC-: and then w to save or "write" a file. If creating a new file use w followed by the new file's name.
INSERT mode¶

Finding and replacing text¶
- From normal mode, use the
/key to invoke a search followed by the text to find - To look for repeated occurrences, type the
nkey for "next match"
To find and replace use the following steps:
1. From normal mode, type ESC-: followed by:
2. %s/ [Word to replace] / [replacement word] /g
The
/gimplies "make the replacements globally for all matches in the file"
Copying, cutting and pasting¶
- To copy a line from normal mode, navigate to the line and type
yyor "yank" - To copy multiple lines, go to the starting line and then prepend
yywith the number of lines to yank. For instance,3yywill yank or copy 3 lines. - To cut, use the same methodology as copy. Except use the
ddkey combination rather thanyy - In either case, to paste the line copied, navigate to the desired location and then use the
pkey.
Lab Time¶
- Lab 6 - Using vim for basic editing.
Visual mode editing, whitespaces¶
TABs vs SPACEs¶
- Extremely important for "indentation sensitive" tools like
YAMLandPython - Hard to tell visually whether whitespace is a
TABor aSPACE
In normal mode, set list . This is a boolean operator that turns-on or off whitespace identifiers. (Default off)
-
Optionally use
set listcharsto specify how to display TABs -
By default TABs are identified as
^Icharacters.
Visualizing TABS¶
Note line #4 and #16-20

Note line #4 and #16-20

Visual mode editing¶
- Used to visually select a block or region of text within vim
- Operations can then be restricted to this block
- Invoked by typing :
vto enter visual modeCTRL-vto enter visual "block" mode - for rectangular selectionsSHIFT-vto enter visual "line" mode - for entire line selection
Typical use cases: - Commenting/un-commenting out a block of code - Indenting a region or block of text
Commenting out a block of code¶
Use CTRL-v to start selection

Use SHIFT-i to insert block

Lab Time¶
- Lab 8 - Working with whitespaces
VS Code¶
VS Code is feature rich, extensible editor. We will highlight a few challenges.
- ⇧⌘P or F1 >> "Show Command Palette"
- Search for "Shell Command", install in VS Code
- From terminal, can now open the open a folder with the
code {folder}command most commonly on the current folder withcode .
- f8 - go to next error
- Cmd+X - remove word or remove line, depending on context
- Cmd+D - Add selection to next Find match
- Cmd+Option (and arrow) - Up and down multi line
- fn+option - Select and Grabbing
- Find and replace for files
- Cmd+] - Indent selection
- Cmd+[ - De-Indent selection
- Cmd+/ - Comment selection
- Cmd+Option+Select(downarrow chevron) - Collapse / Uncollapse all collapsible sections
Lab:¶
Create a local copy of netutils, e.g. git clone https://github.com/networktocode/netutils
- Add
noto the start of each line intests/unit/mock/config/compliance/diff_network_config/cisco_ios/ios_basic_base.txt - In
netutils.interface.pytake theinterface_range_compressfunction, and indent the entire function code- Return that code to normal
- In
netutils.interface.pycomment outsort_interface_listfunction - Using
Cmd+D, replace every error message ofraise ValueErrorwithraise ValueError("You have an error.")innetutils.interface.py - Remove every occurrence of
is_in all Python files.- e.g.
is_valid->valid
- e.g.