Working with Linux editors:
- To create a file using the Vim text editor in Kali, use the vim command followed by the name of the file to be created or modified:
In the example provided, the vim command is used to create a file named vim_demo.txt. Since no file currently exists in the active directory by that name, Vim automatically creates a new file and opens an empty text editor.
- To start entering text into the editor, press the I key or the Insert button. Then, start entering the desired text, as follows:
In the example provided, only a single line was added to the text file. However, in most cases, you will most likely use multiple lines when creating a new file.
- Once finished, press the Esc key to exit insert mode and enter command mode in Vim. Then, type :wq and press the Enter key to save. You can then verify that the file exists and verify its contents using the following bash commands:
The ls command can be used to view the contents of the current directory. Here, you can see that the vim_demo.txt file was created. The cat command can be used to read and display the contents of the file.
- An alternative text editor that can also be used is GNU nano. The basic usage of GNU nano is very similar to Vim. To get started, use the nano command, followed by the name of the file to be created or modified:
In the example provided, the nano command is used to open a file called nano_demo.txt. Since no file currently exists with that name, a new file is created.
- Unlike Vim, there is no separate command and writing mode. Instead, writing to the file can be done automatically, and commands are executed by pressing the Ctrl button in conjunction with a particular letter key. A list of these commands can be seen at the bottom of the text editor interface at all times:
In the example provided, a single line was written to the nano_demo.txt file. To close the editor, you can use Ctrl + X. You will then be prompted to either save the file with y or not save it with n. You will be asked to confirm the filename to be written to. By default, this will be populated with the name that was provided when the nano command was executed. However, this value can be changed and the contents of the file saved to a different filename, as follows:
- Once you're done, the ls and cat commands can be used again to verify that the file was written to the directory and to verify the contents of the file, respectively.
The intention of this recipe was to discuss the basic use of each of these editors to write and manipulate files. However, it is important to note that these are both very robust text editors that have a large number of other capabilities for file editing. For more information on the usage of either, access the man pages with the man command followed by the name of the specific text editor.