Skip to main content
added 804 characters in body
Source Link
statox
  • 51.1k
  • 19
  • 158
  • 237

You should use getpos():

To save you position in a variable:

let save_pos = getpos(".") 

getpos() takes as argument a mark, here "." represents the current position of your cursor.

And to restore it:

call setpos('.', save_pos) 

Here the first argument indicate that you will move the mark of the current position of your cursor (hence your current position) and the second is where to put the mark (the position that you saved earlier).

Your function would look like this:

function! DollarSplit() let save_pos = getpos(".") normal! 6|r$ " replaces the 6th caracter in line with a $ call setpos(".", save_pos) endfunction 

For more details see: :h getpos() and :h setpos()


For more details about your usage of execute: this function will take a string and execute it. Your string can be only hardcoded characters between double quotes or the contents of variables.

When you write

execute col_number."|" 

If you are on the 12th column the expanded string will be 12|. Execute will try to execute this command but it won't work because 12| is not a vimscript function but a normal mode command.

To execute it from a vimscript you have to say "execute it as if I had typed it in normal mode`, that's what normal is used for.

So without the execute you would have wrote:

normal 12| 

Now to make your execute call work you have to add the normal keyword to your expanded string, like this:

execute "normal " . col_number . "|" 

You should use getpos():

To save you position in a variable:

let save_pos = getpos(".") 

getpos() takes as argument a mark, here "." represents the current position of your cursor.

And to restore it:

call setpos('.', save_pos) 

Here the first argument indicate that you will move the mark of the current position of your cursor (hence your current position) and the second is where to put the mark (the position that you saved earlier).

Your function would look like this:

function! DollarSplit() let save_pos = getpos(".") normal! 6|r$ " replaces the 6th caracter in line with a $ call setpos(".", save_pos) endfunction 

For more details see: :h getpos() and :h setpos()

You should use getpos():

To save you position in a variable:

let save_pos = getpos(".") 

getpos() takes as argument a mark, here "." represents the current position of your cursor.

And to restore it:

call setpos('.', save_pos) 

Here the first argument indicate that you will move the mark of the current position of your cursor (hence your current position) and the second is where to put the mark (the position that you saved earlier).

Your function would look like this:

function! DollarSplit() let save_pos = getpos(".") normal! 6|r$ " replaces the 6th caracter in line with a $ call setpos(".", save_pos) endfunction 

For more details see: :h getpos() and :h setpos()


For more details about your usage of execute: this function will take a string and execute it. Your string can be only hardcoded characters between double quotes or the contents of variables.

When you write

execute col_number."|" 

If you are on the 12th column the expanded string will be 12|. Execute will try to execute this command but it won't work because 12| is not a vimscript function but a normal mode command.

To execute it from a vimscript you have to say "execute it as if I had typed it in normal mode`, that's what normal is used for.

So without the execute you would have wrote:

normal 12| 

Now to make your execute call work you have to add the normal keyword to your expanded string, like this:

execute "normal " . col_number . "|" 
added 724 characters in body
Source Link
statox
  • 51.1k
  • 19
  • 158
  • 237

You should use getpos():

To save you position in a variable:

let save_pos = getpos(".") 

getpos() takes as argument a mark, here "." represents the current position of your cursor.

And to restore it:

call setpos('.', save_pos) 

Here the first argument indicate that you will move the mark of the current position of your cursor (hence your current position) and the second is where to put the mark (the position that you saved earlier).

Your function would look like this:

function! DollarSplit() let save_pos = getpos(".") normal! 6|r$ " replaces the 6th caracter in line with a $ call setpos(".", save_pos) endfunction 

For more details see: :h getpos() and :h setpos()

You should use getpos():

To save you position in a variable:

let save_pos = getpos(".") 

And to restore it:

call setpos('.', save_pos) 

You should use getpos():

To save you position in a variable:

let save_pos = getpos(".") 

getpos() takes as argument a mark, here "." represents the current position of your cursor.

And to restore it:

call setpos('.', save_pos) 

Here the first argument indicate that you will move the mark of the current position of your cursor (hence your current position) and the second is where to put the mark (the position that you saved earlier).

Your function would look like this:

function! DollarSplit() let save_pos = getpos(".") normal! 6|r$ " replaces the 6th caracter in line with a $ call setpos(".", save_pos) endfunction 

For more details see: :h getpos() and :h setpos()

Source Link
statox
  • 51.1k
  • 19
  • 158
  • 237

You should use getpos():

To save you position in a variable:

let save_pos = getpos(".") 

And to restore it:

call setpos('.', save_pos)