Skip to content

Linux Editors

Nano

  • Simple command line text editor
  • Not installed by default
  • No options for advanced editing
[ntc@ntc ~]$ nano change_notes.txt

Nano

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 nano to edit files

vim or the "VI Improved" Editor

  • vim is the upgraded version of the default vi editor
  • 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:

[ntc@ntc ~]$ vim configs/nyc-rtr01.cfg

Vim

  • By default opens in "normal mode"
  • h, j, k and l keys 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

  1. Type the ESC key. This will tell the editor that the context is being changed.

  2. Now type the : key. Now your cursor will be blinking at the bottom of the file.

  3. Type the q or q!(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

  1. Type the ESC key. This will tell the editor that the context is being changed.

  2. Now type the : key. Now your cursor will be blinking at the bottom of the file.

  3. 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

Vim

Editing text

  • From normal mode, use the i key to enter the INSERT mode. In this mode you can edit text by simply typing the keys.

  • To delete a character:

    1. Exit the INSERT mode by pressing the ESC key.
    2. Navigate to the character
    3. Use the x key to delete it
  • To undo an edit, from the normal mode, type the u key. Similarly, to redo an edit, use the CTRL-r key 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 cw for "Change Word". This will switch the editor automatically to the INSERT mode

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

Vim

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 n key 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 /g implies "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 yy or "yank"
  • To copy multiple lines, go to the starting line and then prepend yy with the number of lines to yank. For instance, 3yy will yank or copy 3 lines.
  • To cut, use the same methodology as copy. Except use the dd key combination rather than yy
  • In either case, to paste the line copied, navigate to the desired location and then use the p key.

Lab Time

  • Lab 6 - Using vim for basic editing.

Visual mode editing, whitespaces

TABs vs SPACEs

Silicon_valley

  • Extremely important for "indentation sensitive" tools like YAML and Python
  • Hard to tell visually whether whitespace is a TAB or a SPACE

In normal mode, set list . This is a boolean operator that turns-on or off whitespace identifiers. (Default off)

`:set list` displays whitespace, while `:set nolist` displays normally
  • Optionally use set listchars to specify how to display TABs

  • By default TABs are identified as ^I characters.

Visualizing TABS

Note line #4 and #16-20

Vim

Note line #4 and #16-20

Vim

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 :
  • v to enter visual mode
  • CTRL-v to enter visual "block" mode - for rectangular selections
  • SHIFT-v to 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

Vim

Use SHIFT-i to insert block

Vim

Lab Time

  • Lab 8 - Working with whitespaces

VS Code

VS Code is feature rich, extensible editor. We will highlight a few challenges.

Shortcuts

  • ⇧⌘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 with code .
  • 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 no to the start of each line in tests/unit/mock/config/compliance/diff_network_config/cisco_ios/ios_basic_base.txt
  • In netutils.interface.py take the interface_range_compress function, and indent the entire function code
    • Return that code to normal
  • In netutils.interface.py comment out sort_interface_list function
  • Using Cmd+D, replace every error message of raise ValueError with raise ValueError("You have an error.") in netutils.interface.py
  • Remove every occurrence of is_ in all Python files.
    • e.g. is_valid -> valid