Repositioning the Current Line a la VIM

47 views
Skip to first unread message

Christopher Stone

unread,
Jan 2, 2015, 11:29:22 PM1/2/15
to BBEdit-Talk
Hey Folks,

Due to Vlad's recent request (and my own intention to get around to it eventually) I finally tackled this little task.

Repositioning the current line at top and bottom was quite simple with Keyboard Maestro.  I just used the the key-sequence (PgUp/PgDn)-Ctrl-A.

That did the job and was very fast but unfortunately didn't preserve the selection.

I then wrote the reposition-line-at-middle script based on where BBEdit puts the line when you reposition with AppleScript and an offset.

After that I played with the offsets and got scripts working for reposition-line-at-top and reposition-line-at-bottom.

The problem with this method is that it depends upon the window size being fixed, and you have to adjust the offset for your font-size and and window-height.

The error-handler is appended at the end and should be added to each saved script.

These work much faster when run from BBEdit's own Script Menu.

Hopefully there are others besides Vlad and myself who'll find them useful.

--
Best Regards,
Chris

-------------------------------------------------------------------------------------------
# Auth: Christopher Stone <script...@thestoneforge.com>
# dCre: 2015/01/02 21:50
# dMod: 2015/01/02 21:59
# Appl: BBEdit
# Task: Reposition Current Line at Top.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @BBEdit, @Line, @Reposition 
-------------------------------------------------------------------------------------------

try
  
  set lineOffset to 14
  
  tell application "BBEdit"
    tell text of front text window
      set _sel to selection
      set selectLine to (startLine of _sel) + lineOffset
      select insertion point before line 1
      select line selectLine
      select _sel
    end tell
  end tell
  
on error e number n
  stdErr(entruetrueof me
end try

-------------------------------------------------------------------------------------------


-------------------------------------------------------------------------------------------
# Auth: Christopher Stone <script...@thestoneforge.com>
# dCre: 2015/01/02 21:50
# dMod: 2015/01/02 21:59
# Appl: BBEdit
# Task: Reposition Current Line at Middle.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @BBEdit, @Line, @Reposition 
-------------------------------------------------------------------------------------------

try
  
  set lineOffset to 14
  
  tell application "BBEdit"
    tell text of front text window
      set _sel to selection
      set selectLine to (startLine of _sel) - lineOffset
      select insertion point before line -1
      select line selectLine
      select _sel
    end tell
  end tell
  
on error e number n
  stdErr(entruetrueof me
end try

-------------------------------------------------------------------------------------------


-------------------------------------------------------------------------------------------
# Auth: Christopher Stone <script...@thestoneforge.com>
# dCre: 2015/01/02 21:50
# dMod: 2015/01/02 21:59
# Appl: BBEdit
# Task: Reposition Line at Bottom.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @BBEdit, @Line, @Reposition.
-------------------------------------------------------------------------------------------

try
  
  set lineOffset to 44
  
  tell application "BBEdit"
    tell text of front text window
      set _sel to selection
      set selectLine to (startLine of _sel) - lineOffset
      select insertion point before line -1
      select line selectLine
      select _sel
    end tell
  end tell
  
on error e number n
  stdErr(entruetrueof me
end try

-------------------------------------------------------------------------------------------


-------------------------------------------------------------------------------------------
--» HANDLERS
-------------------------------------------------------------------------------------------
on stdErr(enbeepFlagddFlag)
  set e to e & return & return & "Num: " & n
  if beepFlag = true then
    beep
  end if
  if ddFlag = true then
    tell me
      set dDlg to display dialog e with title "ERROR!" buttons {"Cancel""Copy""OK"default button "OK"
    end tell
    if button returned of dDlg = "Copy" then set the clipboard to e
  else
    return e
  end if
end stdErr
-------------------------------------------------------------------------------------------

Reply all
Reply to author
Forward
0 new messages