ZSH key binding to search backward/forward in buffer for [hardcoded list of chars]












1















I have [left arrow] [right arrow] mapped to backward/forward char, and [ctrl-left-arrow] [ctrl-right-arrow] mapped to backward/forward word"



Sometimes I want to move back and forward to the previous/next occurance of any of a list of characters. For example in a long command or long path, it's useful to move back/forward to the previous or next [ / | ; _ ] as these are common major separators.



Looking at man zshzle it looks like it should be possible, but what would the appropriate commands look like?










share|improve this question



























    1















    I have [left arrow] [right arrow] mapped to backward/forward char, and [ctrl-left-arrow] [ctrl-right-arrow] mapped to backward/forward word"



    Sometimes I want to move back and forward to the previous/next occurance of any of a list of characters. For example in a long command or long path, it's useful to move back/forward to the previous or next [ / | ; _ ] as these are common major separators.



    Looking at man zshzle it looks like it should be possible, but what would the appropriate commands look like?










    share|improve this question

























      1












      1








      1








      I have [left arrow] [right arrow] mapped to backward/forward char, and [ctrl-left-arrow] [ctrl-right-arrow] mapped to backward/forward word"



      Sometimes I want to move back and forward to the previous/next occurance of any of a list of characters. For example in a long command or long path, it's useful to move back/forward to the previous or next [ / | ; _ ] as these are common major separators.



      Looking at man zshzle it looks like it should be possible, but what would the appropriate commands look like?










      share|improve this question














      I have [left arrow] [right arrow] mapped to backward/forward char, and [ctrl-left-arrow] [ctrl-right-arrow] mapped to backward/forward word"



      Sometimes I want to move back and forward to the previous/next occurance of any of a list of characters. For example in a long command or long path, it's useful to move back/forward to the previous or next [ / | ; _ ] as these are common major separators.



      Looking at man zshzle it looks like it should be possible, but what would the appropriate commands look like?







      zsh key-binding xbindkeys






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Feb 18 at 18:21









      StilezStilez

      78011022




      78011022






















          1 Answer
          1






          active

          oldest

          votes


















          1














          jump-target



          is a tool for such moves -- very versatile, but on the cost of some additional key-strokes.



          Let's assume, it is bound to CTRL+Y. Then you press CTRL+Y, release and press ; and release. jump-target then highlights all occurences of ; in the commandline and labels them with a through z:



           # this; is; a; nonsense; command; line


          becomes



          enter image description here



          Now just press a, b, c,... to jump with your cursor to the respective position.



          shell function



          However, it's also easy to write a specific function which is doing what you want. Credits for the original idea are going to the author of this blogpost, I adapted the code to your demands:



              function backward-shell-block()          # original code from http://www.longhaired.org/blogg/individuell/2007-04-29-zsh
          { # adapted by mpy at https://superuser.com/a/1407146/195224
          local blocks block colons commandline
          commandline=${LBUFFER//[[]/|;_]/;} # replace al disired block separators with ;
          blocks=("${(s:;:)commandline/~/_}") # split at ; and replace ~ to prevent FILENAME EXPANSION messing things up
          block=$blocks[-1]
          colons=-1
          while [[ $commandline[$colons] == ";" ]]; do
          (( colons-- ))
          done
          (( CURSOR -= $#block - $colons ))
          }
          function forward-shell-block()
          {
          local blocks block colons commandline
          commandline=${RBUFFER//[[]/|;_]/;}
          blocks=("${(s:;:)commandline/~/_}")
          if [[ $commandline[1] == ";" ]]; then
          block=$blocks[2]
          else
          block=$blocks[1]
          fi
          colons=1
          while [[ $commandline[$colons] == ";" ]]; do
          (( colons++ ))
          done
          (( CURSOR += $#block + $colons -1 ))
          }

          zle -N backward-shell-block
          zle -N forward-shell-block
          bindkey '^W' backward-shell-block
          bindkey '^E' forward-shell-block


          Here I bound the left and right jumping functions to CTRL+W and CTRL+E, resp.






          share|improve this answer
























          • The shell function works perfectly! I just added it to my zshrc. Thank you!!

            – Stilez
            Feb 18 at 22:26











          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "3"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1407130%2fzsh-key-binding-to-search-backward-forward-in-buffer-for-hardcoded-list-of-char%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          jump-target



          is a tool for such moves -- very versatile, but on the cost of some additional key-strokes.



          Let's assume, it is bound to CTRL+Y. Then you press CTRL+Y, release and press ; and release. jump-target then highlights all occurences of ; in the commandline and labels them with a through z:



           # this; is; a; nonsense; command; line


          becomes



          enter image description here



          Now just press a, b, c,... to jump with your cursor to the respective position.



          shell function



          However, it's also easy to write a specific function which is doing what you want. Credits for the original idea are going to the author of this blogpost, I adapted the code to your demands:



              function backward-shell-block()          # original code from http://www.longhaired.org/blogg/individuell/2007-04-29-zsh
          { # adapted by mpy at https://superuser.com/a/1407146/195224
          local blocks block colons commandline
          commandline=${LBUFFER//[[]/|;_]/;} # replace al disired block separators with ;
          blocks=("${(s:;:)commandline/~/_}") # split at ; and replace ~ to prevent FILENAME EXPANSION messing things up
          block=$blocks[-1]
          colons=-1
          while [[ $commandline[$colons] == ";" ]]; do
          (( colons-- ))
          done
          (( CURSOR -= $#block - $colons ))
          }
          function forward-shell-block()
          {
          local blocks block colons commandline
          commandline=${RBUFFER//[[]/|;_]/;}
          blocks=("${(s:;:)commandline/~/_}")
          if [[ $commandline[1] == ";" ]]; then
          block=$blocks[2]
          else
          block=$blocks[1]
          fi
          colons=1
          while [[ $commandline[$colons] == ";" ]]; do
          (( colons++ ))
          done
          (( CURSOR += $#block + $colons -1 ))
          }

          zle -N backward-shell-block
          zle -N forward-shell-block
          bindkey '^W' backward-shell-block
          bindkey '^E' forward-shell-block


          Here I bound the left and right jumping functions to CTRL+W and CTRL+E, resp.






          share|improve this answer
























          • The shell function works perfectly! I just added it to my zshrc. Thank you!!

            – Stilez
            Feb 18 at 22:26
















          1














          jump-target



          is a tool for such moves -- very versatile, but on the cost of some additional key-strokes.



          Let's assume, it is bound to CTRL+Y. Then you press CTRL+Y, release and press ; and release. jump-target then highlights all occurences of ; in the commandline and labels them with a through z:



           # this; is; a; nonsense; command; line


          becomes



          enter image description here



          Now just press a, b, c,... to jump with your cursor to the respective position.



          shell function



          However, it's also easy to write a specific function which is doing what you want. Credits for the original idea are going to the author of this blogpost, I adapted the code to your demands:



              function backward-shell-block()          # original code from http://www.longhaired.org/blogg/individuell/2007-04-29-zsh
          { # adapted by mpy at https://superuser.com/a/1407146/195224
          local blocks block colons commandline
          commandline=${LBUFFER//[[]/|;_]/;} # replace al disired block separators with ;
          blocks=("${(s:;:)commandline/~/_}") # split at ; and replace ~ to prevent FILENAME EXPANSION messing things up
          block=$blocks[-1]
          colons=-1
          while [[ $commandline[$colons] == ";" ]]; do
          (( colons-- ))
          done
          (( CURSOR -= $#block - $colons ))
          }
          function forward-shell-block()
          {
          local blocks block colons commandline
          commandline=${RBUFFER//[[]/|;_]/;}
          blocks=("${(s:;:)commandline/~/_}")
          if [[ $commandline[1] == ";" ]]; then
          block=$blocks[2]
          else
          block=$blocks[1]
          fi
          colons=1
          while [[ $commandline[$colons] == ";" ]]; do
          (( colons++ ))
          done
          (( CURSOR += $#block + $colons -1 ))
          }

          zle -N backward-shell-block
          zle -N forward-shell-block
          bindkey '^W' backward-shell-block
          bindkey '^E' forward-shell-block


          Here I bound the left and right jumping functions to CTRL+W and CTRL+E, resp.






          share|improve this answer
























          • The shell function works perfectly! I just added it to my zshrc. Thank you!!

            – Stilez
            Feb 18 at 22:26














          1












          1








          1







          jump-target



          is a tool for such moves -- very versatile, but on the cost of some additional key-strokes.



          Let's assume, it is bound to CTRL+Y. Then you press CTRL+Y, release and press ; and release. jump-target then highlights all occurences of ; in the commandline and labels them with a through z:



           # this; is; a; nonsense; command; line


          becomes



          enter image description here



          Now just press a, b, c,... to jump with your cursor to the respective position.



          shell function



          However, it's also easy to write a specific function which is doing what you want. Credits for the original idea are going to the author of this blogpost, I adapted the code to your demands:



              function backward-shell-block()          # original code from http://www.longhaired.org/blogg/individuell/2007-04-29-zsh
          { # adapted by mpy at https://superuser.com/a/1407146/195224
          local blocks block colons commandline
          commandline=${LBUFFER//[[]/|;_]/;} # replace al disired block separators with ;
          blocks=("${(s:;:)commandline/~/_}") # split at ; and replace ~ to prevent FILENAME EXPANSION messing things up
          block=$blocks[-1]
          colons=-1
          while [[ $commandline[$colons] == ";" ]]; do
          (( colons-- ))
          done
          (( CURSOR -= $#block - $colons ))
          }
          function forward-shell-block()
          {
          local blocks block colons commandline
          commandline=${RBUFFER//[[]/|;_]/;}
          blocks=("${(s:;:)commandline/~/_}")
          if [[ $commandline[1] == ";" ]]; then
          block=$blocks[2]
          else
          block=$blocks[1]
          fi
          colons=1
          while [[ $commandline[$colons] == ";" ]]; do
          (( colons++ ))
          done
          (( CURSOR += $#block + $colons -1 ))
          }

          zle -N backward-shell-block
          zle -N forward-shell-block
          bindkey '^W' backward-shell-block
          bindkey '^E' forward-shell-block


          Here I bound the left and right jumping functions to CTRL+W and CTRL+E, resp.






          share|improve this answer













          jump-target



          is a tool for such moves -- very versatile, but on the cost of some additional key-strokes.



          Let's assume, it is bound to CTRL+Y. Then you press CTRL+Y, release and press ; and release. jump-target then highlights all occurences of ; in the commandline and labels them with a through z:



           # this; is; a; nonsense; command; line


          becomes



          enter image description here



          Now just press a, b, c,... to jump with your cursor to the respective position.



          shell function



          However, it's also easy to write a specific function which is doing what you want. Credits for the original idea are going to the author of this blogpost, I adapted the code to your demands:



              function backward-shell-block()          # original code from http://www.longhaired.org/blogg/individuell/2007-04-29-zsh
          { # adapted by mpy at https://superuser.com/a/1407146/195224
          local blocks block colons commandline
          commandline=${LBUFFER//[[]/|;_]/;} # replace al disired block separators with ;
          blocks=("${(s:;:)commandline/~/_}") # split at ; and replace ~ to prevent FILENAME EXPANSION messing things up
          block=$blocks[-1]
          colons=-1
          while [[ $commandline[$colons] == ";" ]]; do
          (( colons-- ))
          done
          (( CURSOR -= $#block - $colons ))
          }
          function forward-shell-block()
          {
          local blocks block colons commandline
          commandline=${RBUFFER//[[]/|;_]/;}
          blocks=("${(s:;:)commandline/~/_}")
          if [[ $commandline[1] == ";" ]]; then
          block=$blocks[2]
          else
          block=$blocks[1]
          fi
          colons=1
          while [[ $commandline[$colons] == ";" ]]; do
          (( colons++ ))
          done
          (( CURSOR += $#block + $colons -1 ))
          }

          zle -N backward-shell-block
          zle -N forward-shell-block
          bindkey '^W' backward-shell-block
          bindkey '^E' forward-shell-block


          Here I bound the left and right jumping functions to CTRL+W and CTRL+E, resp.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Feb 18 at 19:10









          mpympy

          18.5k45472




          18.5k45472













          • The shell function works perfectly! I just added it to my zshrc. Thank you!!

            – Stilez
            Feb 18 at 22:26



















          • The shell function works perfectly! I just added it to my zshrc. Thank you!!

            – Stilez
            Feb 18 at 22:26

















          The shell function works perfectly! I just added it to my zshrc. Thank you!!

          – Stilez
          Feb 18 at 22:26





          The shell function works perfectly! I just added it to my zshrc. Thank you!!

          – Stilez
          Feb 18 at 22:26


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Super User!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1407130%2fzsh-key-binding-to-search-backward-forward-in-buffer-for-hardcoded-list-of-char%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Probability when a professor distributes a quiz and homework assignment to a class of n students.

          Aardman Animations

          Are they similar matrix