Welcome to the Slackware Documentation Project

Diferencias

Muestra las diferencias entre dos versiones de la página.

Enlace a la vista de comparación

es:howtos:software:efficient_cli_navigation [2019/02/28 01:44 (UTC)] (actual)
slackwarespanol creado
Línea 1: Línea 1:
 +<!-- Add your text below. We strongly advise to start with a Headline (see button bar above). -->
 +<note important>​WIP = Work in Progress</​note>​
  
 +
 +====== Efficient CLI Navigation ======
 +
 +The CLI (Command Line Interface) is a very powerful, flexible and programmable environment. If you use the command line interface on a regular basis, you know how important it is to customise your working environment and develop shortcuts to ensure efficient workflow. Below are some tips on navigating through directories in an quick and easy way.
 +
 +===== Go Back Home =====
 +
 +The ''​cd''​ command changes the current working directory to your ''/​home'':​
 +
 +<​code>​$ pwd
 +/​home/​user/​data/​documents/​work
 +$ cd
 +$ pwd
 +/​home/​user</​code>​
 +
 +===== Go Back to the Previous Directory =====
 +
 +To go back to the previous directory, you can use ''​cd -'':​
 +
 +<​code>​$ pwd
 +/​home/​user/​.i3/​config
 +$ cd ~/​data/​projects/​dotfiles/​i3
 +$ pwd
 +/​home/​user/​data/​projects/​dotfiles/​i3
 +$ cd -
 +/​home/​user/​.i3/​config
 +$ pwd
 +/​home/​user/​.i3/​config</​code>​
 +
 +
 +===== Use the Last Argument of the Previous Command =====
 +
 +The ''​$_''​ variable returns the last argument of the previous command. This can be helpful in a variety of scenarios:
 +
 +<​code>​$ pwd
 +/​home/​user/​downloads/​
 +$ cp i3status.tar.gz ~/​data/​builds/​i3/​i3status
 +$ cd $_
 +$ pwd
 +/​home/​user/​data/​builds/​i3/​i3status</​code>​
 +
 +<​code>​$ chmod +x /​path/​to/​my/​script/​script.sh
 +$ $_
 +(This will execute script.sh)</​code>​
 +
 +The same can be achieved using the combination <​key>​Alt</​key>​+<​key>​.</​key>​ or <​key>'​Esc'</​key>​+<​key>​.</​key>​.
 +===== Bash Completion =====
 +
 +Bash offers <​key>​TAB</​key>​ completion, a very useful feature that reduces the number of keystrokes you are required to type to navigate to a directory.
 +
 +<​code>​$ cd d[TAB]</​code>​
 +
 +It will expand the path with a directory starting with ''​d''​. Please note that Linux is case-sensitive so ''​~/​Desktop''​ or ''​~/​Downloads''​ will be ignored. If there are more than 1 directory starting with ''​d'',​ you need to press the <​key>​TAB</​key>​ twice to get the possible completions listed below:
 +
 +<​code>​$ cd d[TAB][TAB]
 +data/    downloads/</​code>​
 +
 +Now press <​key>'​o'</​key>​ and <​key>​TAB</​key>​ to expand the path to ''​downloads''​.
 +
 +It can greatly reduce the number of keystrokes when accessing directories. For example:
 +<​code>​$ cd data/​projects/​python-dir/​euler/​completed/</​code>​
 +
 +The keystrokes I used:
 +<​code>​cd da[TAB]p[TAB]p[TAB]e[TAB]c[TAB]</​code>​
 +
 +If each directory contained only one subdirectory,​ I could have done the following:
 +
 +<​code>​cd [TAB][TAB][TAB][TAB][TAB]</​code>​
 +
 +Not only does the bash completion reduce the number of key strokes, but it also comes handy if you do not remember directory names.
 +
 +Please note that bash completion works also on files:
 +
 +<​code>​$ cp data/​projects/​scripts/​sync-script.sh .</​code>​
 +
 +Keystrokes used:
 +
 +<​code>​$ cp d[TAB]p[TAB]s[TAB]s[TAB] .</​code>​
 +
 +The dot (.) represents the current directory so the command will copy ''​sync-script.sh''​ to the current directory.
 +
 +===== Programmable Bash Completion =====
 +
 +To take advantage of all Bash completion features, you need to install an additional package from ''/​extra'':​
 +
 +<​code>#​ slackpkg install bash-completion</​code>​
 +FIXME
 +===== CDPATH =====
 +
 +If you work in certain directories on a regular basis, you might want to include them in the $CDPATH variable. Suppose you often work in the ''​slackbuilds''​ directory which contains some builds:
 +
 +<​code>​$ cd ~/​data/​projects/​slackbuilds/​
 +$ ls
 +i3 i3status yajl dmenu libev</​code>​
 +
 +Add it to the ''​$CDPATH''​ variable by modifying ''​~/​.bashrc'':​
 +
 +<​code>​CDPATH=$CDPATH:​~/​data/​projects/​slackbuilds/</​code>​
 +
 +Please note the path included in the CDPATH variable is the path to the **parent directory** where the said directories are located. ​
 +
 +After you have sourced ''​.bashrc''​ (''​source ~/​.bashrc''​),​ you can ''​cd''​ to any of those directories from any place:
 +
 +<​code>​$ pwd
 +/​home/​user/​.config/​xfce4
 +$ cd yajl
 +/​home/​user/​data/​projects/​slackbuilds/​yajl</​code>​
 +
 +If you would like to enable Tab completion within the directories added through the CDPATH variable, you need to install ''​bash-completion''​ from Slackware'​s ''/​extra''​ directory.
 +
 +<note important>​It is NOT recommended to give your directories names similar to the system ones, eg. ''​usr'',​ ''​etc'',​ which can result in unpredictable behaviour.</​note>​
 +
 +===== Symlinks =====
 +
 +In some situations you may consider using [[slackbook:​shell?​s[]=symlinks#​linking|symlinks]] to create //​shortcuts//​ to regularly visited directories:​
 +
 +<​code>​$ ln -s /​home/​user/​data/​projects/​scripts/​slackbuilds ./​slackbuilds</​code>​
 +
 +
 +===== Bash Aliases =====
 +
 +You can make your life easier by creating aliases (= shortcuts) for commands that you use often. The syntax is very simple:
 +
 +<​code>​name_of_the_alias='​value'</​code>​
 +
 +You can place your aliases in ''​~/​.bashrc''​. You might need to create this file. Each time you edit this file you need to ''​source''​ it afterwards for the changes to take effect:
 +
 +<​code>​source ~/​.bashrc</​code>​
 +
 +or
 +
 +<​code>​. ~/​.bashrc</​code>​
 +
 +When it comes to navigation, one could, for example, create a few aliases to speed up navigating up the directory tree:
 +
 +<​code>​alias 1.='cd .. ; pwd'
 +alias 2.='cd ../.. ; pwd'
 +alias 3.='cd ../../.. ; pwd'
 +alias 4.='cd ../../../.. ; pwd'</​code>​
 +
 +The value of an alias can be quite complex. As you can see, ''​4.''​ will first change directories ''​cd ../​../​../​..''​ and then print the current working directory - ''​pwd''​. Please note a semi-colon ('';''​) separating the commands.
 +
 +<​code>​$ cd data/​projects/​python-dir/​euler/​
 +$ 4.
 +/home/user
 +$ cd -
 +/​home/​user/​data/​projects/​python-dir/​euler
 +$ 3.
 +/​home/​user/​data</​code>​
 +
 +Aliases can be used in a number of different ways. A few more examples:
 +
 +<​code>​alias epyt='​emacs -nw /​home/​user/​data/​projects/​python-dir/​euler/​32-problem.py'​
 +alias slacktop='​ssh user@slacktop'</​code>​
 +
 +===== Directory Stack =====
 +
 +BASH features some helpful directory stack buildins that help you navigate recently visited directories.
 +
 +  * ''​pushd''​ - push a directory into the directory stack and ''​cd''​ to it.
 +  * ''​popd''​ - remove a directory from the directory stack and ''​cd''​ to it.
 +  * ''​dirs''​ - display the list of the directories in the stack.
 +
 +==== How does it work in practice? ====
 +
 +First of all, add a directory to the stack. Please note that it also automatically switches to the directory (the ''​-n''​ flag suppresses this behaviour).
 +
 +<​code>​user@darkstar:​~$ pushd data/​projects/​programming/​
 +~/​data/​projects/​programming ~
 +user@darkstar:​~/​data/​projects/​programming$</​code>​
 +
 +Alternatively,​ you can ''​cd''​ to a given directory and issue:
 +
 +<​code>​pushd .</​code>​
 +
 +After adding a few directories you can display the content of the stack:
 +
 +<​code>​user@darkstar:​~$ dirs -v
 +0 ~/​projects/​web-develop/​project-eden/​includes
 +1 ~/​projects/​web-develop/​project-eden/​includes
 +2 ~/​projects/​web-develop/​project-eden/​pages/​en
 +3 ~/​projects/​web-develop/​project-eden/​css
 +4 ~/​projects/​designs
 +5 ~/​projects/​notes
 +6 ~/​public_html/​project_eden</​code>​
 +
 +Please note that the first entry always displays the current working directory so if it also sits at the top of the stack, you'll see what seems like duplicate lines. The ''​-v''​ flag is responsible for a nicely indexed output.
 +
 +To switch to one of the directories in the stack you could issue:
 +
 +<​code>​user@darkstar:​~$ cd $(dirs +2 -l)
 +user@darkstar:​~/​projects/​web-develop/​project-eden/​pages/​en$</​code>​
 +
 +Admittedly, this is not the most concise way of changing directories. To make it shorter we can add an alias and a function to the ''​~/​.bashrc''​ file.
 +
 +<​code>​alias dv='​dirs -v'</​code>​
 +
 +List the current stack by simply typing ''​dv''​.
 +
 +<​code>​cdd()
 +{
 +    position=$1
 +    if [ -z $position ]; then
 +        echo "You need to specify the position of the directory in the stack"
 +    else
 +        cd $(dirs +$1 -l)
 +    fi
 +}</​code> ​   ​
 +
 +The ''​cdd''​ function((Based on [[http://​linux.byexamples.com/​archives/​138/​directory-stack/#​comment-99015|this function]].)) makes it possible to ''​cd''​ to a given directory from the stack by typing:
 +
 +<​code>​cdd 3</​code>​
 +
 +====== Sources ======
 +<!-- If you are copying information from another source, then specify that source -->
 +  * Original source: [[http://​www.slackword.net/?​p=494|Blog Post]] ​ written by [[wiki:​user:​sycamorex |sycamorex]]
 +<!-- * Contributions by [[wiki:​user:​yyy | User Y]] -->
 +
 +<!-- Please do not modify anything below, except adding new tags.-->
 +<!-- You must remove the tag-word "​template"​ below before saving your new page -->
 +{{tag>​howtos cli cd bashrc completion work_in_progress author_sycamorex}}

En otros idiomas
QR Code
QR Code es:howtos:software:efficient_cli_navigation (generated for current page)