This article covers the usage of pagers and the Vim text editor, detailing commands for viewing, navigating, and editing text files in the terminal.
In this guide, we explore the usage of pagers and the powerful Vim text editor. First, we discuss two popular pagers—Less and More—detailing how to view and navigate through text files in the terminal. Then, we introduce Vim, covering its core functionality, essential commands, and editing techniques.
Pagers like Less allow you to view and navigate through large text files directly in the terminal. Less provides advanced features such as scrolling and searching, making it a popular choice for system log inspections and file viewing.To open a file using Less, enter the following command (here, we inspect the system log):
Copy
Ask AI
sudo less /var/log/syslog
When the file is open, you will typically see the filename highlighted in the lower left corner. Navigation is easy with the arrow keys for scrolling up and down.Below is an example snippet from a syslog as displayed by Less:
Remember to use the arrow keys in Less for smooth navigation through your log files. For more extensive documents, Less is highly recommended over other simpler pagers.
While Less offers many features, the More pager is a simpler alternative with a more straightforward interface. More is effective for quick file views.To open a file with More, run the following command:
Copy
Ask AI
more /path/to/your/file
When you view a file with More, a progress indicator (like “More (2%)”) appears at the bottom of the screen. Use the spacebar to advance through the file page by page.Below is a sample output from More:
Copy
Ask AI
2024-05-07T00:34:30.894971+00:00 kodekloud systemd[1]: plymouth-start.service - Show Plymouth Boot Screen2024-05-07T00:34:30.894974+00:00 kodekloud systemd[1]: Started systemd-ask-password-console.path - Dispatch Password Requests to Console Directory Watch.2024-05-07T00:34:30.894980+00:00 kodekloud systemd[1]: systemd-ask-password-plymouth.path - Forward Password Requests to Plymouth Directory Watch was skipped because of an unmet condition check (ConditionPathExists=/run/plymouth/pid).2024-05-07T00:34:30.894983+00:00 kodekloud systemd[1]: Reached target cryptsetup.target - Local Encrypted Volumes.2024-05-07T00:34:30.894987+00:00 kodekloud (udev-worker)[456]: dm-0: Process '/usr/bin/unshare -m /usr/bin/snap auto-import --mount=/dev/dm-0' failed with exit code 1.2024-05-07T00:34:30.894992+00:00 kodekloud (udev-worker)[451]: sda: Process '/usr/bin/unshare -m /usr/bin/snap auto-import --mount=/dev/sda' failed with exit code 1.2024-05-07T00:34:30.894995+00:00 kodekloud lvm[522]: PV /dev/sda3 online, VG ubuntu-vg is complete.2024-05-07T00:34:30.894996+00:00 kodekloud lvm[522]: VG ubuntu-vg finished2024-05-07T00:34:30.895000+00:00 kodekloud (udev-worker)[467]: sda2: Process '/usr/bin/unshare -m /usr/bin/snap auto-import --mount=/dev/sda2' failed with exit code 1.2024-05-07T00:34:30.895008+00:00 kodekloud (udev-worker)[451]: sda1: Process '/usr/bin/unshare -m /usr/bin/snap auto-import --mount=/dev/sda1' failed with exit code 1.2024-05-07T00:34:30.895012+00:00 kodekloud (udev-worker)[473]: sr0: Process '/usr/bin/unshare -m /usr/bin/snap auto-import --mount=/dev/sr0' failed with exit code 1.2024-05-07T00:34:30.895019+00:00 kodekloud (udev-worker)[450]: sda3: Process '/usr/bin/unshare -m /usr/bin/snap auto-import --mount=/dev/sda3' failed with exit code 1.2024-05-07T00:34:30.895020+00:00 kodekloud systemd[1]: Found device dev-disk-by\x2duuid-18157e24\x2d3de7\x24d2b2\x2d9dc0\x2d21d01162db644.service - File System Check on /dev/disk/by-uuid/18157e24-3d--More-- (2%)
Vim, short for “Vi Improved,” is a powerful text editor designed for efficiency. When you start Vim, a welcome screen appears, providing basic information and usage tips:
Copy
Ask AI
VIM - Vi IMprovedversion 9.1.16by Bram Moolenaar et al.Modified by [email protected]Vim is open source and freely distributableBecome a registered Vim user!type :help register<Enter> for informationtype :q<Enter> to exittype :help<Enter> or <F1> for on-line helptype :help version9<Enter> for version info
Vim is a mode-based editor, meaning it operates in different modes for specific tasks:
Insert Mode: Press the i key to enter Insert mode and start editing your file.
Command Mode: Press the Esc key to switch back to Command mode. In this mode, you can run commands for saving, quitting, or navigating within the file.
Always press Esc to ensure you are in Command mode before executing commands.
In this guide, we reviewed the Less and More pagers for file navigation in the terminal, and provided an introduction to Vim with practical commands for editing, searching, and navigating files. Below is an additional text snippet as a closing note:
Copy
Ask AI
OK - This is another line of textOK - This is another line of textFusce cursus efficitur hendrerit. Duis aliquam nibh diam, sed fermentum justo ultrices ac. Nunc eu dui tempus, accumsan odio ut, bibendum lacus. Pellentesque sed congue nibh. Donec eu egestas erat. Etiam vita dolor turpis. Sed eget velit consectetur, scelerisque ex at, fermentum lectus. Integer porttitor nisl orci, ut porttitor libero sollicitudin id. Duis sagittis, quam ut bibendum pretium, purus nisl eleifend quam, finibus semper mi diam ut urna. Fusce volutpat mauris ut efficitur vulputate. Vestibulum et iaculis mi. Aenean eget magna sollicitudin, convallis tortor finibus, cursus est.
That concludes our overview of terminal pagers and Vim. Happy editing, and see you in the next lesson!