notepad++自定义功能

37 篇文章 0 订阅
31 篇文章 0 订阅
本文介绍了Notepad++中PythonScript插件的使用,包括如何开发自定义扩展功能。通过该插件,可以实现如插入时间、删除右侧空格等操作,并提供了全功能的Python脚本支持,允许访问Notepad++和Scintilla的特性,创建菜单项、快捷键和工具栏图标。要了解更多详情,可以查阅官方文档PythonScript.chm。
摘要由CSDN通过智能技术生成

notepad++ 提供了丰富的自扩展功能,这里只介绍一下Python的plugin的扩展。

如何开发属于自己的扩展功能呢?请参考附件中的PythonScript.chm官方文档。

功能1:notepad++如何插入时间 地址:http://kanpiaoxue.iteye.com/admin/blogs/2340937

功能2:去掉右侧空格:

#rstrip the contents
def testContents(contents, lineNumber, totalLines):                
	newContents = contents.rstrip()
	editor.replaceLine(lineNumber, newContents)

editor.forEachLine(testContents)

 

【 notepad++的Python自身对象和用法需要参考它自带的文档】

需要先安装插件 python script。

插件的官方说明如下:

================== start

Scripting plugin for Notepad++, documentation included

As easy as:

 notepad.open('filename.txt')

 editor.appendText("Hello World")

Full programmatic access to Notepad++ features and menus

Full programmatic access to all of Scintilla features

Call other plugin menu items

Assign menu items, shortcuts and toolbar icons to scripts

Process Notepad++ and Scintilla events, direct from a Python script

Python console built-in

Full regular expression support for search and replace - script Python regular expression replaces

Start external programs and pipe the output direct to a Notepad++ document, or filter it, or simply to the console window

Full documentation for all the objects and methods

Author: Dave Brotherstone + Jocelyn Legault

Source: http://github.com/davegb3/PythonScript

Homepage: http://npppythonscript.sourceforge.net

Latest update: Important bug fix for editor.pymlreplace()

Added editor.getCharacterPointer() that returns a string

Added notepad.getPluginVersion() to get version of Python Script

================== end

它文档的位置在: npp.7.2.1.bin\plugins\doc\PythonScript\PythonScript.chm

在这个文档里面可以查找到各种notepad++的自身对象和调用的方法。

自定义Python脚本的目录: npp.7.2.1.bin\plugins\Config\PythonScript\scripts

 

功能

快捷键

对于脚本

插入日期

Alt + Shift + d

ActionInsertDate.py

插入日期和时间

Alt + Shift + f

ActionInsertDateTime.py

插入时间戳

Alt + Shift + t

ActionInsertTimestamp.py

插入分割线

Alt + Shift + =

ActionInsertSplitLine.py

去掉右侧空格

Alt + Shift + r

ActionRstripLine.py

格式化Windows样式的路径为Unix样式

Alt + Shift + l

ActionForReplaceLinuxPathSplit.py

为列模式计算求和与平均值

ActionCalculateNumberSumForColumnModel.py

 

 

脚本名称

脚本内容

ActionInsertDate.py

import datetime

now = datetime.datetime.now()

editor.addText(now.strftime('%Y/%m/%d'))


 

ActionInsertDateTime.py

import datetime

now = datetime.datetime.now()

editor.addText(now.strftime('%Y/%m/%d %H:%M'))

 

ActionInsertTimestamp.py

import datetime

now = datetime.datetime.now()

editor.addText(now.strftime('%Y%m%d%H%M%S'))

ActionInsertSplitLine.py

import datetime

now = datetime.datetime.now()

nowString = now.strftime('%Y%m%d%H%M%S')

formatString = '[%s %s]'.center(100,'=')

result_start = formatString % ('start', nowString)

result_end = formatString % ('  end', nowString)

editor.addText(result_start)

editor.addText('\n')

 

editor.addText(result_end)

ActionRstripLine.py

#rstrip the contents

def testContents(contents, lineNumber, totalLines):               

newContents = contents.rstrip()

editor.replaceLine(lineNumber, newContents)

 

editor.forEachLine(testContents)

 

ActionForReplaceLinuxPathSplit.py

import re

 

def replaceAction(strObject):

return strObject.replace('\\','/')

 

def replaceFunc(m):

if m :

if m.group(1):

return '/' + m.group(1) + replaceAction(m.group(2))

else:

return replaceAction(m.group(0))

return None

 

def doAction():

#wPath = r'd:\baidu\workspaces\workspace_java\dmap-console'

wPath = editor.getSelText()

if '' != wPath.strip():

distPatten = r'^(?:(\w):)?(.*)$'

rs = re.sub(distPatten,replaceFunc, wPath)

if rs:

console.write(rs + '\n')

else:

console.write('can not find right path of windows style\n')

else:

console.write('select any nothing!\n')

 

 

#==============================================[start 2017/01/05 21:11:09]===============================================

doAction()

console.show()

editor.setFocus(True) 

#==============================================[ end  2017/01/05 21:11:09]===============================================

 

ActionCalculateNumberSumForColumnModel.py

def calculateNumberSumForColumnModel():

wPath = editor.getSelText()

if '' != wPath.strip():

lst = wPath.split('\n')

 

numSum = 0

numCount = 0

for numStr in lst:

if '' == numStr.strip():

continue

else:

numSum = numSum + float(numStr)

numCount = numCount + 1

averageNumString = 'denominator is 0'

if 0 != numCount:

averageNumString = str(numSum / numCount)

result = 'sum:%s, average:%s\n' % (str(numSum), averageNumString)

console.write(result)

else:

console.write('select any nothing!\n')

 

#==============================================[start 2017/01/11 14:36:12]===============================================

console.show()

calculateNumberSumForColumnModel()

editor.setFocus(True)

 

#==============================================[ end  2017/01/11 14:36:12]===============================================

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值