Command history in mysql client only showing last line of multiline query

前端 未结 1 596
攒了一身酷
攒了一身酷 2021-02-09 17:12

I\'m using Mysql\'s command-line client in Screen/Tmux, from Bash in OSX\'s Terminal.app. When using arrow-up to re-display a previously run query that spanned more than on line

1条回答
  •  情歌与酒
    2021-02-09 17:26

    You have a few options I can think of to make your life easier in this regard:

    • readline commands combined with mysql options: readline accepts commands similar to basic emacs commands (can be set to vi as well), or example CTRL-a returns you to the start of a line.

      this combined with set horizontal-scroll-mode On, a readline that you set in ~/.inputrc, which allows you to get your input in 1 straight line, putting your cursor at the very end. Combine it with CTRL-a to immediately jump to the beginning and it's quite convenient.

      ~/.inputrc:

      $if Mysql
          set horizontal-scroll-mode On`
          # uncomment the commands below to use vi keybindings
          #set keymap vi
          #set editing-mode vi
      $endif
      

      (some systems, OSX 10.5 for certain I believe use libedit instead of readline, in which case you need to put it all in ~/.editrc, if you're not sure about your system issue the mysql --version command )

      To look for commands you issued you also have CTRL-r, which allows you to type in a term and your history will be searched for the last occurence.

      interesting commands are:

      • CTRL-P go to the Previous command in your history
      • CTRL-N go to the Next command in your history
      • CTRL-R Reverse-search through your history
      • CTRL-S Search forward through your history
      • CTRL-A Move the cursor to the beginning of the line
      • CTRL-E Move the cursor to the end of the line
      • CTRL-W delete a Word backwards
      • ALT-D delete a word forwards
      • CTRL-F move the cursor Forward 1 character
      • CTRL-B move the cursor Backward 1 character
      • ALT-F move the cursor Forward 1 word
      • ALT-B move the cursor Backward 1 word
      • ALT-_ undo


      Depending on your shell and the underlying they might not all work or be intercepted though. For example on Konsole, which I use on kde, I had to disable flow-control in advanced settings to allow for CTRL-s amongst others.

      Finally mysql also gives you the \e command which allows you to edit your commands in your general file editor, if vi or emacs isn't your thing, try nano, it's easy and works well. The main disadvantage of using this is that when scrolling up newlines are ignored bu tabs and spaces aren't. It's a unix only thing but OS X should do fine there, can't check since I don't own any Apple computers, sorry. :)

      To more easily use this command you could put a readline string macro in ~/.inputrc e.g. Control-o: "\\e;\n" would bind CONTROL-o to \e; followed by enter for instant execution. (look at the Keybindings Section)

    • use an alternate shell: such as altSQL: it gives syntax colouring, nice history scrolling and some other niceties, big bonus is that you have the source so you can adapt what you want.

    • drop the shell and go GUI: Finally I'd like to plug the nice MySQL workbench to work on your databases, it's cross-platform, free and in my humble opinion a nice tool to work with.

    I realise it's not a perfect solution and each have advantages and drawbacks but I hope this has helped you along somewhat.

    0 讨论(0)
提交回复
热议问题