# Create, View, and Edit Text Files ## Dennis Kibbe ### Mesa Community College Note: This slide presentation was created using [Reveal.js](https://revealjs.com/). You can access a transcript of this presentation by pressing S for speaker notes. You can access navigation help by pressing the question mark key. Audio for this presentation is artificially generated. --- # Module Outline 1. Introduction 1. Redirect output to a file or program 1. Create pipelines 1. Edit text files from the command line 1. Change the shell environment 1. Key Takeaways 1. Resources 1. Graded Lab Note: --- # Learning Objectives *After completing the work in this module you will be able to:* 1. Use the `vim` text editor to create and edit text files. 1. Use shell redirection to redirect the output of Standard Output and Standard Error. 1. Use a pipe to redirect the output of one command to the input of another command. 1. Set shell and environmental variables. Note: --- # Introduction ## Create, View, and Edit Text Files Note: Unlike Windows, system administration in Linux is often done by editing plain text files using a text editor like Vim. While graphical editors exist they are often impractical when you need to configure a remote server over a slow connection. --- # Redirect Output to a File or Program Note: Shell redirection lets you redirect the output of the keyboard to a file instead of displaying it on the monitor. A pipe takes the output of a command and uses it as the input to another command. >>> # Standard Input, Standard Output, and Standard Error ![Screenshot of stdin, stdout, stderr](../images/stdout_1.png) ``` [user@host ~]$ ls -R /etc/ ``` Note: Standard Input is how you communicate with a program usually with the keyboard or it could be with the mouse or another means. Standard Output, and Standard Error are the responses you get from the program. Normally, you want what you type on the keyboard to show on your monitor but sometimes you want to redirect some or all of the output to a file instead. >>> # Redirect Standard Output to a File ![Screenshot of stdin, stdout, stderr](../images/stdout_2.png) ``` [user@host ~]$ ls -R /etc/ > file1 ``` Note: In this example standard output is redirected to a file while standard error still goes to the monitor. >>> # Redirect Standard Error to the Bit Bucket ![Screenshot of stdin, stdout, stderr](../images/stdout_3.png) ``` [user@host ~]$ ls -R /etc/ > file1 2> /dev/null ``` Note: If you are not interested in seeing any error messages you can redirect standard error to /dev/null which is known as the "Bit Bucket" a black hole for data. >>> # Redirect Both Standard Output and Standard Error to the Same File ![Screenshot of stdin, stdout, stderr](../images/stdout_4.png) ``` [user@host ~]$ ls -R /etc/ > file1 2>&1 ``` Note: Both standard output and standard error can be redirected to the same file. >>> # Redirect Standard Output and Standard Error to Different Files ![Screenshot of stdin, stdout, stderr](../images/stdout_5.png) ``` [user@host ~]$ ls -R /etc/ > file1 2> file2 ``` Note: In this example standard output is redirected to `file1` while standard error is redirected to `file2`. --- # Construct Pipelines ``` bash [user@host ~]$ cat /etc/passwd | cut -d : -f 1 | sort > accounts [user@host ~]$ head -n 5 accounts avahi bin chrony dnsmasq flatpak ``` Note: A powerful feature of the Bash shell is the ability to construct complex commands by stringing simple commands together using the vertical bar character. The output of the first program becomes the input of the second program. In the example here both types of redirection are used. The `cut` command takes the output of the `cat` command and shows only the first field, then the `sort` command sorts the output of the `cut` command alphabetically, and finally the output of the `sort` command is redirected to a file named _accounts_. --- # Graded Quiz ## Redirect Output to a File or Program ![clipboard](../images/clipboard-check.png) Note: After reading this section in the textbook complete the quiz in Canvas. --- # Edit Files with the Vim Text Editor ![Screenshot of the Vim text editor](../images/vim_logo.png) Note: Vim has become the standard command line editor in UNIX and Linux because it is available on all distributions, works over slow connections, and allows you to make quick, precise edits. While Vim is not intuitive nor is it menu-driven, every system administrator needs to have a basic understanding of how Vim works. --- # Vim Editor Modes Note: Unlike other text editors you may be used to, in Vim you do not start immediately editing a file. When you open Vim you are placed in the Normal mode. In the Normal mode keystrokes move you around the screen. To actually edit a file you need to enter the **INSERT** mode by pressing **i** on the keyboard. To save changes to a file requires that you first press the Escape key to return to the Normal mode. >>> # Normal Mode ![Visual Line Mode](../images/vim_normal_mode.png) Note: This is the mode you are in when you first open Vim. In the Normal mode the letters you type move you around the document. >>> # Insert Mode ![Visual Line Mode](../images/vim_insert_mode.png) Note: Type **i** for **INSERT** mode. In this mode you are editing the document. Notice that Vim now shows **INSERT** at the bottom of the screen. >>> # Visual Line Mode ![Visual Line Mode](../images/vim_visual_line_mode.png) Note: In **VISUAL LINE** mode you select rows of text with the arrow keys. >>> # Visual Block Mode ![Visual Block Mode](../images/vim_visual_block_mode.png) Note: In the **VISUAL BLOCK** mode you select a start column and then use the arrow keys to define a block of text. >>> # Command Mode ![Visual Line Mode](../images/vim_command_mode.png) Note: From the Normal mode you type a **:** (colon) to enter commands such as **w** to write your changes to disk and **q** to exit Vim. >>> # Replace or Substitute Mode ![Visual Line Mode](../images/vim_replace_mode.png) Note: You enter the **REPLACE** mode from Normal mode by typing **R** and then edit text by typing over it. This is handy for quick edits when only a couple of characters need to be changed. >>> # Search Mode ![Visual Line Mode](../images/vim_highlight_search_mode.png) Note: While in Normal mode type a **/** (forward slash) followed by a term you want to find in the document. If the *highlight search* option has been set you will see the term highlighted in the document. --- # The `vimtutor` Command ![Screenshot of the vimtutor](../images/vim_tutor.png) Note: The `vimtutor` program which is installed along side Vim is a good place to start learning the basics of Vim. When the `vimtutor` program is run it starts an instance of Vim with a practise tutorial file which makes it easy to get up to speed on the basics of using the Vim text editor. --- # Vim Configuration Files ![Screenshot of the .vimrc file.](../images/vimrc.png) Note: A hidden `.vimrc` file stores your custom setting such as showing line numbers and setting indentation. You create this hidden file in your home directory and Vim looks for it every time the program starts. --- # Recovering a Vim Session
Note: Vim saves edits as you type in case Vim dies while editing a file. This could be caused, for example, by a broken network connection. Vim creates a hidden swap file and offers to recover your work when you restart Vim. This short video shows how it works. --- # Guided Exercise ## Edit Text Files from the Shell Prompt ![Clipart of two students studying](../images/homework.png) Note: In this exercise, you use the `vimtutor` command to practice basic editing in the Vim editor. --- # Change the Shell Environment ``` [user@host ~]$ printenv $PATH /home/user/bin:/usr/local/bin:/usr/bin:/bin [user@host ~]$ printenv $HOME /home/user ``` Note: A Linux system uses many preset variables which set the environment every time a shell is opened. Examples are the PATH variable which is a list of directories where commands can be found and the HOME variable which defines the user's home directory. --- # Shell Variables ``` [user@host ~]$ set | less BASH=/bin/bash BASH_VERSION='4.4.23(1)-release' HISTFILE=/home/user/.bash_history HISTFILESIZE=1000 OSTYPE=linux PAGER=less PATH=/home/user/bin:/usr/local/bin:/usr/bin:/bin output omitted... ``` Note: Shell variables are custom set variables that are unique to the current shell and will be unset when the current shell is closed. The `set` command lists all the varibles set for a shell both environmental variables and shell variables. --- # Assign a Value to a Variable ``` [user@host ~]$ pet=hamster ``` Note: You create a variable by assigning a value to a name with the equal sign. Since environmental variables such as "HOME" are in upper case letters it is a best practice to use lower or mixed case letters for variables that you create to avoid accidentlly overwriting an important environmental variable. --- # Variable Expansion ``` [user@host ~]$ echo "My favorite pet is my $pet." My favorite pet is my hamster. ``` Note: To use the variable the dollar sign tells the shell that the second instance of "pet" should be replaced with the value "hamster." --- # Environmental Variables ``` [dennisk@sahuaro ~]$ printenv HOME=/home/dennisk HOSTNAME=sahuaro LANG=en_US.UTF-8 OSTYPE=linux SHELL=/bin/bash output omitted... ``` Note: Environmental variables are system-wide and tell commands, for example, where the user's home directory is, and which directories contain commands available to the user. --- # Bash Aliases ``` [user@host ~]$ type ls ls is aliased to 'ls --color=auto' ``` Note: Creating a Bash alias lets you run a command with a minimal amount of typing. In this example the `type` command shows that `ls` is actually an alias for `ls --color=auto`. There is a short exercise in Canvas where you will create a simple alias. --- # Make a Bash Alias Permanent ![Screenshot of aliases listed in the `~/.bashrc` file.](../images/bash_aliases.png) Note: When the Bash shell is closed a newly created alias is no longer availabe. To make the alias permanent add it to the `~/.bashrc` file. When the shell starts it will use any aliases found in that file. This excerpt from the `~/.bashrc` file on my laptop shows several aliases and a bash function used to back up the laptop to an AWS S3 bucket. --- # Guided Exercise ## Change the Shell Environment ![Clipart of two students studying](../images/homework.png) Note: In this exercise, you use shell variables and variable expansion to run commands, and set an environment variable to adjust the default editor for new shells. --- # Key Takeaways 1. Processes communicate through standard input, standard output, and standard error. 1. You can use I/O redirection to redirect output to a file or files. 1. Using a pipe lets you build more complex commands. 1. The Vim text editor is found on all UNIX and Linux distributions and a basic understanding of how Vim works is required. 1. Shell variables are unique to a shell session. 1. Environmental variables can be used by all commands. --- # Resources - [Interactive Vim Tutorial](https://www.openvim.com) - [Vim Editor Modes Explained](https://www.freecodecamp.org/news/vim-editor-modes-explained/) - [How to use the tee command: 2-Minute Linux Tips](https://youtu.be/EsP6WeNCR18) - [How to use the redirect command: 2-Minute Linux Tips](https://www.networkworld.com/video/663065/how-to-use-the-redirect-command.html) Note: Here are some resouces to help you. --- # Graded Lab ## Create, View, and Edit Text Files ![Screenshot of a sample grading script](../images/lab_techs.png) Note: In this lab you edit a text file with the Vim editor. When you complete the lab submit a screenshot of the output of the `lab grade edit-review` command. --- ![Mesa Community College logo](../images/mcc_logo.png "Mesa Community College") Note: This is the end of this slide presentation.