在 Gedit 文件浏览器面板右键菜单中添加‘重复文件’?

在 Gedit 文件浏览器面板右键菜单中添加‘重复文件’?

在 TextMate 和其他 IDE(如 Netbeans)中,有一个右键单击选项可以复制文件。是否可以在右键单击时在 Gedit 文件浏览器面板中添加此功能?

目前,我可以创建新文件、文件夹、删除现有文件、重命名,甚至可以“在此打开终端”。为什么我们没有“复制文件”选项?


通过使用外部工具,我得出了以下脚本:

笔记:我不太懂 Python 程序员 :-)

#!/bin/sh

DocDir=$GEDIT_CURRENT_DOCUMENT_DIR #document directory
DocNm=$GEDIT_CURRENT_DOCUMENT_NAME #document name

NNM=$DocNm'_copy' #append _copy to document name
cp "$DocRir/$DocNm" "$DocDir/$NNM" #duplicate

到目前为止,文件复制成功,不过我也遇到了一些问题:

  1. index.php正在重命名为index.php_copy=> 应该是index_copy.php
  2. init.jquery.js应该init_copy.jquery.js

最好的解决方案是提取文档名称的第一部分,附加“_copy”,然后将其与名称的最后一部分(扩展名)连接起来。

你能改进这个脚本,甚至创作一个更好的脚本吗?

答案1

这是一个可以与ExternalTools 插件@Rinzwind 推荐:

#!/bin/bash

FILE="$GEDIT_CURRENT_DOCUMENT_PATH"

FILENAME="${FILE##*/}"
EXTENSION_PRE="${FILENAME#*.}"
BASENAME="${FILENAME%%.*}"
DIRNAME="${FILE%/*}"

if [[ "$FILENAME" = "$EXTENSION_PRE" ]] # handle files without extension
  then
      EXTENSION=""
  else
      EXTENSION=".$EXTENSION_PRE"
fi

cp -v "$FILE" "$DIRNAME/${BASENAME}_copy$EXTENSION"

Untitled Document我对没有扩展名的文件(例如)、具有单一扩展名的文件(例如Untitled Document.txt)和复合扩展名的文件(例如)进行了测试Untitled Document.text.txt

下面是我使用 gEdit 的外部工具进行设置的方法(此设置将在底部窗格中显示 cp 的(详细)输出):

在此处输入图片描述


编辑

以下是代码功能的逐步解释:

#!/bin/bash
#  Note: we are using /bin/bash and not /bin/sh
#  /bin/sh is the DASH shell, /bin/bash the BASH shell
#
#  We need BASH to perform the string manipulations on
#  the file path.
#
#  More information on the differences may be found here:
#  - http://stackoverflow.com/a/8960728/1708932
#  - https://wiki.ubuntu.com/DashAsBinSh

FILE="$GEDIT_CURRENT_DOCUMENT_PATH" # assign the full file path provided
                                    # by gedit to a shorter variable

# What follows are a number of bash string manipulations.
# These are very well documented in the following article:
# - http://linuxgazette.net/issue18/bash.html

# 1.) remove everything preceding (and including) the last slash
#     in the file path  to get the file name with its extension
#     (e.g. /home/test/file.tar.gz → file.tar.gz)
FILENAME="${FILE##*/}"

# 2.) remove everything in the file name before (and including)
#     the first dot to get the extension
#     (e.g. file.tar.gz → tar.gz)
EXTENSION_PRE="${FILENAME#*.}"

# 3.) remove everything in the file name after (and including) 
#     the first dot to get the basename
#     (e.g. file.tar.gz → file)
BASENAME="${FILENAME%%.*}"

# 4.) remove everything after (and including) the last slash
#     in the file pathto get the directory path
#     (e.g. /home/test/file.tar.gz → /home/test)
DIRNAME="${FILE%/*}"


# If there's no extension in the filename the second string manipulation
# will simply print the filename. That's why we check if $FILENAME and
# $EXTENSION_PRE are identical and only assign EXTENSION to a value if
# they aren't.

if [[ "$FILENAME" = "$EXTENSION_PRE" ]]
  then
      EXTENSION=""
  else
      EXTENSION=".$EXTENSION_PRE"
fi

# in the last step we compose the new filename based on the string 
# manipulation we did before and pass it to cp
cp - v "$FILE" "$DIRNAME/${BASENAME}_copy$EXTENSION"

相关内容