While performing your operations with commands in the terminal, you wanted to make corrections in a file (resolv.conf etc.) with the nano editor. In this case, you type sudo nano document name on the command line and enter it into the editor. When you enter the editor, the command line is now gone. What should you do if you want to do something on the command line even though you are not done with the file you are working on?
- I open a new tab.
- Save the document with Ctrl + O, exit with Ctrl + X. I type the command in the terminal, run it and then open the nano editor again.
- Ctrl + Z key combination and fg command
The first of these options might be if you are working with a visual desktop. However, this may not be possible on the remote server you connect to with ssh. The second option seems to be a method from the first ages when the computer was invented. You will find that there is a short and quick way to fix this problem.
Ctrl+Z key combination and fg command
Developers who are bored with such save-exit-re-enter processes have developed the Ctrl + Z key combination and the fg command. In this way, it is possible to send the working editor to the background without exiting and switch back to the command line. You can even open another document in the editor from the command line and send it to the background. After you send two open documents to the background, you can open another document and send it to the background.
First, let’s create test1.txt, test2.txt and test3.txt files.
root@omer:~# cd Documents/ root@omer:~/Documents# echo 'Hi, I am document number 1'> test1.txt root@omer:~/Documents# echo 'Hi, I am document number 2'> test2.txt root@omer:~/Documents# echo 'Hi, I am document number 3'> test3.txt
We open the documents with the nano editor and send them to the background with the Ctrl + z keys. In this way, we reach the command line again without closing the document.
root@omer:~/Documents# nano test1.txt // Send it to the background with Ctrl + Z when the nano is opened. Use "fg" to return to nano. + Stopped nano test1.txt root@omer:~/Documents# nano test2.txt // Send it to the background with Ctrl + Z when the nano is opened. Use "fg" to return to nano. + Stopped nano test2.txt root@omer:~/Documents# nano test3.txt // Send it to the background with Ctrl + Z when the nano is opened. Use "fg" to return to nano. + Stopped nano test3.txt root@omer:~/Documents#
As can be seen below, with each Ctrl + Z key combination, a number is given next to the transaction sent to the background. This number is used for you to recall the background task in question.
Now let’s recall a task we sent to the background. We use the fg command for this. Anyway, in the screen outputs, shell tells us to use fg to return to nano.
If you just type fg on the command line, you will return to the last closed action. However, if you want to go back to the number 1 process, it will be enough to give the fg 1 command.
root@omer:~/Documents# fg 2 Ctrl + Z key
Note1: If you want to see all the processes running in the background, you can use the jobs command.
Note2: Although the subject is explained through nano editor in this article, it is possible to refer to the same background with vi or other editors.