Skip to main content

Text Editor

In practical development, besides knowledge of Linux, developers often need to modify system configuration files and edit code. Proficiently using editor tools can help developers improve their development speed and programming efficiency. This chapter will introduce Vim and Nano editors, as well as GCC and other code compilers. These tools come pre-installed on Lyra, and users can directly use them.

1. Nano Editor

Nano is a simple and user-friendly terminal text editor, suitable for beginners or developers with limited customization needs for the editor. It provides basic editing functions such as insert, delete, copy, and paste, along with a straightforward interface and easy-to-use operations. Nano is convenient for quick editing or fixing code, as well as editing configuration files and other small tasks. Although Nano's features are relatively simple, due to its simplicity and ease of use, it remains a preferred choice for developers in certain scenarios.

To start Nano, simply enter "nano" in the terminal. Alternatively, you can open a specific file by adding its path after the command. If the file does not exist, Nano will create it. For example, in the Raspberry Pi terminal, typing "sudo nano luckfox.txt" will open the "luckfox.txt" file for editing, just like using a notepad.

  1. The top line displays the version of the Nano editor and the name of the currently edited file. The bottom two lines show the most commonly used shortcuts in the editor, with the "^" symbol representing Ctrl and "M-" representing Alt.
  2. Common shortcuts:
  • File Management

    Ctrl+S  Save
    Ctrl+O Save As
    Ctrl+X Exit Nano
    Ctrl+R Insert File into Buffer
    Ctrl+G Get Help
  • Editing

    Alt+N   Toggle line numbering on/off
    Shift+↑ or ↓ Select lines upward or downward
    Alt+3 Comment or uncomment selected lines or block
    Alt+U Undo
    Alt+E Redo
  • Search

    Ctrl+Q  Start backward search
    Ctrl+W Start forward search
    Alt+Q Search for the next match backward
    Alt+W Search for the next match forward

2. Vim Editor

Vim is a highly customizable text editor widely used by developers for coding work in a terminal environment. Vim boasts powerful editing features and an extensive plugin ecosystem, enabling developers to efficiently edit and organize code. It supports multiple programming languages and provides abundant shortcuts and commands for quick navigation, editing, and debugging of code. Due to its high customizability and powerful features, Vim enjoys broad application and high regard among developers. The following is a simple introduction to its usage and common commands.

For convenience, the following three lines are usually added to the Vim configuration file:

set nu        # Show line numbers
syntax on # Enable syntax highlighting
set tabstop=4 # Set tab width to four spaces

2.1 Vim Modes

Basically, Vim can be divided into three modes: Command mode, Insert mode, and Last line mode:

  • Command mode: Controls the movement of the screen cursor, deletion of characters, words or lines, and copying/moving of certain sections
  • Insert mode: Allows inputting characters and editing the file
  • Last line mode: Used for saving or exiting Vim, as well as configuring editing settings like searching for strings or displaying line numbers

2.2 Common Commands

  1. Opening, Saving, and Closing Files (vi command mode):

    vim filenameOpen the "filename" file
    :wSave the file
    :qExit the editor; use the following command if the file has been modified
    :q!Exit the editor without saving
    :wqExit the editor and save the file
    :wq!Force exit the editor and save the file
    ZZExit the editor and save the file
    ZQExit the editor without saving
  2. Inserting Text or Lines (vi command mode; after executing the following command, it enters insert mode, and pressing ESC exits insert mode):

    aAdd text to the right of the current cursor position
    iAdd text to the left of the current cursor position
    AAdd text at the end of the current line
    IAdd text at the beginning of the current line
    OCreate a new line above the current line
    oCreate a new line below the current line
    RReplace (overwrite) text at the current cursor position and beyond
    JMerge the current line with the next line (still in command mode)
  3. Deleting and Undoing Characters or Lines (vi command mode):

    xDelete the current character
    nxDelete n characters starting from the cursor
    ddDelete the current line
    nddDelete n lines, including the current line
    uUndo the previous action
    UUndo all changes to the current line
  4. Copying and Pasting (vi command mode):

    yyCopy the current line to the clipboard
    nyyCopy n lines starting from the current line to the clipboard
    ywCopy from the cursor to the end of the word to the clipboard
    nywCopy n words starting from the cursor to the clipboard
    y^Copy from the cursor to the beginning of the line to the clipboard
    y$Copy from the cursor to the end of the line to the clipboard
    pPaste the content from the clipboard after the cursor
    PPaste the content from the clipboard before the cursor