5

I have an ora file that is 200,000 lines long but the last 60,000 some lines are all blank carriage returns/whitespace.

I know G jumps to the end of a file, but I do I configure vim to jump to the last line or character that is non-whitespace and non-carriage return?

luxon
  • 417
  • 5
  • 14

3 Answers3

6

G?\SEnter

Go to end of document, search backwards, find a non-whitespace, go. If you need a mapping for that,

nnoremap <Leader>G G?\S<CR>:noh<CR>

EDIT: This will not work if the last line is not blank. Here's a modification that will work if the last character is not blank:

nnoremap <Leader>G G$?\S<CR>:noh<CR>

To fix that, it gets a bit complicated. We'll go to the last character of the file, and test whether it's a whitespace or not. If it is not a whitespace, we're done; but if it is, we can use the previous solution. (This also saves the state of things we mess up, so it will interfere minimally with your work.)

function! LastChar()
  let oldsearch = @/
  let oldhls = &hls
  let oldz = @z

  norm G$"zyl
  if match(@z, '\S')
    exe "norm ?\\S\<CR>"
  endif

  let @/ = oldsearch
  let &hls = oldhls
  let @z = oldz
endfunction

nnoremap <Leader>G :call LastChar()<CR>
Amadan
  • 191,408
  • 23
  • 240
  • 301
  • What if the last line is not blank, but have spaces on its end? – john c. j. Mar 09 '20 at 10:55
  • Consider a file with 3 lines of code: `aaa`, `bbb`, `ccc`. The `ccc` line have trailing spaces on its end. You want to move to the last `c`, but it willn't work. – john c. j. Mar 09 '20 at 11:22
  • Initially, the cursor is on first `a`. I press `G`, the cursor moves to the first `c`. I type `?\S` and press Enter, the cursor moves to the last `b`. This behavior is OK. What I talked for is that it would be great to have a single solution which will work for both cases. That is, for the case described in the original post and for the case that I described here in comments. EDIT: Oh, I see the edit in your last comment :D – john c. j. Mar 09 '20 at 11:33
  • Ah okay I finally see what you meant. Sorry about being thick :P Unfortunately, I can't come up with something simple if the last character is a space. – Amadan Mar 09 '20 at 11:36
  • Well, in a such a case we can use `Gg_`. – john c. j. Mar 09 '20 at 11:40
  • @johnc.j. The last edit might address your concerns :) – Amadan Mar 09 '20 at 12:15
  • Works perfect, thank you. I have already upvoted, would upvote twice if I could :D – john c. j. Mar 09 '20 at 18:26
2

(count)g_ will go to the last non-blank character of a line count lines below the current line. So a silly way you could do it is:

999999999g_

As a mapping:

nnoremap <leader>g_ 999999999g_
Andy Ray
  • 30,372
  • 14
  • 101
  • 138
0

I use

nnoremap G G{}

It jumps to the line after the last paragraph.

Andy A.
  • 1,392
  • 5
  • 15
  • 28
david2431
  • 1
  • 2