Excuse me for the non-informative title. Just can't shortly describe what I want within one question.
Okay, let's say we have two lines in our text file. The cursor is somewhere in the 1st line (the example shows cursor position):
SO helps| people
People like SO
What I want is to substitute the first line with an empty line or with another line and cursor should be at the start of this empty line.
I tried CTRL +L and type the "new line". But CTRL +L expands selection to the line + \n and that causes this result (the example shows cursor position):
new line|People like SO
If the cursor is at the begining of the line, CTRL +K+K works perfect. But how can do the same when the cursor is not at the begining (without pressing ENTER , arrows and using mouse)?
The equivalent of Ctrl + K + K at the beginning of a line when the cursor is somewhere in the line is : Ctrl + Shift + K
Here:
first line with text
second line| with other text
third line with another text
-> Ctrl + Shift + K :
first line with text
|third line with another text
EDIT
Since it is requested that the result should be:
first line
|
third line
You could either do what is written above and press ENTER or go to Packages/Delete Line.sublime-macro and replace the file with the following code:
[
{"command": "expand_selection", "args": {"to": "line"}},
{"command": "add_to_kill_ring", "args": {"forward": true}},
{"command": "left_delete"},
{"command": "insert", "args": {"characters": "\n"}},
{"command": "move", "args": {"by": "lines", "forward": false}}
]
Related
I'd like to create a keyboard shortcut (such as CTRL+T) that automatically moves the cursor to the line after the occurence of a fixed text, such as &todo.
Example:
foo
bar
&todo
fix bug #783
blah
blah2
Pressing CTRL+T would automatically move the cursor to the line beginning with fix ....
Currently I'm doing it like this:
CTRL F
enter &todo, ENTER
ESCAPE (closes the Search bottom panel)
HOME
DOWN ARROW (moves to next line)
but this requires too many actions.
How to do that in a single key shortcut?
The best solution is it use a plugin to do that.
The plugin below does what you require. It will find the next occurrence of pattern (i.e. the &todo marker) below the current cursor position, move the cursor to the line below it, and centre that position in the window. If the pattern is not found below the current cursor position it will search again from the top of the buffer providing a wrap around feature.
Copy and paste the following Python code into a buffer and save it in your Sublime Text config User folder as GoToPattern.py.
import sublime
import sublime_plugin
class GotoPatternCommand(sublime_plugin.TextCommand):
def run(self, edit, pattern):
sels = self.view.sel()
# Optional flags; see API.
flags = sublime.LITERAL | sublime.IGNORECASE
start_pos = sels[0].end() if len(sels) > 0 else 0
find_pos = self.view.find(pattern, start_pos, flags)
if not find_pos and start_pos > 0:
# Begin search again at the top of the buffer; wrap around
# feature, i.e. do not stop the search at the buffer's end.
find_pos = self.view.find(pattern, 0, flags)
if not find_pos:
sublime.status_message("'{}' not found".format(pattern))
return
sels.clear()
sels.add(find_pos.begin())
self.view.show_at_center(find_pos.begin())
row, col = self.view.rowcol(find_pos.begin())
self.view.run_command("goto_line", {"line": row + 2})
# Uncomment for: cursor to the end of the line.
# self.view.run_command("move_to", {"to": "eol"})
Add key bindings:
// The pattern arg, i.e. "&todo", can be changed to anything you want
// and other key bindings can also be added to use different patterns.
{"keys": ["???"], "command": "goto_pattern", "args": {"pattern": "&todo"}}
Add a Command Palette entry to Default.sublime-commands if you want:
{"caption": "GoToPattern: &todo", "command": "goto_pattern", "args": {"pattern": "&todo"}},
These links may be useful to you ST v. 2 API and ST v. 3 API.
P.S. Did you know that Sublime Text has bookmarks? [Just in case you didn't.]
The accepted answer is really better and I'm finally using it.
For reference, here an old solution I used: first create a gototodo.py file in "C:\Users\User\AppData\Roaming\Sublime Text 2\Packages\User\" containing:
import sublime, sublime_plugin
class GototodoCommand(sublime_plugin.TextCommand):
def run(self, edit):
contents = self.view.substr(sublime.Region(0, self.view.size())) # https://stackoverflow.com/questions/20182008/sublime-text-3-api-get-all-text-from-a-file
a = contents.find('&todo')
cursors = self.view.sel()
cursors.clear()
location = sublime.Region(a, a)
cursors.add(location)
self.view.show_at_center(location)
(row, col) = self.view.rowcol(self.view.sel()[0].begin()) # go to the next line
self.view.run_command("goto_line", {"line": row+2})
Then add this in "C:\Users\User\AppData\Roaming\Sublime Text 2\Packages\User\Default (Windows).sublime-keymap":
{ "keys": ["ctrl+t"], "command": "gototodo" }
Done!
I'm trying to make my program have a dialogue box with a password that has been set in a file. I need help having it scan a line of code in a file and if the password entry is true, then proceed.
This code gets a password and compares it to a password in a file:
INPUT "Password", x$
OPEN "password.dat" FOR INPUT AS #1
INPUT #1, y$
IF x$ = y$ THEN PRINT "Match": ' do stuff
I was wondering if there is any shortcut key for sublime text(3) or plugin for selecting all the text between two line breaks.
For example:
$search_box = Input::get('search_box');
$city_code = Input::get('city_code');
$check_in = Input::get('check_in');
$check_out = Input::get('check_out');
$noofrooms = Input::get('noofrooms');
$adultguest = Input::get('adultguest');
$childguest = Input::get('childguest');
$childage = Input::get('childage');
Session::put('adultguest', $adultguest);
Session::put('childguest', $childguest);
Session::put('childage', $childage);
In above code, I want to select all the text from$noofrooms = Input::get('noofrooms'); to Input::get('childage'); with shortcut key at once. Is there any?
Add the following line to Preferences > Key Bindings - User
{ "keys": ["alt+p"], "command": "expand_selection_to_paragraph" }
(You can change the "alt+p" to whatever key combo you want)
Followed the below steps to save a file in desired location:
Step1: Save As window getting opened(with the default downloads location, with file name as DOC)
Step2: entering the file name as "D:\temp\sample.pdf" (which is getting entered in the edit bar)
Step3: clicking the save button (button clicked, file downloaded in the default location rather than the "D:\temp" location)
I have created an .exe with the below .au3 script
WinWait("[CLASS:#32770]","",10)
Sleep(2000)
ControlSetText("Save As", "", "Edit1", $CmdLine[1])
Sleep(5000)
ControlClick("Save As", "", "Button1");
Sleep(5000)
On clicking save, it is getting saved in the default location rather than the specified location.
The below code, executing the script.
IO.popen('autoit_script.exe D:\temp') #Ruby Code
Is there a way to sort it out?
It depends on the software you are trying to automate but usually this happens because the software is not recognizing there is a change in the file save path box. The problem is in how ControlSetText works. Try using ControlSend with some error checking to make sure the file path you are try to set is getting put in right. Sometimes you have to play with a few different variations to see what works with the software you are automating. Here are two examples you can try:
Example one:
WinWait("[CLASS:#32770]", "", 10)
If Not #error Then ;make sure the window was found
$hWin = WinGetHandle("[CLASS:#32770]") ;get window handle
ControlSetText($hWin, "", "Edit1", $CmdLine[1]) ;set text
ControlFocus($hWin, "", "Edit1") ;just to make sure we have focus
ControlSend($hWin, "", "Edit1", "{ENTER}")) ;should work like click button 1 but you will have to check
;better then a sleep
$hTimer = TimerInit() ; Begin the timer and store the handle in a variable.
Do
Until WinExists($hWin) = 0 Or TimerDiff($hTimer) >= 10000
EndIf
Example two:
WinWait("[CLASS:#32770]", "", 10)
If Not #error Then ;make sure the window was found
$hWin = WinGetHandle("[CLASS:#32770]") ;get window handle
While 1
ControlSetText($hWin, "", "Edit1", "") ;just makes sure there is no text in the control text
ControlFocus($hWin, "", "Edit1") ;just to make sure we have focus
ControlSend($hWin, "", "Edit1", $CmdLine[1])) ;set text using ControlSend
If ControlGetText($hWin, "", "Edit1") = $CmdLine[1] Then ExitLoop ;makes sure that the control got ALL of the text before exiting loop
WEnd
ControlClick($hWin, "", "Button1");
;better then a sleep
$hTimer = TimerInit() ; Begin the timer and store the handle in a variable.
Do
Until WinExists($hWin) = 0 Or TimerDiff($hTimer) >= 10000
EndIf
I tried to implement a save-as-ftp button in Rebol embedded editor. Implementation of the save-as-button is this:
save-as-ftp: has [file-content][
file-content: t1/text
prefs-file: rejoin [_self-path %ftp.preferences.txt]
either exists? prefs-file [
prefs-ftp: construct load prefs-file; see article application configuration file
user: prefs-ftp/user
password: prefs-ftp/password
server-path: prefs-ftp/server-path
][
user: ask "User: "
password: ask/hide "Password: "
server-path: ask "Server-Path: "
]
view ftp-view: layout [
origin 10x10 space 8x4
style btn btn 140
ftp-field: text bold "" 140 center
pad 0x4
btn-enter 140 "Save" #"s" [hide-popup result: ftp-field/text]
btn red + 50 "Quit - No Save" [hide-popup quit-now]
]
file-target: result
ftp-target: rejoin [ftp:// user ":" password "#" server-path file-target]
write ftp-target file-content
print ["uploaded" file-target "to" rejoin [ftp:// "XXXXXXX" ":" "XXXXXXX" "#" server-path]]
true
]
My problem is with view ftp-view : I cannot even type in ftp-field text box as the popup window loses focus.
hide-popup is used to close a modal window.
A modal window is opened by using 'inform
I don't see any modal windows here.
Oh .. this is a button or something that you are attaching to the rebol editor?
I modified mine some years ago to edit ftp files ... I'll have to see if I can find what I did.