Skip to main content
added 201 characters in body
Source Link
D. Ben Knoble
  • 27.7k
  • 3
  • 31
  • 68

You can use the zero-width \%# to match the cursor position, so something like

substitute/\%#\s\+$//e

on the current line, and your other pattern on the other lines:

global/\%#/substitute/\%#\s\+$//e
vglobal/\%#/substitute/\s\+$//e

Unfortunately, by the time either :substitute is happening, the cursor position has moved. So let's save it and make the matches more exact with \%123l and \%123c for matching exact lines and columns:

let [_, line, col, _, _] = getcurpos()
execute printf('global/\%%%dl/substitute/\%%%dc\s\+$//e', line, col)
execute printf('vglobal/\%%%dl/substitute/\s\+$//e', line)

But now that we already have the line number, we don't need the first :global:

let [_, line, col, _, _] = getcurpos()
execute printf('%d substitute/\%%%dc\s\+$//e', line, col)
execute printf('vglobal/\%%%dl/substitute/\s\+$//e', line)

Then you can surround it with winsaveview() and winrestview() to handle the cursor position.

PS technically the first substitute does not need the line number, but being explicit does not hurt.

You can use the zero-width \%# to match the cursor position, so something like

substitute/\%#\s\+$//e

on the current line, and your other pattern on the other lines:

global/\%#/substitute/\%#\s\+$//e
vglobal/\%#/substitute/\s\+$//e

Unfortunately, by the time either :substitute is happening, the cursor position has moved. So let's save it and make the matches more exact with \%123l and \%123c for matching exact lines and columns:

let [_, line, col, _, _] = getcurpos()
execute printf('global/\%%%dl/substitute/\%%%dc\s\+$//e', line, col)
execute printf('vglobal/\%%%dl/substitute/\s\+$//e', line)

But now that we already have the line number, we don't need the first :global:

let [_, line, col, _, _] = getcurpos()
execute printf('%d substitute/\%%%dc\s\+$//e', line, col)
execute printf('vglobal/\%%%dl/substitute/\s\+$//e', line)

You can use the zero-width \%# to match the cursor position, so something like

substitute/\%#\s\+$//e

on the current line, and your other pattern on the other lines:

global/\%#/substitute/\%#\s\+$//e
vglobal/\%#/substitute/\s\+$//e

Unfortunately, by the time either :substitute is happening, the cursor position has moved. So let's save it and make the matches more exact with \%123l and \%123c for matching exact lines and columns:

let [_, line, col, _, _] = getcurpos()
execute printf('global/\%%%dl/substitute/\%%%dc\s\+$//e', line, col)
execute printf('vglobal/\%%%dl/substitute/\s\+$//e', line)

But now that we already have the line number, we don't need the first :global:

let [_, line, col, _, _] = getcurpos()
execute printf('%d substitute/\%%%dc\s\+$//e', line, col)
execute printf('vglobal/\%%%dl/substitute/\s\+$//e', line)

Then you can surround it with winsaveview() and winrestview() to handle the cursor position.

PS technically the first substitute does not need the line number, but being explicit does not hurt.

added 583 characters in body
Source Link
D. Ben Knoble
  • 27.7k
  • 3
  • 31
  • 68

You can use the zero-width \%# to match the cursor position, so something like

substitute/\%#\s\+$//e

on the current line, and your other pattern on the other lines:

global/\%#/substitute/\%#\s\+$//e
vglobal/\%#/substitute/\s\+$//e

Unfortunately, by the time either :substitute is happening, the cursor position has moved. So let's save it and make the matches more exact with \%123l and \%123c for matching exact lines and columns:

let [_, line, col, _, _] = getcurpos()
execute printf('global/\%%%dl/substitute/\%%%dc\s\+$//e', line, col)
execute printf('vglobal/\%%%dl/substitute/\s\+$//e', line)

But now that we already have the line number, we don't need the first :global:

let [_, line, col, _, _] = getcurpos()
execute printf('%d substitute/\%%%dc\s\+$//e', line, col)
execute printf('vglobal/\%%%dl/substitute/\s\+$//e', line)

You can use the zero-width \%# to match the cursor position, so something like

substitute/\%#\s\+$//e

on the current line, and your other pattern on the other lines:

global/\%#/substitute/\%#\s\+$//e
vglobal/\%#/substitute/\s\+$//e

You can use the zero-width \%# to match the cursor position, so something like

substitute/\%#\s\+$//e

on the current line, and your other pattern on the other lines:

global/\%#/substitute/\%#\s\+$//e
vglobal/\%#/substitute/\s\+$//e

Unfortunately, by the time either :substitute is happening, the cursor position has moved. So let's save it and make the matches more exact with \%123l and \%123c for matching exact lines and columns:

let [_, line, col, _, _] = getcurpos()
execute printf('global/\%%%dl/substitute/\%%%dc\s\+$//e', line, col)
execute printf('vglobal/\%%%dl/substitute/\s\+$//e', line)

But now that we already have the line number, we don't need the first :global:

let [_, line, col, _, _] = getcurpos()
execute printf('%d substitute/\%%%dc\s\+$//e', line, col)
execute printf('vglobal/\%%%dl/substitute/\s\+$//e', line)
Source Link
D. Ben Knoble
  • 27.7k
  • 3
  • 31
  • 68

You can use the zero-width \%# to match the cursor position, so something like

substitute/\%#\s\+$//e

on the current line, and your other pattern on the other lines:

global/\%#/substitute/\%#\s\+$//e
vglobal/\%#/substitute/\s\+$//e