At some point during your HPC session, you will need to edit a script or text document. The question is, how? This is done using text editors. Nano and Vim are two terminal text editors that come to mind. These text editors are standard in a Linux environment. Let's jump into each one and see what they have to offer the users.
What is Nano Text Editor?
Nano is a user-friendly text editor for Unix-like systems with no modes. It emulates the Pico text editor which improves the features and user-friendliness. Nano has an easy GUI that allows the user to interact directly with the text instead of switching modes as in the Vim editor.
To create a new and/or open a file type the following command: $nano filename
When you type the command “nano" while in the terminal window, you will open a new blank buffer with the Nano text editor. When you add a filename, this will create a file in the current working directory labeled “filename" if it does not exist. If the file does exist, it will open that file and display its content. After you open or create a ﬁle, you will see the interface, the content, and the actions you can perform along the bottom. All the actions within Nano, can be done by pressing CTRL button and the corresponding keys.
Key Combination Description Ctrl + O To write the current ﬁle to disk Ctrl + X Close the current ﬁle buffer / Exit from Nano Ctrl + K Cut text Ctrl + U UnCut text Ctrl + A Move to the beginning of the current line Ctrl + G Display the help text Ctrl + C Display the position of the cursor Ctrl + J Justiﬁes the current paragraph
To save a file using CTRL + O
Here we have a python code that was written for later use. Now you must save all this python code so the data will not be lost. With the key combination of CTRL + O, you will be able to save those changes. When you press those keys, it will show you the filename and if you want to name it something else, you can. Hit enter and then to exit the editor, hit the key combination of CTRL + X.
Cut and paste the selected text using CTRL + K and CTRL + U
To select the text to cut, left click and hold on the mouse and highlight the text. Next click on the CTRL + K key combinations and this will copy it to the clipboard. To paste it, the key combination is CTRL + U.
To search for a word in Nano using CTRL + W
While inside the Nano text editor, you can search for a word or phrase in your file by using the key combination CTRL + W. It will show a line labeled “Search:” to type in your word or phrase. The word or phrase you are searching for, the editor will place the cursor in the first letter of the first occurrence of the word.
Above, I have entered the phrase “import h5py”. Notice there are six “import” words in this file. Without the “h5py”, the cursor would have been placed on the first import.
Here you see the search concluded and placed the cursor at the "import h5py" located near the beginning of the file. Using the search function can be as general or specific as you need.
For more information on Nano, please visit nano-editor.org
What is Vim Text Editor?
The Vim text editor is a standard Unix editor and is available on all Unix ﬂavors. It is a clone of the Vi editor and the name Vim is an acronym for Vi Improved. This editor is an enhanced version of the Vi text editor. It is a command-centric editor, so beginners might find it difficult to work with it. Once you master it, you can solve many complex text-related tasks with few commands.
The Vim text editor has 2 modes
Command Mode – This mode allows you to give commands to the editor.
Insert mode - This allows you to insert text into the ﬁle. The I key on your keyboard is used to enter the insert mode. Some basic Vim commands and their descriptions are listed below.
Some basic Vim commands and their descriptions are listed below.
|:wq||Save and exit|
|:q!||Force quit without saving|
|view <myFile.txt> or vi -R <myFile.txt>||View an existing ﬁle in read-mode only|
|vi <myFile.txt>||Create a new ﬁle or modify an existing ﬁle|
To create a new and/or open a file
When you want to create and/or open a file in vim, you will open the text editor with the vim command followed by the file name. $vim <filename>
Enter insert mode to edit a document
To insert text in your document, hit the ‘I’ key on the keyboard to enter insert mode. You can now begin typing any text you want. If you opened a document, you will have to go into insert mode to make changes to that document. On the bottom left corner of the terminal window, you should see -- INSERT -- when you click the ‘I’ key on the keyboard. This puts you in insert mode and the editor is ready to receive input from you. When you’re done editing, hit the escape key to get back to command mode.
To save and exit your document
After you have made changes to your document, now it is time to save your progress. With a few keystrokes from the keyboard, you will be able to save your progress. By typing the following command ‘:wq’ in the command mode of the editor, you will be able to save, exit, as well as name your file if you have not done so.
If you wish to view a document and make no changes, or you accidentally made a change and want to exit without saving, use the following command :q!
The ‘:q!’ command tells the editor to exit without saving.
Practice makes perfect
If you need some extra practice or enjoy technology so much and would like to go on an interactive adventure to help you learn and understand the editor, there are two ways to do this.
Vim offers a tutor built into the application. This tutor is interactive by asking the user to navigate through examples using Vim commands. To get to this Vim tutor, type in the following command in the Bash shell window to go to a tutorial page: $vimtutor
Following the onscreen readings, you will learn how to use and understand Vim and its commands. If you want to exit this tutorial at any time, hit the ‘:q’ keys on the keyboard and you will exit the application.
If you are a person who loves visual learning with a hint of fun involved, then this next interactive tutorial will catch your eye. Vim-Adventures is a fun interactive adventure game created to help understand Vim and how to use it visually.
To learn more information about the Vim editor, please visit TutorialsPoint.