Difference between revisions of "Shell/Tab Completion"

From Coder Merlin
(Created page with "== Tab Completion == Very often, the shell can help us to complete file and directory names when we begin to type the name of a file (or directory) and then type {{Key|TAB}}....")
 
m (Editorial review and minor corrections)
 
(2 intermediate revisions by 2 users not shown)
Line 1: Line 1:
== Tab Completion ==
Very often, the shell can help us to complete file and directory names when we begin to type the name of a file (or directory) and then press {{Key|TAB}}. Try it now. Type "cd M" and then press {{Key|TAB}}. The shell completes the rest of the name by appending "erlin" to what you've already typed. Then press {{Key|ENTER}}. Because the completion is not ambiguous (i.e., there is only one solution) the shell completes the name. If there were multiple possibilities, the shell would complete as much as possible and then beep. At this point, we could press {{Key|TAB}} twice in succession to see the possible completions. Let's try this. Within the Merlin directory, create three new directories: hello, hello2, and hello3:
Very often, the shell can help us to complete file and directory names when we begin to type the name of a file (or directory) and then type {{Key|TAB}}. Try it now. Type "cd M" and then press {{Key|TAB}}. The shell will complete the rest of the name by appending "erlin" to what you’ve already typed. Then press {{Key|ENTER}}. Because the completion is not ambiguous (i.e. there is only one solution) the shell will complete the name. If there were multiple possibilities, the shell would complete as much as possible and then beep. At this point, we could press {{Key|TAB}} twice in succession to see the possible completions. Let’s try this. Within the Merlin directory, create three new directories: hello, hello2, and hello3:


{{ConsoleLine|john-williams@codermerlin:~/Merlin$ |mkdir hello}}
{{ConsoleLine|john-williams@codermerlin:~/Merlin$ |mkdir hello}}
Line 6: Line 5:
{{ConsoleLine|john-williams@codermerlin:~/Merlin$ |mkdir hello3}}
{{ConsoleLine|john-williams@codermerlin:~/Merlin$ |mkdir hello3}}


Then, type “cd h”:
Then, type "cd h" as below:


{{ConsoleLine|john-williams@codermerlin:~/Merlin$ |cd h}}
{{ConsoleLine|john-williams@codermerlin:~/Merlin$ |cd h}}


Now, press {{Key|TAB}}. The shell will complete as much of the name as possible and then beep. (Depending on your terminal you may not hear the beep or you may see a flash.)
Now, press {{Key|TAB}}. The shell completes as much of the name as possible and then beeps. (Depending on your terminal you might not hear the beep or you might see a flash.)


{{ConsoleLine|john-williams@codermerlin:~/Merlin$ |cd hello}}
{{ConsoleLine|john-williams@codermerlin:~/Merlin$ |cd hello}}


Now press {{Key|TAB}} twice in succession. The shell will display matching possibilities and then provide a new prompt followed by the previous entry:
Now press {{Key|TAB}} twice in succession. The shell displays matching possibilities and then provides a new prompt followed by the previous entry:


{{ConsoleLine|hello/ hello2/ hello3/|}}
{{ConsoleLine|hello/ hello2/ hello3/|}}
{{ConsoleLine|john-williams@codermerlin:~/Merlin$ |cd hello}}
{{ConsoleLine|john-williams@codermerlin:~/Merlin$ |cd hello}}


You now have the option of pressing {{Key|ENTER}} to accept "hello" or adding some more characters (in this case “2” to form “hello2” or “3” to form “hello3”) and then pressing {{Key|ENTER}} to execute the command as-is or {{Key|TAB}} to search for additional completions. In our case, let’s press {{Key|ENTER}} to descend into the "hello" directory:
You now have the option of pressing {{Key|ENTER}} to accept "hello" or adding some more characters (in this case "2" to form "hello2" or "3" to form "hello3") and then pressing {{Key|ENTER}} to execute the command as-is or {{Key|TAB}} to search for additional completions. In our case, let's press {{Key|ENTER}} to go to the "hello" directory:


{{ConsoleLine|john-williams@codermerlin:~/Merlin$ |cd hello}}
{{ConsoleLine|john-williams@codermerlin:~/Merlin$ |cd hello}}
{{ConsoleLine|john-williams@codermerlin:~/Merlin/hello$ |}}
{{ConsoleLine|john-williams@codermerlin:~/Merlin/hello$ |}}
{{MerlinMultipageExperienceNavBar}}

Latest revision as of 13:19, 22 February 2023

Within these castle walls be forged Mavens of Computer Science ...
— Merlin, The Coder

Very often, the shell can help us to complete file and directory names when we begin to type the name of a file (or directory) and then press TAB. Try it now. Type "cd M" and then press TAB. The shell completes the rest of the name by appending "erlin" to what you've already typed. Then press ENTER. Because the completion is not ambiguous (i.e., there is only one solution) the shell completes the name. If there were multiple possibilities, the shell would complete as much as possible and then beep. At this point, we could press TAB twice in succession to see the possible completions. Let's try this. Within the Merlin directory, create three new directories: hello, hello2, and hello3:

john-williams@codermerlin:~/Merlin$  mkdir hello

john-williams@codermerlin:~/Merlin$  mkdir hello2

john-williams@codermerlin:~/Merlin$  mkdir hello3

Then, type "cd h" as below:

john-williams@codermerlin:~/Merlin$  cd h

Now, press TAB. The shell completes as much of the name as possible and then beeps. (Depending on your terminal you might not hear the beep or you might see a flash.)

john-williams@codermerlin:~/Merlin$  cd hello

Now press TAB twice in succession. The shell displays matching possibilities and then provides a new prompt followed by the previous entry:

hello/ hello2/ hello3/ 

john-williams@codermerlin:~/Merlin$  cd hello

You now have the option of pressing ENTER to accept "hello" or adding some more characters (in this case "2" to form "hello2" or "3" to form "hello3") and then pressing ENTER to execute the command as-is or TAB to search for additional completions. In our case, let's press ENTER to go to the "hello" directory:

john-williams@codermerlin:~/Merlin$  cd hello

john-williams@codermerlin:~/Merlin/hello$