Prompt
Learn how to use command line file editors.
Tutorial Video
Walk-Through
This challenge will give you experience running basic Linux commands. Additional learning materials about Linux commands can be found on Linux Journey. To solve these challenges, you will be using a Linux Command Line Interface (aka terminal or shell).
Background
While using the CLI, you may find a need to edit a file. You are likely familiar with using tools like Microsoft Word, Notepad, or Textedit to edit the contents of files. These are all graphical file editors and they are not available on the CLI. Instead, you will likely be using one of three popular CLI text editing programs: nano, vim, and emacs. Nano is the simplest and least comprehensive tool. Vim and emacs have substantially more capabilities and have a bit of a learning curve to understand. In this guide, we will provide examples of using nano and vim.
Guide
Question 1 asks a question about using nano. To open nano, simply type nano
into the terminal. You may also include a file as the second argument to the command if you wish to edit or create a specific file, eg. nano example.txt
.
nano example.txt
When you launch nano, you will see a list of commands at the bottom of the screen. Each command is preceded by a caret character (the ^ character) - this represents the “CTRL” or control key on the keyboard. The caret is used as a shorthand for the CTRL key.
Once in nano, you can type as you normally would in a graphical text editor. However, you will not be able to use your mouse to change your position in the document. You will need to use the arrow keys to move your position within the document. A text cursor will highlight your position in the file. Once you are ready to save and exit, you can press the CTRL + X characters to trigger the exit process. You will be prompted to save the buffer (buffer is referring to the data) and you can press the “Y” key to save or the “N” key to discard your edits. Nano can often be too simple for certain tasks, which may be reason for you to use vim. You can start vim using the vim command and optionally providing a filename.
vim example.txt
When using vim, you will often be switching between different modes. The default vim normal mode is effectively a read-only mode. To start making edits, you can switch to “insert” mode by simply pressing the i
letter key. When you are in insert mode, you will see INSERT at the bottom left of the screen. You can type normally to make edits while in insert mode (note: you need to be in insert mode to make normal typing edits, including deletions). To leave insert mode, simply hit the escape key.
There is also visual mode, which lets you copy and paste. To enter visual mode, hit the v
character key when you are in the normal mode. This will start a selection at your current cursor position. You can now use the arrow keys to highlight the text you want to copy. When you get the entirety of your selection, you can “yank” the selection using the y
character key.
You can then paste the copied text using the p
character key, which will paste the text immediately after your cursor.
Once you are done making your edits, you need to type in a specific command. To do this, make sure you are in normal mode. From here, you need to go into command mode by typing in a colon. If you want to save your file, you follow this with the w
character. If you are also ready to exit, you can use the q
character. After typing in your commands, you hit enter to execute them.
:wq
to save and exit vimVim will give you a warning if you attempt to quit when there are unsaved changes. To make the warning go away, you can add an exclamation after the q
.
Vim Command | Purpose |
:q | Quit (only if there are no unsaved edits) |
:w | Save the file |
:wq | Save the file and quit |
:q! | Quit without saving |
Don’t get disheartened if you get stuck in vim – it’s a common trope that beginners are stuck and never able to exit vim. If you’re stuck in vim, your best course of action is typically to hit escape and then type :q!
and hit enter to attempt the forced exit. Keep repeating that until you get out.
You might be wondering – why are there so many keyboard combinations to memorize? Well without buttons to press on the text editor, you need a lot of keyboard combinations to accomplish all the tasks you would expect in a text editor. We only covered the basic features, but there are ways to delete lines (by typing dd
), jump to the end of the document (<SHIFT> + G
), and more. As you use vim over time, you will learn these shortcuts and there are many vim guides available online.
Once you have created a file with nano or vim, you may subsequently want to rename the file, make a copy, or delete the file.
To rename a file, you can use the mv
(move) command. You will specify the original file as the first argument and the new filename as the second argument.
mv example.txt newname.txt
To make a copy of your file, you can use the cp
(copy) command. You will specify the original file as the first argument and the name of the copy as the second argument.
cp example.txt copy.text
To delete your file, you can use the rm
(remove) command. You will specify the file as the first argument. Keep in mind that this type of deletion is permanent. Unlike the Recycle Bin, you cannot restore files that have been deleted using rm
without using forensic tools.
rm example.txt
Questions
What key should you press in addition to the CTRL key to trigger the combination to exit nano?
What vim mode allows you to write new characters in the file?
What keyboard combination will save and quit the file with vim?
What keyboard combination will delete an entire line in vim?
What command would you use to rename a file?
©️ 2024 Cyber Skyline. All Rights Reserved. Unauthorized reproduction or distribution of this copyrighted work is illegal.