Using the vim and vi Commands in the ChromeOS Linux Environment
The vim and vi commands are powerful text editors available in Linux. vi (Visual Editor) is the original Unix text editor, while vim (Vi IMproved) is an enhanced version of vi with additional features like syntax highlighting, undo/redo, and plugin support. Both editors are useful in the ChromeOS Linux (Crostini) environment for coding and system configuration.
Installing vim (If Not Preinstalled)
Most Linux distributions, including ChromeOS's Crostini, come with vi preinstalled. However, vim may need to be installed separately. You can install vim using:
bash
sudo apt update && sudo apt install vim
Launching vim or vi
To open a file in vim or vi, use:
bash
vim filename
or
bash
vi filename
If the file does not exist, it will be created upon saving.
Basic Navigation and Editing
Once inside vim or vi, you are in Normal Mode. To begin editing, switch to Insert Mode by pressing:
- i – Insert before the cursor
- a – Append after the cursor
- o – Open a new line below the current line
To return to Normal Mode, press Esc.
Saving and Exiting
:w– Save the file:q– Quit (if no changes were made):wqor:x– Save and quit:q!– Quit without saving
Navigating in Normal Mode
- h – Move left
- l – Move right
- j – Move down
- k – Move up
- G – Go to the last line
- gg – Go to the first line
- Ctrl+d – Scroll half a page down
- Ctrl+u – Scroll half a page up
Searching and Replacing
/word– Search forwordn– Move to the next search result:s/old/new/g– Replaceoldwithnewon the current line:%s/old/new/g– Replaceoldwithnewin the entire file
Copy, Paste, and Delete
- yy – Copy a line
- p – Paste copied text
- dd – Delete a line
- dw – Delete a word
- x – Delete a single character
Practical Use Cases
- Editing system configuration files:
bash sudo vim /etc/hosts - Writing and modifying scripts:
bash vim script.sh - Using
vimas an efficient code editor:bash vim main.py
Conclusion
Both vim and vi are powerful text editors available in the ChromeOS Linux environment. While vi provides a minimal editing experience, vim offers enhanced functionality. Learning their key commands can significantly improve efficiency when working with text files and code.