• @ch00f@lemmy.world
        link
        fedilink
        2724 days ago

        Shit is usually a pain in the ass. The challenge is divining how much of a pain in the ass something has to be that someone else might have made a solution for it.

        I didn’t know you could ctrl+shift+c to copy in the terminal until a month ago when my linux n00b wife said "there has to be a better way to do this. I’ve been right clicking to copy for 10 years.

      • @grrgyle@slrpnk.net
        link
        fedilink
        1024 days ago

        Congratulations! I remember where I was when I first learned it (in a noisy server room at the back of a machine shop).

        Now pair it with FZF for fuzzy finding – it’s surprisingly easy to set up, just following any guide. It’s insanely useful. I find myself even doing things like typing:

        $ xinput --disable $(xinput --list | grep -i touchpad | grep 'id=[0-9]\+' -o | cut -d= -f2)  # Disable synaptic touchpad trackpad pointer
        

        commands with these like comments on the ends as sort of “tags” so I can ctrl+r search for them later. Yes, I know I could just use a named function, but this is like the step just before that–before I know if I’ll be issuing the same command all the time, or just for the next couple weeks. (This one was from when I was resting my notebook on my laptop.)

        • @oddlyqueer@lemmy.ml
          link
          fedilink
          English
          524 days ago

          I like this; I have a lot of commands that I don’t use often enough to justify an alias, but still need to rerun all the time. thanks!

      • @Dave@lemmy.nz
        link
        fedilink
        624 days ago

        Just checking, because I learnt to type before I worked this out, and because surely someone reading doesn’t know: press tab. Bash will fill in file names from your current directory.

        E.g. say you have files fred1file, fred2file, jim.

        Type f then press tab, it will fill to “fred”. Then press 2 and press tab again and it will fill the full “fred2file”.

        Have a play, it works in heaps of situations.

        • @bobo@lemmy.world
          link
          fedilink
          1
          edit-2
          23 days ago

          Bash will also do autocomplete for cli programs that have autocomplete functionality. Try typing:

          git r<tab><tab>

          you’ll see options for all the git commands that start with r. Often cli commands will have autocompletion for long (double dash) options.

          If you want to see all the commands that have auto complete available, look in:

          /usr/share/bash-completion/completions/

          There’s a few other locations they can live, notably:

          /etc/bash_completion.d/ ~/.bash_completion ~/.local/share/bash-completion/completions/

          I don’t know if there are more or if there is any variation per distro.

          You can also write your own bash completions. They can get pretty smart and context sensitive.

          Pretty good beginning tutorial:

          https://iridakos.com/programming/2018/03/01/bash-programmable-completion-tutorial

          edit - I should’ve mentioned that this isn’t native to bash, it requires installation of bash-completion. But bash-completion is installed by default in many distros.

      • @lmmarsano@lemmynsfw.com
        link
        fedilink
        English
        4
        edit-2
        24 days ago

        As usual, that’s documented (we can RTFM).

        Before trying ctrl-s, you may want to disable software flow control: run stty -ixon & add it to your initialization files. Otherwise, you’ll pause terminal output. ctrl-q resumes terminal output.

        stty reveals terminal special characters

        $ stty -a
        ⁝
        intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>; eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R; werase = ^W; lnext = ^V; discard = ^O; …
        ⁝
        

        These special characters/keys often perform special functions. To illustrate

        • ctrl-d on empty input typically exits/logs out of interactive terminal applications (including shells)
        • ctrl-u discards input (useful for inputs like password prompts that don’t echo input back)
        • ctrl-v inputs next character literally (such as tab)
    • @Damage@feddit.it
      link
      fedilink
      424 days ago

      Even quicker with zsh or atuin: write the first few letters then arrow up to cycle through all matches

      • Ephera
        link
        fedilink
        English
        624 days ago

        Even quicker with fish: Write the first few letters then it auto-suggests the last-run command matching that prefix (and then you can still arrow up to cycle through all matches).

        (There is also a zsh plugin for that, called zsh-autosuggestions.)

        • Cethin
          link
          fedilink
          English
          224 days ago

          A lot of stuff assumes Bash, so occasionally when the syntax differs its slightly annoying; sometimes you have to modify a few lines of a file or something (or run it as in Bash manually). Fish is so nice though. It really should replace Bash for almost everyone. If you really need Bash you can still use it.

          • Ephera
            link
            fedilink
            English
            524 days ago

            With script files, you can (and should regardless of Fish usage) put a shebang at the top, like #!/bin/sh or #!/bin/bash. Then it will run with Bash as you’d expect.

            I also recommend not setting Fish as your system-wide default shell (since then a missing shebang will cause it to run in Fish), but rather just have your terminal emulator start fish by default.

            And yeah, outside of scripting, if I notice a command requires Bash syntax (which you can often tell from Fish’s syntax highlighting turning red), then I just run bash, execute the Bash command in there and then exit back out of there.

            Having said all that, I’m not trying to take away from your point. If I wasn’t just joking around, I would caveat a Fish recommendation just as much.

            • Cethin
              link
              fedilink
              English
              224 days ago

              Yeah, it’s just some scripts that come with programs sometimes not including that is the issue. Like I said, it isn’t a big issue, just occasionally requires small edits if you try to run the script in Fish, or, like you said, running them with Bash.

    • @Cenzorrll@lemmy.world
      link
      fedilink
      4
      edit-2
      24 days ago

      I’m not going to say how long I used linux before learning this. It isn’t “this many days old”, but it may as well be.

      I have 7 headless linux boxes running.

    • @Jankatarch@lemmy.world
      link
      fedilink
      224 days ago

      I like to imagine someone added it in for their own personal use and told nobody because surely they are the only one.

    • Mikelius
      link
      fedilink
      124 days ago

      I’m shocked how far I had to scroll down before it was mentioned, I was getting scared I would have to say it lol

    • @merc@sh.itjust.works
      link
      fedilink
      123 days ago

      The worst is when you remember doing something before, but don’t remember enough details to be able to effectively search for it.

      Although, even then, I’m not going to just mindlessly hit “up”. Last time it happened I fed my command history through grep and removed all the things that I knew the command wasn’t. Just removing “ls” and “cd” from your history cuts the number of commands down by 80% or something.

      • Korthrun
        link
        fedilink
        423 days ago

        Check out the fzf shell bindings. Reverse history search with fuzzy matching is one of the features.

        • @merc@sh.itjust.works
          link
          fedilink
          123 days ago

          That doesn’t help when you remember what effect the command had but nothing about what the command itself looked like.

  • @bdonvr@thelemmy.club
    link
    fedilink
    63
    edit-2
    24 days ago

    I used to be like this but people seriously. CTRL+R

    Do it. Don’t make this one of those things you’ve heard about and just never got around to trying. Open your terminal right now and CTRL+R and type any part of the command you did before. If the command you want is not showing first just hit CTRL+R again to go to the next one back.

    DO IT.

    Edit: I did learn from this thread today though that ZSH has it set to where you can just type part of what you’re looking for then hit up to do the same thing. Neat!

    • NotNotMike.o
      link
      fedilink
      224 days ago

      I love this one but is the gif moving at 0.5 frames a second or is it just me

      • Ephera
        link
        fedilink
        English
        324 days ago

        For me, it looks normal (about 8 frames a second, probably).

      • Nailbar
        link
        fedilink
        224 days ago

        Normal for me. Thanks for pointing out it was a gif. I never realize they’re gifs because I have to “open in external” for them to actually move.

  • @PhAzE@lemmy.ca
    link
    fedilink
    2523 days ago

    Ctrl + R and start typing the command, it’ll come up, press enter. Im just more lazy because I know there are still faster ways.

    • @apftwb@lemmy.world
      link
      fedilink
      2
      edit-2
      23 days ago

      ⬆️⬆️⬆️⬆️⬆️⬆️⬆️⬆️⬆️⬆️

      CTL+C

      history

      history | less

      ⬆️⬆️⬆️

      Pg-up

      Pg-up

      q

      ! 2648

      • @Lebernashi@lemmy.world
        link
        fedilink
        923 days ago

        Every time you hit the up arrow, it shows the previous command you used in the terminal.

        So hitting the arrow once gives your last used, hitting it twice gives your second to last command, and so on.

        • @djvinniev77@lemmy.ca
          link
          fedilink
          323 days ago

          Yup and that is me. I could just history grep the command I want but I SWEAR it was just 2 commands ago, or 15 up arrows. lol.

          • @Jarix@lemmy.world
            link
            fedilink
            2
            edit-2
            23 days ago

            Ohhh! I can absolutely relate to this as well! Using a zmud client to play games on. which is probably not much different, looking, than a terminal anyways

  • JATth
    link
    fedilink
    English
    1224 days ago

    zsh-history-substring-search

    I lazily type part of the thing I want like “sys” and then ctrl+⬆️/⬇️ and sudo systemctl start libvirtd etc. appear like magic.

  • @rumba@lemmy.zip
    link
    fedilink
    English
    923 days ago

    You’re in vim, you forgot to sudo, the file is read only and you have loads of changed you don’t feel like saving off to /tmp and playing the copy file shell game.

    [esc]:w !sudo tee %

    it shoves the current buffer through tee (termina adapter) with sudo privs vim will warn you that the file changed, just [esc]:q! and don’t let it save, you already saved it.

  • @___f____g___@lemmy.ca
    link
    fedilink
    9
    edit-2
    23 days ago

    I see everyone posting about Ctrl+R, here’s a couple more useful CLI shortcuts you might enjoy:

    cd - (change directory to $OLDPWD usually the previous directory)

    git checkout - (similarly checkout the previous branch)

    Ctrl+A (return caret to beginning of command, great when you forgot a positional argument and you were almost done typing the command)

    Ctrl+E (similar to Ctrl+A but move to the end of the command)

    • YTG123
      link
      fedilink
      523 days ago

      Ctrl+A, Ctrl+E

      Many more basic Emacs keybindings work, actually! Including C-f, C-b, C-p and C-n (if you prefer them over arrow keys) as well as M-f and M-b to move by words, C-k, M-d and C-y for killing/yanking (but not M-w) and C-SPC, C-w, C-x C-x for region manipulation (tested in Bash and ZSH)

  • @Zink@programming.dev
    link
    fedilink
    8
    edit-2
    23 days ago

    Ah crap how did I set my battery charge interval again?

    history | grep battery

    history | grep bios

    history | grep sudo smbios

    Ah! There you are you little shit!

    edit to add: Actually, I think the last time I did this I remembered some numbers I set it to before. So it worked well with something like “history | grep 75” even though there were a bunch of results.

    • @Buddahriffic@lemmy.world
      link
      fedilink
      123 days ago

      Is there a good way to do this when you use a lot of terminal tabs and aren’t sure which tab you used for the command you’re looking for?

  • @Dave@lemmy.nz
    link
    fedilink
    824 days ago

    Everyone sharing their Ctrl+R tips, here’s my Control+R question:

    How does scope work? Some command history only seems to exist in certain tabs.

    Also sometimes I Ctrl+R in a tab then the command is there but I forget I need a different one first, so I ctrl+c but the next time I search for that original command somehow it doesn’t exist anymore.

    I’m using the default terminal on Nobara (fedora based).

    • @Trail@lemmy.world
      link
      fedilink
      1224 days ago

      As far as I remember, there is a bash setting that controls whether the command history is written immediately after execution (in which case it is immediately available on all tabs/windows of the console) or after closing the session (in which case it will be available next time/potentially lost if the window is forcibly closed etc).

      The default is the second one as far as I remember.

      That said, I had changed to a more powerful one in zsh years ago, so it’s been a while…

      • @Dave@lemmy.nz
        link
        fedilink
        124 days ago

        Thanks, I worked it out. Not a setting as such, but you can use PROMPT_COMMAND to run an action to insert into history each time a prompt is run, as described here.

        Though I have started down a rabbit hole of looking at other options for the shell.

    • @PotatoesFall@discuss.tchncs.de
      link
      fedilink
      9
      edit-2
      24 days ago

      This actually doesn’t depend on your terminal but on the shell running inside it. Bash is the default on most distros. I have also frequently had this issue. There might be config to fix it.

      I switched to fish instead of bash, and now I use fzf and the https://github.com/PatrickF1/fzf.fish plugin and it works GREAT. There might be a similar thing for bash, I don’t know.