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.
- 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.
- Common shortcuts:
File Management
Ctrl+S Save
Ctrl+O Save As
Ctrl+X Exit Nano
Ctrl+R Insert File into Buffer
Ctrl+G Get HelpEditing
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 RedoSearch
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
Opening, Saving, and Closing Files (vi command mode):
vim filename Open the "filename" file :w Save the file :q Exit the editor; use the following command if the file has been modified :q! Exit the editor without saving :wq Exit the editor and save the file :wq! Force exit the editor and save the file ZZ Exit the editor and save the file ZQ Exit the editor without saving Inserting Text or Lines (vi command mode; after executing the following command, it enters insert mode, and pressing ESC exits insert mode):
a Add text to the right of the current cursor position i Add text to the left of the current cursor position A Add text at the end of the current line I Add text at the beginning of the current line O Create a new line above the current line o Create a new line below the current line R Replace (overwrite) text at the current cursor position and beyond J Merge the current line with the next line (still in command mode) Deleting and Undoing Characters or Lines (vi command mode):
x Delete the current character nx Delete n characters starting from the cursor dd Delete the current line ndd Delete n lines, including the current line u Undo the previous action U Undo all changes to the current line Copying and Pasting (vi command mode):
yy Copy the current line to the clipboard nyy Copy n lines starting from the current line to the clipboard yw Copy from the cursor to the end of the word to the clipboard nyw Copy 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 p Paste the content from the clipboard after the cursor P Paste the content from the clipboard before the cursor