Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new feature to base.plugin.bash #2257

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions aliases/available/general.aliases.bash
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ alias _='sudo'
alias vbrc='${VISUAL:-vim} ~/.bashrc'
alias vbpf='${VISUAL:-vim} ~/.bash_profile'


# colored grep
# Need to check an existing file for a pattern that will be found to ensure
# that the check works when on an OS that supports the color option
Expand Down Expand Up @@ -55,6 +56,7 @@ alias cd..='cd ..' # Common misspelling for going up one directory
alias ...='cd ../..' # Go up two directories
alias ....='cd ../../..' # Go up three directories
alias -- -='cd -' # Go back
alias d='cd /home/$USER/Downloads' # Go to the Downloads directory
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit too short and already taken by a function for the fasd plugin. please rename to something that won't clobber or move to your private alias file.


# Shell History
alias h='history'
Expand All @@ -68,6 +70,9 @@ fi
alias md='mkdir -p'
alias rd='rmdir'

# Remove
alias rmrf='rm -rf'

# Shorten extract
alias xt='extract'

Expand Down
13 changes: 13 additions & 0 deletions plugins/available/base.plugin.bash
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,16 @@ if ! _command_exists del; then
mkdir -p /tmp/.trash && mv "$@" /tmp/.trash
}
fi

# replace multiple file extensions at once
function rex() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is already a package in Ubuntu and other distros called rex and I would rather not have them clash. Could you consider naming it renex or rext?

about 'mass replace of the extension of multiple files'
param '1: extension to replace'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The indentation is mixed spaces and tabs, please align all to just tabs. Thanks!

param '2: new extenstion'
example 'rex txt md'
group 'base'
local ext2replace="${1:-}"
local newext="${2:-}"
local files=(`ls *.$ext2replace`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
local files=(`ls *.$ext2replace`)
local files=(*."$ext2replace")

`ls *.$foo` is subject to unwanted word splitting and pathname expansions. One should simply use *."$foo". In this case, one should check the generated filenames exist because the literal '*.txt' remains if the matching filename does not exist and shopt -s nullglob is not set.

for file in "${files[@]}"; do mv "$file" "${file/%.$ext2replace/.$newext}"; done
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • $ext2replace should be quoted. Otherwise, $ext2replace is treated as a glob pattern.
  • In Bash >= 5.1, when pathsub_replacement is turned on (which is the default), & inside $newext would be expanded to the original matching. It is unusual to include & in the filename extension, but it would still cause unexpected results when the user specifies such an extension.
    • One naive workaround would be to quote $newext as "$newext", but it causes another issue with Bash <= 4.2 or when shopt -s compat42 is turned on: For example, a=x.dat; echo "${x/".dat"/".txt"}" would result in a".txt".
    • To properly handle this, one needs to save, unset, and restore the shell setting patsub_replacement.
    • Or another way to work it around is to assign the replacement result to a shell variable with no quoteing of the entire RHS: local dst=${file/%."$ext2replace"/."$newext"}. This works under an arbitrary combination of shopt -q patsub_replacement and shopt -q compat42.

}
Loading