2

I'm looking for a way to make a line be only logically broken at the end of the available space. Here's an example: let's suppose I have a really long line (i.e. a string without any \n in it), and to render it on the screen, ViM must split it, so, by turning on line numbers I see

1: This is the beginning of my very very very long line that does not fit 
ViM's window and therefore it must be split into two lines.
2: And this is another line of text

Now, if I'm in normal mode, I can move from line 1 to line 2 by using j, but I have no ways to move from the word "beginning" to the word "therefore", apart from gj which I find slow. I tried to remap j to gj, only for the filetypes I'm interested in (I wouldn't ever do this with C source files for example), but this gives me another issue: relative numbers do not work anymore. If I'm on line 1 and type d1j only the first two "logical lines" are deleted (which in my example they correspond to the whole line 1). So I'm looking for a way to move inside the lines pretending they're logical lines (i.e. by gjing or gking) and work with the physical lines.

I tried with the textwidth option, but it physically breaks the lines, so it's not exactly what I'm looking for.

Thanks in advance.

  • 1
    Your sentence about d1j is confusing—only the first two logical lines... (what)? If you want dj to work normally, dont omap j; if you want dj == dgj, you have to omap j – D. Ben Knoble Mar 3 at 22:57
  • @D.BenKnoble As a logical line, I mean a line that is split by ViM and not by a \n. So what physically on the file is a single line (i.e. a string of characters followed by a \n), in the ViM editor, depending on the size of the window, can become more than one line of text. I call logical line the line of text I see in the ViM editor before it wraps it and makes the rest of the line go in another one. Anyway, thanks to you and the answer by husB, I figured out the problem and solved it. – LuxGiammi Mar 4 at 9:09
  • 1
    glad you solved your problem, but im not sure you understood my question—your sentence about d1j is logically and grammatically incomplete. It effectively reads “if i do X, only the first two logical lines.” Do you see how there’s something missing? Only the first two logical lines... what? What do they or should they do? – D. Ben Knoble Mar 4 at 13:08
  • ..."are deleted". I'm sorry about it, but I did not notice it. Sorry for the inconvenience. :-( I edited my question accordingly. – LuxGiammi Mar 5 at 14:28
  • 1
    no problem! Nothing to worry about—glad you got it squared away! – D. Ben Knoble Mar 5 at 14:36
4

I can think of two solutions to this:

1: Use normal mode mappings only; do not use an operator-pending mode mapping

As @D.Ben Knoble mentioned in his comment, instead of

noremap j gj
noremap k gk 

use

nnoremap j gj
nnoremap k gk

The former maps j and k in normal, visual, and operator-pending modes, while the latter maps j and k only in normal mode. With the latter mappings, after an operator (such as d, c, or y), j and k remain as-is.


2: Use an expression mapping to check if a count is given

noremap <expr> j v:count ? 'j' : 'gj'
noremap <expr> k v:count ? 'k' : 'gk'

These check if a count has been supplied. If there is a count, j or k is used; otherwise gj or gk is used.

In this way, 1j or 1k moves up or down a whole line, while j or k moves up or down a screen line.


For more information, see

  • :h map-modes
  • :h map-expression
  • :h count-variable
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.