Whenever I press << or >> in insert or normal mode, I lose the cursor position, as it always goes back to the start of the line. Is there a setting to fix this?

Here is where I am right now, but am having some trouble figuring out why the below isn't working in normal mode:

inoremap            <leader>]   <C-O>>>
inoremap            <leader>[   <C-O><<
nnoremap    <exp>   <leader>]   col('.') == 1 ? '>>' : "D>>A<C-O>P<Esc>`["
nnoremap    <exp>   <leader>[   col('.') == 1 ? '<<' : "D<<A<C-O>P<Esc>`["
vnoremap            <leader>]   >gv
vnoremap            <leader>[   <gv

2 Answers 2


The option is called set nostartofline. However, it's a global which affects many(!) other commands in Vim and I'd rather not suggest to use it.

  • I think set nostartofline makes a lot of sense in most cases.
    – Biggybi
    Jun 20, 2020 at 9:35
  • 1
    @Biggybi I'm not so sure. For example, many people (and even many scripts) expect gg to go to the first char in file, not to the first line.
    – Matt
    Jun 20, 2020 at 10:03
  • Indeed, I never though of that.
    – Biggybi
    Jun 20, 2020 at 12:11
  • @Matt does my solution below seem adequate then to deal with this issue, or is there a better way to basically ignore nostartofline for the <<, >> commands?
    – David542
    Jun 20, 2020 at 16:35
  • @David542 Personally I just don't do anything with it. Okay, sometimes you got to the start of the line forcefully. And so what?
    – Matt
    Jun 20, 2020 at 16:42

I'm sure this is overkill, but this is what I end up doing to 'keep the cursor position' when in insert and normal mode:

inoremap    <expr>  <leader>]   col('.') == 1 ? '<C-O>>>' : '<C-O>D<C-O>>><C-O>A<C-O>P<Esc>`[i'
inoremap    <expr>  <leader>[   col('.') == 1 ? '<C-O><<' : '<C-O>D<C-O><<<C-O>A<C-O>P<Esc>`[i'
nnoremap    <expr>  <leader>]   col('.') == 1 ? '>>' : 'D>>A<C-O>P<Esc>`['
nnoremap    <expr>  <leader>[   col('.') == 1 ? '<<' : 'D<<A<C-O>P<Esc>`['
vnoremap            <leader>]   >gv
vnoremap            <leader>[   <gv

Any suggestions on how this could be improved would be great.

Update: there's already a way to do this in insert mode so we can use that to our advantage. The mapping then becomes:

inoremap <leader>]   <C-T>
inoremap <leader>[   <C-D>
nnoremap <leader>]   i<C-T><Esc>`[
nnoremap <leader>[   i<C-D><Esc>`[
vnoremap <leader>]   >gv
vnoremap <leader>[   <gv

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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