Linux Graphical Text Editing

LinuxBeginner
Practice Now

Introduction

Text editing is a fundamental skill for anyone working with Linux systems. Whether you're creating configuration files, writing scripts, or taking notes, knowing how to efficiently create and edit text files is essential. In this lab, you will learn to use gedit, a beginner-friendly graphical text editor available in most Linux distributions. Unlike command-line editors such as nano or vim, gedit provides a familiar graphical interface with menus, toolbars, and visual feedback that makes text editing more accessible for newcomers to Linux.

Getting Started with gedit

In this step, you will learn how to open gedit and create your first text file. Gedit is a graphical text editor that comes pre-installed on many Linux distributions, including Ubuntu.

  1. First, let's navigate to our project directory. Open a terminal by clicking on the terminal icon in the dock or pressing Ctrl+Alt+T, then type:

    cd ~/project
    

    This command changes your current directory to /home/labex/project.

  2. Now, let's launch gedit to create a new file. In the same terminal, type:

    gedit first_note.txt
    

    This command opens gedit with a new file named first_note.txt. If the file already exists, it will open that file for editing.

  3. You should now see the gedit window with an empty document. Type the following text:

    Hello
    
  4. To save your file, press Ctrl+S or click on the menu option File > Save. The file will be saved in your current directory (~/project).

  5. You can confirm that your file has been saved by checking the title bar of the gedit window - it should display the filename without an asterisk, which would indicate unsaved changes.

  6. Close gedit by clicking the X in the top-right corner of the window or by selecting File > Quit from the menu.

  7. Verify that your file was created by listing the files in your project directory:

    ls -l
    

    You should see first_note.txt in the output, similar to:

    -rw-rw-r-- 1 labex labex 6 Jul 15 10:30 first_note.txt
    

Editing Existing Files with gedit

In this step, you will learn how to open and edit existing files using gedit. You'll also explore some basic editing features.

  1. Let's open the file you created in the previous step. In the terminal, type:

    gedit first_note.txt
    

    Gedit will open the existing file and display its contents.

  2. Now, place your cursor at the end of the text you previously wrote, press Enter to create a new line, and type:

    World
    

    Your file should now contain:

    Hello
    World
    
  3. Notice that gedit automatically shows an asterisk (*) next to the filename in the title bar, indicating that there are unsaved changes.

  4. Save your changes by pressing Ctrl+S or selecting File > Save from the menu. The asterisk should disappear, indicating that all changes have been saved.

  5. Let's explore some useful gedit features:

    • To select text, click and drag with your mouse
    • To copy text, select it and press Ctrl+C
    • To paste text, place your cursor where you want the text and press Ctrl+V
    • To find text, press Ctrl+F to open the search bar
  6. Try selecting the word "World" and copying it (with Ctrl+C). Then place your cursor at the end of the file, press Enter for a new line, and paste it (with Ctrl+V).

  7. Practice these operations, and when you're done, save your file with Ctrl+S and close gedit.

  8. You can verify the contents of your file from the terminal using the cat command:

    cat first_note.txt
    

    This should display the contents of your file in the terminal.

Creating a Reference File

In this step, you will create a more comprehensive text file using gedit to demonstrate how useful a text editor can be for creating reference materials or documentation.

  1. Open gedit to create a new file for storing common Linux commands:

    gedit linux_commands.txt
    
  2. In this file, you'll create a simple reference guide for basic Linux commands. Type the following content into the file:

    Basic Linux Commands Reference:
    
    ls - List files and directories
    cd - Change directory
    mkdir - Create a new directory
    rm - Remove files or directories
    
  3. Experiment with basic text formatting in gedit:

    • Select a line and press Ctrl+B to make it bold (if supported in plain text)
    • Try other formatting options from the Format menu

    Note: Plain text files (.txt) won't retain formatting like bold or italic, but gedit allows you to see formatting while editing.

  4. Save your file by pressing Ctrl+S or using the File > Save menu option.

  5. Let's add more content to our reference guide. Add these additional commands:

    cp - Copy files or directories
    mv - Move or rename files
    pwd - Print working directory (show current directory)
    touch - Create an empty file
    
  6. Gedit includes a spell checker. Enable it by clicking on Tools > Check Spelling in the menu. Any misspelled words will be underlined in red.

  7. When you're satisfied with your reference guide, save the file and close gedit.

  8. You can view your completed reference guide using the cat command:

    cat linux_commands.txt
    

    You should see all the commands you documented displayed in the terminal.

Advanced gedit Features and Customization

In this final step, you'll explore some advanced features of gedit and learn how to customize it to suit your preferences.

  1. Open gedit with a new file:

    gedit example.py
    
  2. Let's experiment with search and replace functionality. Type the following text:

    The quick brown fox jumps over the lazy dog.
    The quick brown fox jumps over the lazy dog again.
    
  3. Now let's replace all occurrences of "fox" with "cat":

    • Press Ctrl+H to open the Find and Replace dialog
    • In the "Search for" field, type fox
    • In the "Replace with" field, type cat
    • Click "Replace All"

    All occurrences of "fox" should now be replaced with "cat".

  4. Clear the file and add the following Python code:

    def greet(name):
        print(f"Hello, {name}!")
    
    greet("World")
    

    Notice how gedit automatically applies syntax highlighting for Python code, making different parts of the code appear in different colors. This feature helps in reading and writing code more efficiently.

  5. Save the file by pressing Ctrl+S. Gedit should already recognize this as a Python file from the extension.

  6. Let's customize gedit to make it more comfortable for coding:

    • Click on Edit > Preferences in the menu
    • Click on the "Font & Colors" tab
    • Experiment with different color schemes from the "Color Scheme" dropdown
    • Click on the "Editor" tab
    • Enable "Display line numbers" for easier navigation
    • Set "Tab width" to 4 spaces, which is common in Python
    • Click "Close" to apply your changes
  7. Gedit also supports plugins to extend its functionality:

    • Open Preferences again (Edit > Preferences)
    • Click on the "Plugins" tab
    • Browse through the available plugins and enable a few that interest you, such as "File Browser Panel" or "Code Comment"
    • Click "Close"
  8. Try out any plugins you enabled. For example, if you enabled the "File Browser Panel":

    • Click on View > Side Panel to show the side panel
    • Use the file browser to navigate and open files
  9. Save your Python file and close gedit when you're done exploring.

  10. You can run your Python script from the terminal to see if it works:

    python3 example.py
    

    You should see the output: Hello, World!

Summary

In this lab, you have learned the fundamentals of graphical text editing in Linux using gedit. You have mastered essential skills including:

  • Opening and launching gedit, a popular graphical text editor
  • Creating and saving new text files
  • Opening and editing existing files
  • Using basic editing operations like copy, paste, and find/replace
  • Creating structured content with proper formatting
  • Exploring advanced features like syntax highlighting and plugins
  • Customizing gedit to match your preferences

These text editing skills are crucial for various Linux tasks, from writing simple notes to editing configuration files or creating scripts. As you continue your Linux journey, the ability to efficiently create and edit text files will be an essential part of your toolkit. Gedit provides a user-friendly bridge between the simplicity of command-line editors and the complexity of full-featured IDEs, making it an excellent choice for both beginners and experienced users.