this post was submitted on 22 Jul 2024
74 points (98.7% liked)

Programming

17538 readers
77 users here now

Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!

Cross posting is strongly encouraged in the instance. If you feel your post or another person's post makes sense in another community cross post into it.

Hope you enjoy the instance!

Rules

Rules

  • Follow the programming.dev instance rules
  • Keep content related to programming in some way
  • If you're posting long videos try to add in some form of tldr for those who don't want to watch videos

Wormhole

Follow the wormhole through a path of communities [email protected]



founded 2 years ago
MODERATORS
top 21 comments
sorted by: hot top controversial new old
[–] [email protected] 17 points 4 months ago (5 children)

Ctl-U to delete everything on the line before cursor.

Ctl-E to skip to end of line.

Ctl-A to skip to beginning of line.

[–] [email protected] 12 points 4 months ago (3 children)

Or, just use Home and End like they were intended! Kids these days….

[–] NostraDavid 6 points 4 months ago* (last edited 4 months ago)

Kids these days….

These Ctrl keys are shortcuts from Emacs - there's a Bash settings to switch to vi-mode if you so wish. Anyway, the first Emacs was written in 1981, probably on a PDP-11, which did not have Home and End! Same reason Neovim uses "yank" instead of "copy". ctrl-c/ctrl-v did not exist as a shortcut back when vi was being written!

I know you didn't intend to be mean or anything, but maaaaaan kids these days don't know their history (not entirely your fault, btw)😆

[–] [email protected] 2 points 4 months ago

This tip is super useful to me because not everyone is using a PC. On a PC sure, I would use the Home and End keys all the time. Now I'm using a laptop as my main computer and the Home and End keys are in a weird position that even to this day, 4ish years of laptop use, I still have to actually look at the keys to find them.

[–] Ferk 0 points 4 months ago* (last edited 4 months ago)

That's horrible for muscle memory, every time I switch desk/keyboard I have to re-learn the position of the home/end/delete/PgUp/PgDn keys.

I got used to Ctrl-a / Ctrl-e and it became second nature, my hands don't have to fish for extra keys, to the point that it becomes annoying when a program does not support that. Some map Ctrl-a to "Select all" so, for input fields where the selection is one line, I'd rather Ctrl-a then left/right to go to the beginning/end than fish for home/end, wherever they are.

[–] [email protected] 7 points 4 months ago (1 children)

Ctrl-y to paste what Ctrl-u deleted or cut

[–] [email protected] 2 points 4 months ago

That's a new one for me. Thanks!

[–] [email protected] 2 points 4 months ago

If you're a VIM motions fan, you can always install the zsh-vi-mode: https://github.com/jeffreytse/zsh-vi-mode.

[–] Hammerheart 2 points 4 months ago* (last edited 4 months ago) (1 children)

ctrl-b: move cursor back one character

ctrl-f: move cursor foward one character

ctrl-d: delete character under cursor

[–] [email protected] 1 points 4 months ago (1 children)

But that's more key presses than just using existing keys

[–] Hammerheart 1 points 4 months ago

I find it easier using my pinky to hit ctrl than taking my fingers off the home row to use the arrow keys.

[–] Ferk 1 points 4 months ago* (last edited 4 months ago)
  • Alt-delete deletes the whole word before cursor
  • Alt-d deletes the whole word after cursor
  • Ctrl-k deletes (kill) everything after the cursor

Whatever is deleted is stored in the "killring" and can be pasted(yanked) back with Ctrl-y (like someone else already mentioned), consecutive uses of Alt-delete/Alt-d add to the killring.

  • Alt-b / Alt-f moves one word backwards / forwards
  • Alt-t swaps (translocates) the current word with the previous one
  • Ctrl-_ undo last edit operation

All those bindings are the same as in emacs.

Also, normally Ctrl-d inserts the end-of-file character, and typically can be used to close an active shell session or when you have some other interpreter open in the terminal for interactive input.

[–] [email protected] 5 points 4 months ago* (last edited 4 months ago)

My favorite tips are:

You can filter the output of a command. Most commands return parameters like (output, error) so you can filter them by number like 1>/dev/null will filter the output and only show the errors, and 2>/dev/null will filter the errors and only show the output. Also if you want a command to run silently but it doesn't have it's own built-in quiet mode you can add &>/dev/null which will filter everything.

Bash (and other shell's I assume) can be fully customized. In addition to the .bashrc file in your home directory, there are also a few common files that bash will look for like .bash_aliases, .bash_commands, .bash_profile or you can create your own and just add to the end of the .bashrc file ./YOUR_CUSTOM_BASH_FILE_NAME

Inside that file you can add any custom commands you want to run for every bash shell like aliases and what not.

I personally often use a simple update command like so alias up='sudo apt update -y && sudo apt upgrade -y && sudo apt autoremove -y' which just makes running updates, upgrades, and clean-up so much easier. Just type up and enter your password. I have previously added in things like &>/dev/null to quiet the commands and echo Fetching updates... to make some commands quieter but still give some simple feedback.

There's also the basics of moving around a terminal command as others have pointed out. The easiest and the one I use the most is if you hold CTRL+LEFT_ARROW the cursor will move entire words instead of one character at a time. Very helpful if you need to change something in the middle of a command.

[–] [email protected] 1 points 4 months ago (1 children)

What can tmux do that you can't do using startup and exiting scripts?

[–] [email protected] 7 points 4 months ago* (last edited 4 months ago) (2 children)

Retain sessions when you're over SSH and get disconnected.

I'm surprised this wasn't mentioned, since it's my only use case for tmux.

[–] [email protected] 2 points 4 months ago

I use dtach for this feature

[–] [email protected] 1 points 4 months ago

I should've just looked it up but got lazy to be honest.

I use Dreamhost to connect to IRC support channels using irssi. I go through Dreamhost since it hides my home IP address. It disconnects me frequently. This would be very useful.

Thank you for the info.

[–] tyler 1 points 4 months ago (1 children)

You can use - everywhere you can use a ref in a git command. I very often use

git checkout master
git pull
git checkout -
git merge -

(Of course that’s all aliased and I have other flags in there too, but that’s the gist)

[–] NostraDavid 1 points 4 months ago (1 children)

Same for cd -. Nice if you want to go to /etc/blah, and then back to where you were.

You can also use pushd and popd which will push and pop directories from a stack, if you need to do something more complex.

Also, if you want to switch faster between branches, slap this in your ~/.gitconfig under [alias]:

co = !git checkout $(git for-each-ref refs/heads/ --format='%(refname:short)' | fzf)

Presuming you've got fzf installed, you can now git co (co = checkout) to get a menu with fuzzy find capabilities to switch between branches. Nice if branch names are long/similar/only-different-at-the-end.

fzf is dope.

[–] tyler 1 points 4 months ago

I have fzf-git for all of that, but honestly I don't ever use it, as I'm hardly ever switching between branches and tab complete is enough for me in most cases.

###
# FZF GIT
###
# Deciphered from fzf-file-widget. Somewhat unclear why it doesn't exist already!
function fzf_add_to_commandline -d 'add stdin to the command line, for fzf functions'
  #git checkout $1
  read -l result
  
  commandline -t ""
  commandline -it -- (string escape $result)
  commandline -f repaint
end

function fzf_checkout -d "git checkout"
  read -l result
  git checkout $result
end


# https://gist.github.com/aluxian/9c6f97557b7971c32fdff2f2b1da8209
function __git_fzf_is_in_git_repo
  command -s -q git
    and git rev-parse HEAD >/dev/null 2>&1
end

function __git_fzf_git_status
  __git_fzf_is_in_git_repo; or return
  git -c color.status=always status --short | \
    fzf -m --ansi --preview 'git diff --color=always HEAD -- {-1} | head -500' | \
    cut -c4- | \
    sed 's/.* -> //' | \
    fzf_add_to_commandline
  commandline -f repaint
end

function __git_fzf_git_branch
  __git_fzf_is_in_git_repo; or return
  git branch -a --color=always | \
    grep -v '/HEAD\s' | \
    fzf -m --ansi --preview-window right:70% --preview 'git log --color=always --oneline --graph --date=short \
      --pretty="format:%C(auto)%cd %h%d %s %C(magenta)[%an]%Creset" \
      --print0 \
      --read0 \
      (echo {} | sed s/^..// | cut -d" " -f1) | head -'$LINES | \
    sed 's/^..//' | cut -d' ' -f1 | \
    sed 's#^remotes/##' | \
    # fzf_add_to_commandline | \
    fzf_checkout
end

function __git_fzf_git_tag
  __git_fzf_is_in_git_repo; or return
  git tag --sort -version:refname | \
    fzf -m --ansi --preview-window right:70% --preview 'git show --color=always {} | head -'$LINES | \
    fzf_add_to_commandline

end

function __git_fzf_git_log
  __git_fzf_is_in_git_repo; or return
  git log --color=always --graph --date=short --format="%C(auto)%cd %h%d %s %C(magenta)[%an]%Creset" | \
    fzf -m --ansi --reverse --preview 'git show --color=always (echo {} | grep -o "[a-f0-9]\{7,\}") | head -'$LINES | \
    sed -E 's/.*([a-f0-9]{7,}).*/\1/' | \
    fzf_add_to_commandline
end

# https://gist.github.com/junegunn/8b572b8d4b5eddd8b85e5f4d40f17236
function git_fzf_key_bindings -d "Set custom key bindings for git+fzf"
  bind \ca\cs __git_fzf_git_status
  bind \ca\cf __git_fzf_git_branch
  bind \ca\ct __git_fzf_git_tag
  bind \ca\cl __git_fzf_git_log
end

git_fzf_key_bindings
[–] [email protected] -2 points 4 months ago

advanced terminal
or
advanced tips and tricks
?