HB Computer Security | Aide Memoire
  • Introduction
  • Core Technical Skills
    • Core Skills
      • Linux
        • Getting to Know Linux
          • Using the Shell
            • Shells, Terminals, and Virtual Consoles
            • Choose your Shell
            • Shell Commands
              • Non-PATH Commands
              • Command History
                • Command Line Editing
                  • Keystrokes for Navigating Command Lines
                  • Keystrokes for Editing Command Lines
                  • Keystrokes for Cutting and Pasting Text from within Command Lines
                • Command Line Recall
                  • Keystrokes for Command Line Recall
              • Connecting and Expanding Commands
                • Piping Between Commands
                • Sequential Commands
                • Expanding Commands
            • Shell Variables
              • Common Shell Variables
            • Aliases
            • Create your Own Shell Environment
              • Modification Ideas
          • Navigating the Linux File System (LFS)
            • Filesystem Commands
            • Listing Files and Directories
            • File Permissions and Ownership
              • Modifying Permissions with chmod
              • Modifying Default Permissions with umask
              • Change File Ownership with chown
            • Copying, Moving, and Removing Files
            • Finding Files
              • locate
              • find
              • grep
            • Downloading Files
              • axel
              • wget
              • curl
                • User-Agent: Googlebot
          • Working with Text Files
            • Using vim and vi to Edit Text Files
              • Starting with vi
              • Adding Text
              • Moving Around in the Text
            • Text Manipulation
        • System Administration
          • Installing Linux
            • Installing from Live Media
            • Installing in the Enterprise
            • Partitioning Hard Disks
              • Tips for Creating Partitions
          • Account Administration
            • The root Account
              • Becoming root with su
              • sudo
                • The /etc/sudoers File
                • Granting sudo privileges
                  • visudo Guidance
                • Useful sudo Hints
            • Other Administrative Accounts
            • Standard User Accounts
              • Risks of userdel: Orphaned Files
          • Graphical Remote Administration
            • Cockpit
              • Installation Guide
            • Remote Desktop Protocol with xrdp
              • Installation and Configuration
            • Remote Desktop with vnc
              • Installation and Configuration
              • Running VNC as a System Service
          • Managing Running Processes
            • Listing Processes
              • ps
              • top
              • htop
            • Backgrounding and Foregrounding
              • Starting a Background Process
              • Using Foreground and Background Commands
            • Killing and Recining Processes
              • kill and killall
          • Managing Software
            • Managing Software from the Desktop
            • Going Beyond the Limitations of Software Center
              • Debian Packages
                • Advanced Package Tool (apt)
                • Repositories
                • dpkg
        • Shell Scripting
          • Variables
            • Command Substitution
            • Arguments
          • Reading User Input
          • if, else, and elif
          • BOOLEAN Logic
          • Loops
            • for Loops
            • while Loops
          • Functions
          • Local Vs Global Variables
          • Summary
        • Securing Linux
      • Windows
        • Security Hardening
Powered by GitBook
On this page
  • Command Line Editing
  • Let's Give it a Try!

Was this helpful?

  1. Core Technical Skills
  2. Core Skills
  3. Linux
  4. Getting to Know Linux
  5. Using the Shell
  6. Shell Commands
  7. Command History

Command Line Editing

PreviousCommand HistoryNextKeystrokes for Navigating Command Lines

Last updated 1 year ago

Was this helpful?

Command Line Editing

When you make a mistake in typing out a command, the bash shell's got you covered – you don't need to erase everything and start from scratch. You can also pull up a past command and tweak it to craft a new one.

Out of the box, bash employs command-line editing inspired by the text editor. If you've had a play with emacs before, then many of the keystrokes mentioned here might be familiar to you.

If you prefer for editing shell command lines, you can easily make that happen. Add the following line to the .bashrc file in your home directory: set -o vi From then on, every time you launch a shell, you'll be in familiar territory with those vi commands ready to edit!

To do the editing, you can use a combination of control keys, meta keys, and arrow keys. For example:

  • Ctrl + f means to hold down the Ctrl key, and type f

  • Alt + f means to hold down the Alt key, and type f

Instead of the Alt key, your keyboard may use a Meta key or the Esc key. On a Windows keyboard, you can use the Windows key.

Let's Give it a Try!

To try out a bit of command-line editing, enter the following command:

ls /usr/bin | sort -f | less

This command:

  1. Lists the contents of the /usr/bin directory

  2. Sorts the contents in alphabetical order (regardless of case)

  3. Pipes the output to less

The less command displays the first page of output, after which you can go through the rest of the output one line (press Enter) or one page (press spacebar) at a time. Simply press q when you are finished.

Now, let's change this command, altering /usr/bin to /bin. You can use the following steps to change the command:

1. Press the up arrow (↑) key This displays the most recent command from your shell history

2. Press Ctrl + a This moves the cursor to the beginning of the command line

3. Press Ctrl + f or the right arrow (→) key Repeat this command a few times to position the cursor under the first slash (/)

4. Press Ctrl + d Type this command four times to delete /usr from the line

5. Press Enter This executes the command line

When you're tweaking a command line, you can type regular characters at any point, with your new characters appearing at the location of your text cursor. You can use right → and left ← arrows to move the cursor from one end to the other on the command line.

You can also press the up ↑ and down ↓ arrow keys to step through your command history and select one for editing.

emacs
vi