How to backspace a sentence till dot, comma, etc Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
MuhammadAhmadKhalid
Posts: 6
Joined: 08 Jan 2021, 02:43

How to backspace a sentence till dot, comma, etc

Post by MuhammadAhmadKhalid » 30 Nov 2021, 01:34

As the {Ctrl Down}{Backspace}{Ctrl Up} backspace a word how to backspace a sentence till the dot. Comupter recognizes that a word has finish in this example but how to make delete a sentence till dot, comma semi colon, colon, question mark and this "|" line type thing. I have read about the script that backspace a line till the start, but this backspacing till dot require some other coding that I don't know. I want a code that backspaces the line till the up mentioned marks and does it instantly like the {Ctrl Down}{Backspace}{Ctrl Up} code does.
Last edited by gregster on 30 Nov 2021, 01:37, edited 1 time in total.
Reason: Topic moved from 'About This Community'.

Rohwedder
Posts: 7611
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: How to backspace a sentence till dot, comma, etc

Post by Rohwedder » 30 Nov 2021, 07:06

Hallo,
perhaps?:

Code: Select all

F1::
ClipSaved := ClipBoardAll
SendInput, ^{Left}
Loop
{
	ClipBoard =
	SendInput, ^+{Left}^c
	ClipWait, 0
	IF ErrorLevel Or RegExMatch(ClipBoard, "[,;:.?|]")
		Break
	SendInput, {Bs}
}
SendInput, ^{Right}
ClipBoard := ClipSaved
Return

User avatar
mikeyww
Posts: 26848
Joined: 09 Sep 2014, 18:38

Re: How to backspace a sentence till dot, comma, etc

Post by mikeyww » 30 Nov 2021, 07:28

This works in Notepad but might not work in some other programs.

Code: Select all

controlName := "Edit1", find := ".,;:?|"
#IfWinActive ahk_exe notepad.exe
F3::
ControlGet, row , CurrentLine,, %controlName% ; Cursor's line number
ControlGet, col , CurrentCol ,, %controlName% ; Cursor's column number
ControlGet, line, Line, %row% , %controlName% ; Text of the current line
pos := pos2 := 0
While pos2 := RegExMatch(line, "[" find "]", char, pos2 ? pos2 + 1 : 1)
 pos := pos2 < col ? pos2 : pos
SendInput % pos ? "{BS " col - pos - 1 "}" : ""
Return
#IfWinActive

MuhammadAhmadKhalid
Posts: 6
Joined: 08 Jan 2021, 02:43

Re: How to backspace a sentence till dot, comma, etc  Topic is solved

Post by MuhammadAhmadKhalid » 01 Dec 2021, 06:52

@Rohwedder , your code is working but at the end of the sentence it was leaving a word due to the ^{Left} input at the start. I changed the script to the following according to my usage and it is working nicely. The only issue that I am getting is that it is not that fast but does the job. It is even slow in the word or other heavy programme other than notepad.


Code: Select all

F3::
ClipSaved := ClipBoardAll
SendInput, {BackSpace 3}
Loop
{
	ClipBoard =
	SendInput, ^+{Left}^c
	ClipWait, 0
	IF ErrorLevel Or RegExMatch(ClipBoard, "[,;:.?|]")
		Break
	SendInput, {Bs}
}
SendInput, ^{Right}
ClipBoard := ClipSaved
return

MuhammadAhmadKhalid
Posts: 6
Joined: 08 Jan 2021, 02:43

Re: How to backspace a sentence till dot, comma, etc

Post by MuhammadAhmadKhalid » 01 Dec 2021, 06:54

mikeyww wrote:
30 Nov 2021, 07:28
This works in Notepad but might not work in some other programs.

Code: Select all

controlName := "Edit1", find := ".,;:?|"
#IfWinActive ahk_exe notepad.exe
F3::
ControlGet, row , CurrentLine,, %controlName% ; Cursor's line number
ControlGet, col , CurrentCol ,, %controlName% ; Cursor's column number
ControlGet, line, Line, %row% , %controlName% ; Text of the current line
pos := pos2 := 0
While pos2 := RegExMatch(line, "[" find "]", char, pos2 ? pos2 + 1 : 1)
 pos := pos2 < col ? pos2 : pos
SendInput % pos ? "{BS " col - pos - 1 "}" : ""
Return
#IfWinActive
Sir, I can get this code to work even in the noteopad. It is note responding. When I press the hotkey it does not do anything.

User avatar
mikeyww
Posts: 26848
Joined: 09 Sep 2014, 18:38

Re: How to backspace a sentence till dot, comma, etc

Post by mikeyww » 01 Dec 2021, 07:10

If the cursor is positioned after a punctuation mark, then nothing happens because the goal has already been met.

MuhammadAhmadKhalid
Posts: 6
Joined: 08 Jan 2021, 02:43

Re: How to backspace a sentence till dot, comma, etc

Post by MuhammadAhmadKhalid » 01 Dec 2021, 07:54

mikeyww wrote:
01 Dec 2021, 07:10
If the cursor is positioned after a punctuation mark, then nothing happens because the goal has already been met.
Sir, Just 2 last questions.

1. When the backspacing process has reached the very first line then it stops deleting because it is obvious that there is no recognizable mark at the start. Can you please make it also delete the first line?
2. Can you make it work in every application or any text box anywhere?

This was my first post. Thank you all for supporting @Rohwedder @mikeyww . I don't thought it was such a healthy environment. Thank you. Sorry for the bad english.

garry
Posts: 3758
Joined: 22 Dec 2013, 12:50

Re: How to backspace a sentence till dot, comma, etc

Post by garry » 01 Dec 2021, 10:32

maybe , an example to cut existing text

Code: Select all

text=
(Ltrim join`r`n
Windows 11’s hardware= requirements = are strict 
This is undoubtedly the biggest: barrier to entry - millions of Windows 10 devices simply aren’t compatible with Windows 11. Microsoft has made the hardware requirements much stricter as it prioritises security, with a recent 1GHz processor, TPM 2.0 and Secure Boot among the most controversial updates. There are ways to get around this, of course, but most people won’t be comfortable running Windows 11 on a device that’s not supported by Microsoft.
Is this the last line ? No I continue with test . https://www.techadvisor.com/news/windows/windows-11-why-havent-more-people-upgraded-3811404/
)
text1 := RegExReplace(text, "([. ! ? : = - ] )", "$1`r`n")
Fileappend,%text1%`r`n,test21.txt,utf-8
run,test21.txt
return

/*
Windows 11’s hardware= 
requirements = 
are strict
This is undoubtedly the biggest: 
barrier to entry - millions of Windows 10 devices simply aren’t compatible with Windows 11. 
Microsoft has made the hardware requirements much stricter as it prioritises security, with a recent 1GHz processor, TPM 2.0 and Secure Boot among the most controversial updates. 
There are ways to get around this, of course, but most people won’t be comfortable running Windows 11 on a device that’s not supported by Microsoft.
Is this the last line ? 
No I continue with test . 
https://www.techadvisor.com/news/windows/windows-11-why-havent-more-people-upgraded-3811404/
*/


User avatar
mikeyww
Posts: 26848
Joined: 09 Sep 2014, 18:38

Re: How to backspace a sentence till dot, comma, etc

Post by mikeyww » 01 Dec 2021, 11:43

This version works across multiple lines.

Code: Select all

controlName := "Edit1", find := ".,;:?|"
#IfWinActive ahk_exe notepad.exe
F3::
absPos := pos := pos2 := 0
ControlGetText, cText, %controlName%
ControlGet, row , CurrentLine,, %controlName% ; Cursor's line number
ControlGet, col , CurrentCol ,, %controlName% ; Cursor's column number
ControlGet, line, Line, %row% , %controlName% ; Text of the current line
For lineNum, line in StrSplit(cText, "`r`n")  ; Get position within all text
 absPos += lineNum < row ? StrLen(line) + 2 : lineNum = row ? col : 0
While pos2 := RegExMatch(cText, "[" find "]",, pos2 ? pos2 + 1 : 1)
 pos := pos2 <= absPos ? pos2 : pos
RegExReplace(SubStr(cText, 1, pos), "`n",, count)
SendInput % pos ? "{BS " absPos - pos - 1 - (row - (pos ? count + 1 : 0)) "}" : ""
Return
#IfWinActive

MuhammadAhmadKhalid
Posts: 6
Joined: 08 Jan 2021, 02:43

Re: How to backspace a sentence till dot, comma, etc

Post by MuhammadAhmadKhalid » 01 Dec 2021, 12:32

mikeyww wrote:
01 Dec 2021, 11:43
This version works across multiple lines.

Code: Select all

controlName := "Edit1", find := ".,;:?|"
#IfWinActive ahk_exe notepad.exe
F3::
absPos := pos := pos2 := 0
ControlGetText, cText, %controlName%
ControlGet, row , CurrentLine,, %controlName% ; Cursor's line number
ControlGet, col , CurrentCol ,, %controlName% ; Cursor's column number
ControlGet, line, Line, %row% , %controlName% ; Text of the current line
For lineNum, line in StrSplit(cText, "`r`n")  ; Get position within all text
 absPos += lineNum < row ? StrLen(line) + 2 : lineNum = row ? col : 0
While pos2 := RegExMatch(cText, "[" find "]",, pos2 ? pos2 + 1 : 1)
 pos := pos2 <= absPos ? pos2 : pos
RegExReplace(SubStr(cText, 1, pos), "`n",, count)
SendInput % pos ? "{BS " absPos - pos - 1 - (row - (pos ? count + 1 : 0)) "}" : ""
Return
#IfWinActive
Can this work in word, please?

User avatar
mikeyww
Posts: 26848
Joined: 09 Sep 2014, 18:38

Re: How to backspace a sentence till dot, comma, etc

Post by mikeyww » 01 Dec 2021, 13:26

I'm not so sure that Word uses a standard edit control. You may have better luck with Rohwedder's script there.

MuhammadAhmadKhalid
Posts: 6
Joined: 08 Jan 2021, 02:43

Re: How to backspace a sentence till dot, comma, etc

Post by MuhammadAhmadKhalid » 01 Dec 2021, 14:15

mikeyww wrote:
01 Dec 2021, 13:26
I'm not so sure that Word uses a standard edit control. You may have better luck with Rohwedder's script there.
Thank you very much. Have a nice day.

Post Reply

Return to “Ask for Help (v1)”