Quick Tip: How to unfreeze your terminal after typing CTRL + s

Every once in a while (usually when my morning coffee hasn’t kicked in yet), I’ll type Ctrl+s to save a document while using vi. Obviously this isn’t how documents are saved in vi, and the result causes your terminal to freeze, becoming completely unresponsive to keyboard commands.

Solution: To unfreeze you terminal after accidentally typing CTRL + s, you simply need to type CTRL + q, and you should now have control of you terminal again.

 

Advertisements

Quick tip: searching your shell history

My boss recently shared a great tip for performing a reverse search of your shell history. The next time you’re trying to remember a command you recently typed in terminal. Try the key stroke combination of Ctrl + r.

Typing Ctrl + r in your terminal will prompt you for a search string, and the first result it matches in your history will be returned. If the match is not correct, you can add more characters to make the search more specific, or type Ctrl + r again, and the next valid match will be displayed.

Note: The filtered search results may include the string in any portion of a history log entry, and are not limited to the beginning characters of each log entry. This also means that if multiple matches exists in a single log entry, Ctrl + r will move the search cursor to the next match in the same entry.

Search and Replace in vi

I frequently edit documents in vi and one task that’s often required is searching and replacing text. To search and replace text, you can use the substitute command :s, which follows the pattern :[range]s/{search-string}/{replacement-string}/[flags].

Example of range values:

  • % – indicates the search should occur with all lines
  • 4 – indicates the search should occur on line 4
  • 2,8 – indicates the search will occur from line 2 to line 8

Examples of flag values:

  • g – make the replacement if the text is found for all instances
  • c – ask for confirmation of a change
  • i – not case sensitive
  • I – case sensitive
  • e – don’t report error messages if no match is found

Usage examples:

Find and replace

:s/StringToFind/Replacement/

Find and replace all instances on the current line

:s/StringToFind/Replacement/g

Find and replace all instances from line 5 to line 7 upon confirmation

:5,7s/StringToFind/Replacement/gc

Find and replace with case sensitivity throughout the entire document

:%s/StringToFind/Replacement/gI

To learn more about the substitute command, open vi and type :help :s

Checking available and used disk space

Two useful terminal commands for checking disk usage in Linux, are the commands df and du.

The first command, df, provides a table of file systems with information on free and used disk space. Note: Using the -h option provides the table in a human readable format.

df -h

The second command, du, can also retrieve disk usage information. Typing du into any directory will provide a list of files and directories at the specified location with their corresponding disk usage. However du may be more useful with the options -s and -h providing a human readable summary of the specified location on disk.

eg. display disk usage for the current working directory

du -hs

or at a specified directory

du -hs directoryname

How to create symbolic links

Occasionally you will need to make a link to a file or directory in Linux. To create a symbolic link you will need to use the ln command, which will follow the form of;

ln -s <target> <link name>

Example: Assume you frequently use gnome-calculator and decide you want a link on your desktop so you can quickly start that application in the future. To accomplish this with a symbolic link you could do the following;

ln -s /usr/bin/gnome-calculator ~/Desktop/calculator

You can also update an old symbolic link by overwriting the link using the following command.

ln -sf /usr/bin/gnome-calculator ~/Desktop/calculator

or when linking to directories you will need to include an n option

ln -sfn /mydir/ linkname