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

Rename To Preserve Extension #279

Closed
smhmd opened this issue Dec 25, 2019 · 18 comments
Closed

Rename To Preserve Extension #279

smhmd opened this issue Dec 25, 2019 · 18 comments

Comments

@smhmd
Copy link

smhmd commented Dec 25, 2019

rename: README.md│ should instead be rename: README│.md as keeping the extension is a more common use case.

The recommended way in the wiki removes the extension (and leaves the dot) and forces you to have multiple keybindings for files, files without extensions and directories.

@lucas-mior
Copy link
Contributor

lucas-mior commented Dec 27, 2019

very ugly solution (map it to A or whatever you like)

cmd renameappend ${{
filename="${f%.*}"
nome="$(basename "$filename")"
lf -remote "send $id :rename; cmd-home"
chars="$(echo $nome | wc -m)"
n=1
while [[ $n -lt $chars ]]
do
lf -remote "send $id cmd-right"
n=$((n+1))
done
}}

@roket1428
Copy link

My solution:

cmd rename-before-ext &{{
	if [ "$(echo "$fx" | wc -l)" -gt 1 ]; then
		lf -remote "send $id echo You can't change more than one file name"
		lf -remote "send $id unselect"
	else
		if [ -d "$f" ]; then
			lf -remote "send $id rename"
		else
			ext_len="$(echo ${f##*.} | wc -m)"
			for i in $(seq "$ext_len"); do
				pos="${pos}<left>"
			done
			lf -remote "send $id rename"
			lf -remote "send $id push "$pos""
		fi
	fi
}}

@Limero
Copy link
Contributor

Limero commented Jan 3, 2020

I agree that keeping the extension is the most common when renaming. Should we make this the default behavior? Should there be an option to toggle it? Would be nice to not have to use custom commands.

@smhmd
Copy link
Author

smhmd commented Jan 3, 2020

@Limero I think it should be the default. If you wanna change the entire name you would ctrl+u.

@roket1428
Copy link

I agree that keeping the extension is the most common when renaming. Should we make this the default behavior? Should there be an option to toggle it? Would be nice to not have to use custom commands.

I think there gotta be at least 4 default naming function: One for renaming it entirely (like just let me change its name to this new value), one for appending (at the very end of the file/directory), one for inserting (at the very beginning of the file/directory), one for appending before the extension of the file (default behavior for directories should be same as appending IMHO).

Here is my lfrc for those (yes, I use hard tabs and I don't care what you think):

# insert
cmd rename-insert &{{
	if [ "$(echo "$fx" | wc -l)" -gt 1 ]; then
		lf -remote "send $id echo You can't change more than one file name"
		lf -remote "send $id unselect"
	else
		len="$(basename "$f" | wc -m)" # length of selected file/directory
		for i in $(seq "$len"); do
			pos="${pos}<left>" # how many times do I have to press <left> in order to get first char
		done
		lf -remote "send $id rename"
		lf -remote "send $id push $pos"
	fi
}}

# append before ext
cmd rename-before-ext &{{
	if [ "$(echo "$fx" | wc -l)" -gt 1 ]; then
		lf -remote "send $id echo You can't change more than one file name"
		lf -remote "send $id unselect"
	else
		if [ -d "$f" ]; then
			lf -remote "send $id rename"
		else
			ext_len="$(echo ${f##*.} | wc -m)" # extention length
			for i in $(seq "$ext_len"); do
				pos="${pos}<left>"
			done
			lf -remote "send $id rename"
			lf -remote "send $id push "$pos""
		fi
	fi
}}

# change its name entirely
cmd rename-new &{{
	if [ "$(echo "$fx" | wc -l)" -gt 1 ]; then
		lf -remote "send $id echo You can't change more than one file name"
		lf -remote "send $id unselect"
	else
		lf -remote "send $id rename"
		lf -remote "send $id push <c-u>"
	fi
}}

map cc rename-new # new name
map I rename-insert # at the very begging
map A rename # at the very end
map aa rename-before-ext # right before the extention

@gokcehan
Copy link
Owner

Thank you all for the feedback. For the record, this is why I try not to include commands as builtin because everyone has some slightly different favorite and expects it to be the default. The current default seems to be the simpler one rather than the common one. You should be able to define other versions of rename commands using either push command:

map R push r<a-b><c-b>

Or by using command line commands:

map R :rename; cmd-word-back; cmd-left

Examples in the wiki page dates back to times we did not have a builtin rename command. Perhaps it is time to remove those examples in favor of these.

@smhmd
Copy link
Author

smhmd commented Jan 10, 2020

I really think sane, great defaults are better than having two commands for renaming.

EDIT: you might as well throw bulk renaming when multiple files are selected in there. and have these defaults in /usr/local/etc/lf/lfrc for easy copy, edit.

In this case, someone is going to realize some command could be better written, and we're all going to benefit. It feels like lf consciously wants people to have custom commands for everything.

@roket1428
Copy link

roket1428 commented Feb 9, 2020

I agree that keeping the extension is the most common when renaming. Should we make this the default behavior? Should there be an option to toggle it? Would be nice to not have to use custom commands.

I think there gotta be at least 4 default naming function: One for renaming it entirely (like just let me change its name to this new value), one for appending (at the very end of the file/directory), one for inserting (at the very beginning of the file/directory), one for appending before the extension of the file (default behavior for directories should be same as appending IMHO).

Here is my lfrc for those (yes, I use hard tabs and I don't care what you think):

# insert
cmd rename-insert &{{
	if [ "$(echo "$fx" | wc -l)" -gt 1 ]; then
		lf -remote "send $id echo You can't change more than one file name"
		lf -remote "send $id unselect"
	else
		len="$(basename "$f" | wc -m)" # length of selected file/directory
		for i in $(seq "$len"); do
			pos="${pos}<left>" # how many times do I have to press <left> in order to get first char
		done
		lf -remote "send $id rename"
		lf -remote "send $id push $pos"
	fi
}}

# append before ext
cmd rename-before-ext &{{
	if [ "$(echo "$fx" | wc -l)" -gt 1 ]; then
		lf -remote "send $id echo You can't change more than one file name"
		lf -remote "send $id unselect"
	else
		if [ -d "$f" ]; then
			lf -remote "send $id rename"
		else
			ext_len="$(echo ${f##*.} | wc -m)" # extention length
			for i in $(seq "$ext_len"); do
				pos="${pos}<left>"
			done
			lf -remote "send $id rename"
			lf -remote "send $id push "$pos""
		fi
	fi
}}

# change its name entirely
cmd rename-new &{{
	if [ "$(echo "$fx" | wc -l)" -gt 1 ]; then
		lf -remote "send $id echo You can't change more than one file name"
		lf -remote "send $id unselect"
	else
		lf -remote "send $id rename"
		lf -remote "send $id push <c-u>"
	fi
}}

map cc rename-new # new name
map I rename-insert # at the very begging
map A rename # at the very end
map aa rename-before-ext # right before the extention

After tweaking a bit, I realized that I can shorten it to this:

map cc push A<c-u> # new rename
map I push A<c-a> # at the very beginning
map A rename # at the very end
map i push A<a-b><a-b><a-f> # before extention
map a push A<a-b> # after extention

@doronbehar
Copy link
Contributor

Hey all :) I'm the one who wrote that section at the wiki and I've just stumbled upon this discussion. Just wanted to let you know I've edited that wiki section with a link to this issue and I've made the commands handle filenames with quotes and possibly other characters better, using a printf '%q'.

You might also be interested in #306 .

@roket1428
Copy link

I agree that keeping the extension is the most common when renaming. Should we make this the default behavior? Should there be an option to toggle it? Would be nice to not have to use custom commands.

I think there gotta be at least 4 default naming function: One for renaming it entirely (like just let me change its name to this new value), one for appending (at the very end of the file/directory), one for inserting (at the very beginning of the file/directory), one for appending before the extension of the file (default behavior for directories should be same as appending IMHO).
Here is my lfrc for those (yes, I use hard tabs and I don't care what you think):

# insert
cmd rename-insert &{{
	if [ "$(echo "$fx" | wc -l)" -gt 1 ]; then
		lf -remote "send $id echo You can't change more than one file name"
		lf -remote "send $id unselect"
	else
		len="$(basename "$f" | wc -m)" # length of selected file/directory
		for i in $(seq "$len"); do
			pos="${pos}<left>" # how many times do I have to press <left> in order to get first char
		done
		lf -remote "send $id rename"
		lf -remote "send $id push $pos"
	fi
}}

# append before ext
cmd rename-before-ext &{{
	if [ "$(echo "$fx" | wc -l)" -gt 1 ]; then
		lf -remote "send $id echo You can't change more than one file name"
		lf -remote "send $id unselect"
	else
		if [ -d "$f" ]; then
			lf -remote "send $id rename"
		else
			ext_len="$(echo ${f##*.} | wc -m)" # extention length
			for i in $(seq "$ext_len"); do
				pos="${pos}<left>"
			done
			lf -remote "send $id rename"
			lf -remote "send $id push "$pos""
		fi
	fi
}}

# change its name entirely
cmd rename-new &{{
	if [ "$(echo "$fx" | wc -l)" -gt 1 ]; then
		lf -remote "send $id echo You can't change more than one file name"
		lf -remote "send $id unselect"
	else
		lf -remote "send $id rename"
		lf -remote "send $id push <c-u>"
	fi
}}

map cc rename-new # new name
map I rename-insert # at the very begging
map A rename # at the very end
map aa rename-before-ext # right before the extention

After tweaking a bit, I realized that I can shorten it to this:

map cc push A<c-u> # new rename
map I push A<c-a> # at the very beginning
map A rename # at the very end
map i push A<a-b><a-b><a-f> # before extention
map a push A<a-b> # after extention

Okay, this is last, I promise. :)

I realized that "before extension" keybinding has one bug. Right now, it goes to the very end of filename thanks to default rename function then goes back one word, and then goes one word back again. After that, it goes forward by one word.

This goes right before the . in a filename format like this <alphanumeric file name>.<ext> but if the file name contains something like " right before the ., it just goes right before the ". So, it needs a fix and that is map i push A<a-b><c-b>.

This first goes to the very end of filename, and goes one word back (right after the .), and then, goes back one character which is the .. So, you'll end up right before the ..

Thanks to @doronbehar to help me realize it. :)

Anyways, have a nice day. :)

@doronbehar
Copy link
Contributor

Answering your comment #306 (comment) which to my view relates to the discussion here more then there.

And while you are writing functions/scripts to lf, prefer POSIX shell scripting as not everyone uses bash as default shell.

Totally agree with you, but replacing printf '%q' with a posix shell equivalent seems like too much effort for an edge case, see https://stackoverflow.com/a/29824428/4935114 . I've added a note to the wiki indicating this was tested with zsh.

@gokcehan
Copy link
Owner

I think the best argument in favor of this is that it is easier to define the other behaviors when the default is keeping the extension since there could be files with no extension or multiple extensions. If anyone wants to implement this, I'm now willing to accept a PR. The function filepath.Ext can be used for this purpose which seems to have a proper extension definition.

@yogeshdnumb
Copy link

yogeshdnumb commented Apr 15, 2023

I think this is a better solution

map A rename # at the very end
map c push A # new rename
map I push A # at the very beginning
map i push A # before extension
map a push A # after extension

I got this from lukesmith's voidrice config

@joelim-work
Copy link
Collaborator

I have updated the tips to reflect the new behavior of the rename command.

@doronbehar
Copy link
Contributor

I have updated the tips to reflect the new behavior of the rename command.

What is the new behavior you are talking about exactly? Speaking of the wiki, that wiki section is very unclear TBH :). I think the previous version was better - It's not clear what is the purpose of showing that table - keybindings after / before applying your config? Also, what happened to the map ra and map re mappings? How does the example config there is implementing them?

Link to wiki diff: https://github.com/gokcehan/lf/wiki/Tips/_compare/8311fe2f2d0fef74cbd1baf858dbac44d0f4c865...12982d7d605d108e2bdd1a2fa35037f890fad631

@joelim-work
Copy link
Collaborator

What is the new behavior you are talking about exactly?

rename places the cursor at the extension by default as of #1162 so the map re mapping isn't needed anymore.

Speaking of the wiki, that wiki section is very unclear TBH :)

I agree that the descriptions by themselves can be vague, I have added another column to the table to visually show the effects of each mapping. Thanks for the feedback.

I think the previous version was better

I fail to see how. Positioning the cursor can be implemented entirely using native lf commands, so why involve shell scripting and the complexity it brings?

It's not clear what is the purpose of showing that table - keybindings after / before applying your config?

After. That is rather obvious when comparing the table with the mappings below.

Also, what happened to the map ra and map re mappings? How does the example config there is implementing them?

As I have explained above, map re is the default behavior already. map ra has the same effect as A in the new example.

@doronbehar
Copy link
Contributor

Thanks for the link to the PR, I missed that. The table indeed looks better now. I still felt that the phrasings around the table and the config example was a bit confusing, so I expanded that a bit.

@joelim-work
Copy link
Collaborator

OK, thanks for tidying the example up. I think it looks even better now that the mapping table comes after the config.

@gokcehan Unless I'm missing something I believe this ticket can actually be closed now, since preserving the extension is now the default behavior.

@gokcehan gokcehan closed this as completed Jul 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants