1.4

Text Files and Editors

  • Objectives: Discuss text files and editors for modifying them.
  • Text files are crucial in POSIX systems and beyond.

Importance of Text Files

  • Programming:
    • Source code is created as text files.
    • Compiled or interpreted.
    • Documentation and tests are text files.
    • Payloads sent over APIs are often text files.
  • Systems and Operations:
    • Configuration files (especially in POSIX and embedded systems) are text files.
    • Log files are text files requiring tools for analysis.
    • Scripts are text files.
    • Infrastructure as a Service (IaaS) files (e.g., playbooks) are used for infrastructure automation and are created as text files.
  • Data Science:
    • Data is often stored in text files (exported data, raw sensor data).
    • Science products (analyzed data derivatives) are stored as text files for further processing.
  • Text files are universally readable and writable, leading to a wide range of tools for manipulation.

Tools for Manipulating Text Files

  • Interactive Text Editors:
    • GUI and command-line interface (CLI) editors.
  • Code Editors:
    • More advanced than text editors.
  • Integrated Development Environments (IDEs):
    • Full-fledged development environments.
  • Stream Editors:
    • Use pattern matching languages (e.g., regular expressions) to search and replace text.
    • Typically run on the command line.
  • Command Line Utilities:
    • Extensive tools for working with text files.
  • Version Control:
    • Optimized for text files (like Git).
    • Can also handle binary files.
  • Converters, Compression Tools, Markup Renderers:
    • Specialized tools for specific text file functionalities.

Text Files vs. Binary Files

  • Word processing documents (e.g., ".docx") are typically binary files, not text files.
  • Word processors are not text editors.

Benefits of Text Files

  • Stand the Test of Time:
    • Usable by thousands of programs.
  • No Vendor Lock-In:
    • Readable now and in the future without specific programs or licenses.
  • Platform Independence:
    • Manipulatable across different platforms.
  • Universal Manipulation:
    • Full choice of editing tools in any environment.
  • Content Focus:
    • Authors concentrate on content and structure, not presentation.
  • Easy Transformation:
    • Easily convertible to other formats.

Favorite Editing Tools

  • Use any text management tools (compression, comparison, etc.).
  • Version control (Git) efficiently tracks changes over time.
  • Store text files (source code, documentation, tests, configuration files, ancillary data files) together.
  • Automation:
    • Automatically generate reports from various text file inputs using scripts.

Text Editors

  • Essential for developers, system administrators, operations engineers, and data scientists.
  • Text files are often formatted with markup or syntax.
    • Markup: Tags adding information.
    • Syntax: Rules of a language (programming or scripting).
  • Text editors help adhere to specific formats.

Text Editor Functionality

  • Basic functionality to simplify adherence to formats.
  • Syntax highlighting.
  • Indication of syntax rule adherence.
  • Powerful search and replace functionality.
  • Efficient batch-style operations.
  • Choose editors carefully due to frequent use.

GUI vs. CLI Text Editors

  • GUI editors are common and popular.
  • Knowing at least one CLI text editor is important.
    • Servers often accessible only via command line.
    • Minimal systems may only have one pre-installed text editor.
    • VI is often the default text editor launched by other tools.

VI Text Editor

  • One of the earliest and still popular text editors.
  • Purely CLI.
  • Efficient and powerful.
  • VI is resource efficient.
  • Difficult to use; hard to learn.

Nano Text Editor

  • Easier to use than VI.
  • Less powerful, but still efficient.
  • Managing servers via CLI is common.
  • Modify/read text files on remote servers.

Recommendations

  • Learn both CLI and GUI-based text editors.
  • May have to work with systems set up by others.

Source Code Editors

  • Text editors with extensions.
  • Syntax highlighting (better than basic text editors).
  • Help with indenting code blocks.
  • Auto-completion (not in text editors).
  • Bracket matching.
  • Syntax checking (pre-compiling/interpreting).
  • Convenient way to run compiler/interpreter and debugger.

Integrated Development Environments (IDEs)

  • Take code editor concept further.
  • Integrate source code editor, compiler, interpreter, debugger, and Git integration into one GUI application.
  • Focus on specific languages or environments (e.g., Android or macOS development).
  • Line between IDEs and source code editors blurring as source code editors add more tools.

Editor Selection

  • Text editors for normal cases.
  • Code editors for more power.
  • IDEs for daily work in a specific environment.

VI Details

  • One of the oldest and most ubiquitous text editors in POSIX environments.
  • CLI editor controlled via keyboard commands.
  • Requires memorizing commands (like the command line interface itself).
  • Basic usage limited to about six keyboard shortcuts.
  • Knowing basics is essential (even to exit).
  • Servers accessed via SSH are often headless (no GUI).
  • VI enables text file manipulation in such environments.

VI/VIM

  • Most installations have VIM (VI Improved).
  • VIM is more like a source code editor.
  • Both require learning keyboard shortcuts.
  • Invoked the same way (typing vi often launches VIM).

Nano/Pico

  • Easier to learn with a handful of commands shown on screen.

VI States

  • Command Mode: Waiting for commands; cannot directly type text.
  • Insert Mode: Allows typing text (accessed via i command).
  • Last Line Mode: For saving and exiting (accessed via :).
    • q: Quit.
    • wq: Write and quit.
    • q!: Force quit (even with changes).
  • Return to Command Mode from Last Line Mode using escape or w (saves file).

Recommendations (Continued)

  • Use a GUI code editor (e.g., VS Code) on a daily basis.

Visual Studio Code (VS Code)

  • Popular code editor from Microsoft.
  • Open source and cross-platform.
  • Different from Microsoft Visual Studio (a full IDE).
  • Extensible with plugins.
  • Configurable.
  • VIM plugin available.
  • Plugins for popular languages and markup languages.
  • Resource demanding.
  • Overkill for simple text file editing.
  • Current favorite editor for developers and others in computing.

Demonstrations

  • VI Demo:
    • vi [filename] launches VI.
    • i enters insert mode.
    • escape returns to command mode.
    • : enters last line mode.
    • wq saves and quits.
  • Nano Demo:
    • nano [filename] launches Nano.
    • Directly start typing.
    • Ctrl+O saves/writes the file.
    • Ctrl+X exits.
    • Common actions displayed at the bottom of the screen.

References

  • Links to tools such as VS Code are provided for further learning.