Given that I have been writing the odd article here and there about server work, hosting, VPS and the like, I thought that perhaps an article about editing configuration files / text documents in a command line scenario might be a good idea.
There are a few major text editors out there, some more user-friendly while some are more complex but bring extra power and configuration (I'm looking at you Vim.)
The editor that most users who are new to the world of working with text only will likely start with, is called Nano.
Nano comes installed with most modern distributions of GNU/Linux, but you MAY run into the odd one that doesn't. If for some strange reason you do not have it, it's guaranteed to be in the main repositories for your distribution.
Nano, in my opinion is relatively straightforward to use, it has a list of various commands that can be used at the bottom of the screen in a legend for you, there's no mucking about with various 'modes' like with other editors, and it has all the functions needed for the basics.
A quick how-to on Nano
To start, open a terminal window and navigate to any directory you choose, or just use your home directory. Once ready, type: nano
Tip: You can load a file in nano directly when you load the editor by adding its path to the command, e.g. nano thisismyfile. If the file does not exist, it will create a new buffer with that filename instead.
Once the editor is open, take a look at the legend of commands at the bottom of the screen. You'll note that they each have either a ^ or and M beside them; these signify which key needs to be pressed with the corresponding second key (such as ^K to Cut Text.)
The keyboard shortcut keys are:
- ^ = CTRL
- M = ALT
A useful command that you may make use of quite a bit in the beginning is Ctrl-G to bring up the help documentation of nano. Another is Ctrl-O to save the text file that is open in the editor at the time.
Highlighting text in Nano is done by holding the SHIFT key and then using your arrow keys, such as when you want to highlight a word to cut it. You may also use Ctrl+^ to mark text instead.
Navigating text in Nano is fairly easily done either via Arrow Keys, or by using commands such as CTRL+ArrowKey to navigate forward/backward by WORD rather than by LETTER, or CTRL+ArrowKey to navigate PARAGRAPH up and down.
- Ctrl-A jumps to the top of a document,
- Ctrl-E to the end of the document.
- Ctrl-Y moves up a page.
- Ctrl-V moves down a page.
- Alt-^ copies lines (or Alt-6)
- Ctrl-U pastes text
- Ctrl-W searches for text
Another useful function especially when dealing with error output from something you are trying to do, is to navigate to a specific line of text.
Let's say that your nginx finds an issue in one of your configuration files for a website you are hosting, and the output tells you that the error is on line 173 of myconfig.conf. What we can do is open the file in nano, and quickly jump to that specific line by pressing ^_ (CTRL+SHIFT+DASH key.) Nano will then prompt you to enter the line, and column numbers. Simply enter "173" and voila, you are now at your problem!
There are many other possible ways to use Nano, it has many commands, not all of which are listed at the bottom of the screen; to access them, press CNTRL+G to bring up (or hide) the help screen.
The final thing in this quick tutorial, is how to exit and save a document in Nano.
CTRL+X is your hotkey for 'exit'. If you have NOT edited a document, Nano will simply quit. However, if you have made any changes, nano will then ask you "Save Modified Buffer?" which is asking if you want to save. You can press Y or N for yes or no. Pressing N will simply exit without saving changes, where pressing Y will then ask you what you want to name the file. You can keep the same name simply by pressing ENTER, or you can enter in a new name.
That's it! These are the absolute basics of using the Nano text editor.
Here are some resource pages that you may find useful: