Practice Exercise: Using More Advanced Text Editors - vi and emacs
Objective
Learn how to use more advanced text editors, specifically vi
and emacs
, in a Linux environment.
Task 1: Basic Editing with vi
- Open a terminal window.
- Navigate to your home directory.
- Create a new text file named
vi_notes.txt
using thevi
text editor. - In
vi
, enter insert mode by pressingi
. - Write a short journal entry for today, including the date and some notes.
- Save your changes and exit insert mode by pressing
Esc
and then typing:wq
to save and quit. - Verify that the file
vi_notes.txt
was created and contains your journal entry.
[intern@intern-a1t-inf-lnx1 ~]$ vi vi_notes.txt
# After pressing `i` you will see this in the bottom
-- INSERT --
# Write anything and then pres ESC
# To save and quit
:wq
[intern@intern-a1t-inf-lnx1 ~]$ cat vi_notes.txt
This is a vi notes
Task 2: Exploring Advanced Editing with emacs
- Open the
emacs
text editor using your system's application launcher or by runningemacs
in the terminal. - Create a new text file named
emacs_code.c
. - Write a simple C program (e.g., "Hello, Linux!") in the file.
- Use
emacs
features like syntax highlighting and indentation to format your code.[intern@intern-a1t-inf-lnx1 ~]$ emacs emacs_code.c File Edit Options Buffers Tools C Help -UU-:----F1 emacs_code.c All L1 (C/*l Abbrev) --------
- Type the following code inside
#include <stdio.h> int main() { printf("Hello, World!\n"); return 0; }
- Press
ctrl + x
andctrl + c
to exit. If you have unsaved changes it will ask you if you want to save itSave file /home/intern/emacs_code.c? (y, n, !, ., q, C-r, C-f, d or C-h) # Press enter Modified buffers exist; exit anyway? (yes or no) # Type yes to save your changes
- Compile the code and run the program
[intern@intern-a1t-inf-lnx1 ~]$ gcc -o hello_world emacs_code.c [intern@intern-a1t-inf-lnx1 ~]$ ./hello_world Hello, World!
Task 3: Advanced Editing with vi
- Use
vi
to open/create thevi_journal.txt
file created. - And paste this paragraph
emacs, a powerful and versatile text editor, is renowned for its efficient search and replace functionality. With emacs, you can effortlessly locate specific words or phrases within your document using its robust search feature, and then replace them seamlessly with new content using the replace command. Whether you're editing code, configuring files, or crafting documents, emacs's search and replace capabilities empower you to make precise and rapid changes, enhancing your productiemacsty and control over your text editing tasks.
- Locate a specific word or phrase within your journal entry using
/
followed by the search term (e.g.,/emacs
). - Navigate to the next occurrence of the search term using
n
. - Replace a word in your journal entry using the
:s
command (e.g.,:s/emacs/vi/g
to replace "emacs" with "vi"). :s
means substitute and the format goes like:s/original-word/substitute-word
and theg
at the ends means global or it will change all the occurunces of the word not just the first one it founds- Save your changes and exit
vi
.
Task 4: Advanced Editing with emacs
- Use
emacs
to open theemacs_code.c
file created in Task 2. - Modify your C program by adding comments and additional code lines.
- Utilize
emacs
keyboard shortcuts for copying, cutting, and pasting text within your code. - Save the changes.
- Close
emacs
.[intern@intern-a1t-inf-lnx1 ~]$ emacs emacs_code.c File Edit Options Buffers Tools C Help #include <stdio.h> int main() { printf("Hello, World!\n"); return 0; } -UU-:----F1 emacs_code.c All L1 (C/*l Abbrev) --------------------------------------------- emacs_code.c has auto save data; consider M-x recover-this-file
- After opening the file add the following comment
# This is a c code that prints "Hello, World!"
- For example
#include <stdio.h> # This is a c code that prints "Hello, World!" int main() { printf("Hello, World!\n"); return 0; }
- Then press
ctrl + x
andctrl + c
and when asked to save file type ySave file /home/intern/emacs_code.c? (y, n, !, ., q, C-r, C-f, d or C-h)
Task 5: Comparing vi and emacs
- In your own words, describe the main differences between the
vi
andemacs
text editors in terms of features, interface, and usage. - Note which editor you find more suitable for quick text editing and which one you prefer for more extensive text manipulation.
Conclusion
In this lab exercise, you've practiced using more advanced text editors, vi
and emacs
, commonly available in Linux environments. You've learned how to create, edit, and format text files using these editors, along with some advanced features for text manipulation. Understanding these text editors is valuable for various Linux-related tasks and system administration.