Vim g0 and g$ for soft-wrapped text?

155 views
Skip to first unread message

Linfeng Li

unread,
Oct 20, 2021, 8:28:03 AM10/20/21
to Ajax.org Cloud9 Editor (Ace)
Hi

With soft-wraps at 80-th col, for the Vim key-bindings, is there a way to get g0 and g$ to work for jumping to the beginning/end of the current soft-wrapped line?

Currently, only 0 and $ are working in Vim's normal mode, to move the cursor to the beginning/end of the full line under the cursor. (This is observed on Overleaf which said they are using the Ace editor.)

Many thanks!

-Linfeng

Harutyun Amirjanyan

unread,
Oct 20, 2021, 5:09:25 PM10/20/21
to ace-d...@googlegroups.com
hi
if you are using the user script to customize keybindings you can add

ace.require("ace/keyboard/vim").handler.defaultKeymap.push({
   keys: "g$", type: "motion", motion: "moveToLineEnd"
}, {
   keys: "g^", type: "motion", motion: "moveToLineStart"
}, {
   keys: "g0", type: "motion", motion: "moveToLineStart"
})
ace.require("ace/keyboard/vim").Vim.defineMotion("moveToLineEnd", function(cm) {
    cm.ace.selection.moveCursorLineEnd()
    cm.ace.selection.clearSelection()
    return cm.getCursor()
})
ace.require("ace/keyboard/vim").Vim.defineMotion("moveToLineStart", function(cm) {
    cm.ace.selection.moveCursorLineStart()
    cm.ace.selection.clearSelection()
    return cm.getCursor()
})

i'll add these to ace itself in the next version

Linfeng Li

unread,
Oct 20, 2021, 5:28:48 PM10/20/21
to Ajax.org Cloud9 Editor (Ace)
Hi Harutyun,

Many thanks for the very quick solution. 

I have updated my Overleaf userscript here. Now I can navigate the wrapped long lines more easily.

Btw, is there a global setting that can adds hard line-breaks at 80-th col? I read from this post that one can force hard line-breaks with `gq`.

All the best,

-Linfeng

Harutyun Amirjanyan

unread,
Oct 21, 2021, 1:38:48 PM10/21/21
to ace-d...@googlegroups.com

Hi
the version on overleaf still uses ace 1.4.12 that doesn't support gq

As for automatic wrapping, could you try if this
works for your usecase?

Linfeng Li

unread,
Oct 21, 2021, 4:36:33 PM10/21/21
to ace-d...@googlegroups.com
Hi,

Thanks for the hard-wrapping userscript. It almost get the job done.

One problem: when hard-wrapping happens, the wrapped "word" gets chopped in halves - the first half that reached 80th col evaporated except for the 80th char, which gets forward to the next line along with the remaining of the word. (I updated the column number 40 in your script to be 80. Same issue happened with 40-columns as the limit.)

To be more precise, suppose "col-umn" is the word that runs over the 80th column, at character "l". Then, "co" vanishes, and "l" gets forward to the next line, along with the remaining keystrokes.

Here is a test with exactly 80 chars:
This is typing on a long long line, to see if things are wrapped at the 80th col

Here is what I tried and the difference. 

Looking forward to quick fix, if possible.

Many thanks again!

-Linfeng



Harutyun Amirjanyan

unread,
Oct 21, 2021, 5:30:30 PM10/21/21
to ace-d...@googlegroups.com
There was a one off bug, thanks for reporting it!
The updated gist should work now.
Do you think this functionality is useful enough to be included in the vim mode by default, or does it need more features added?
(I think this does not accurately mimic the way that vim handles textwidth, mainly in wrapping comments and not wrapping equations)

Thanks,
Harutyun

Linfeng Li

unread,
Oct 21, 2021, 7:02:20 PM10/21/21
to ace-d...@googlegroups.com
Hi Harutyun,

Thanks for the quick fix. I think it makes sense to make the feature available, since it is mostly replicating what Vim is doing:
  1. When one keeps typing, hard-wraps are inserted when one reaches 80-char;
  2. When editing long lines with more than 80 chars, as long as the edit is conducted beyond the 80th char, the whole "line" gets trimmed into pieces.
I can live without gq to format my text on Overleaf (due to its own versioning choice), and I can live with the fact that hard line-breaks for comments are inserted without a leading comment sign. For equations, my take is that one should not write super-long lines for equations to begin with. Since LaTeX ignores all "hard" line-breaks, when there is need to write a super long equation, I usually keep one short bit across a good number of lines.

One thought about the hard-wrap feature: since hard line-breaks are inserted regardless of the "environment", be it comment or equation, it should help to have a ":set nowrap" command for overriding the hard-wraps for the current editing session. I recall myself doing this in Vim when I screw up my wrap-settings and that I need to write very long comments :)

All the best,

-Linfeng


Linfeng Li

unread,
Oct 25, 2021, 11:47:50 PM10/25/21
to ace-d...@googlegroups.com
One observation to add: the hard-wrap ignores the current indentation level, and starts dropping texts at column 1 in the next line.

Linfeng Li

unread,
May 12, 2022, 10:26:55 AM5/12/22
to Ajax.org Cloud9 Editor (Ace)
Thanks again for the auto-hard-wrap script. While I don't have a good way to get the gq to work for formatting long lines upon selecting a few lines with various length in visual mode, thanks to this auto-wrap script, the following operation leads to a set of well-wrapped lines.
wrapping_lines.gif

Then, the following lines written in AutoHotKey will make it faster to format the hard-wrapped lines. (The script assumes that all lines to be wrapped are selected in visual mode. The triggering hotkey is Ctrl+Shift+Q. It is another chore to get the key-sequence gq to wrok in AHK.)

#IfWinActive Online LaTeX Editor Overleaf - Vivaldi ahk_exe vivaldi.exe
    ^+q::
        send J
        send A
        send k
        send {bs}
        send {esc}
    return
#IfWinActive

Then, the operation will look like the following:
wrapping_with_Ctrl+Shift+Q.gif

Ideally, for a future formal release, long lines can be handled natively, in visual mode, with gq.
Reply all
Reply to author
Forward
0 new messages