0

Capital A in Vim takes me to the last character in insert mode, but can I go to the nth-to-last character in insert mode (or append mode) in vim?

Example:

this is some t|ext

how to go specifically to the location of the | in insert mode, I would imagine something like <n>+A where n is an integer. I did see a similar question here: how-to-go-to-before-last-character-in-a-line and an answer suggested to use $i, to go to the second-to-last character in insert mode, but this can't go to the nth character.

[Edit]:

I only want to end up in insert mode, not necessarily do all the commands in insert mode but rather do them in normal mode.

9
  • 3
    that's what normal mode is for ;) Sep 15, 2021 at 17:09
  • 2
    Welcome to Vi and Vim!
    – filbranden
    Sep 15, 2021 at 19:14
  • 2
    @an4s911 You can also use Ctrl+O from Insert mode to enter a single Normal mode command and stay in Insert mode otherwise. So, for example, you can use Ctrl+O 20 | to go to column 20 in the current line (and continue in Insert mode) or Ctrl+O gM to go to the middle of the current line. Unfortunately there isn't a single Normal mode command to go to the nth-character before the end of the line, so Ctrl+O isn't that helpful then, but if you create a Normal-mode mapping to take the count and go to the nth character before the end, Ctrl+O can help you access it.
    – filbranden
    Sep 15, 2021 at 19:19
  • 2
    @filbranden @Christian as I understand it, OP does not ask to move while in insert mode, but to enter it afterwards as would A do.
    – Biggybi
    Sep 16, 2021 at 17:48
  • 1
    @an4s911 Yes gM takes you to the middle of the line, see :help gM.
    – filbranden
    Sep 16, 2021 at 20:00

2 Answers 2

1

I concocted a script that does what you want. I think it handles most corner cases, however you might find some bugs down the road if you decide to use it.

function! AWithCount(count) abort
  " default behaviour (no count)
  if !a:count
    "append to end of line, like `A`
    startinsert!
    return
  endif

  " line too short for `count` (`:h col())
  " `col('$')` => length of current line
  if col('$') - 1 < a:count
    " go to first non-whitespace, or to start if only whitespaces
    " -> `normal 0` / `normal _`
    " `:h =~` `:h trinary`
    normal (getline('.') =~ "^\\s\\+$") ? "0" : "_"
    startinsert
    return
  endif

  " place cursor and enter insert mode
  " -> `$X<left><right>`, X a number
  " `:h :exe`
  exe "normal $" . a:count . "\<left>\<right>"
  startinsert
endfunction

" `:h <silent>` to not cluter the command line
" <c-u> to clear things vim puts in command line
nnoremap <silent> A :<c-u>call AWithCount(v:count)<cr>
2
  • Thats really amazing. Could u please explain the code as well. The syntax is not that intuitively understandable
    – an4s911
    Feb 16, 2022 at 4:07
  • @an4s911 Vimscript can indeed turn out to be pretty cryptic. I added some comments to better explain the code, as well as help topics you may want to check out. This should clarify things a whole lot.
    – Biggybi
    Feb 16, 2022 at 9:03
0

<ALT-$> [count] hi where [count] is a number

  1. the Alt key to enter commands while in insert mode
  2. while pressing the Alt key press "$" to go to end of line
  3. press [count] "h" to go left [count] times
  4. press i to go into insert mode
3
  • 1
    Welcome to Vi and Vim! I'm not sure your answer is actually addressing the question. One part of the question is about moving while in Insert mode, while your answer seems to include Normal mode commands only... Your answer was also flagged due to being too short, make sure you further explain what the commands are doing, why they're needed, etc.
    – filbranden
    Sep 15, 2021 at 19:14
  • 1
    @fibrandon edited
    – Drek
    Sep 15, 2021 at 19:48
  • 1
    @filbranden by ‘in insert mode’ I actually meant doing the commands in normal mode and then finally ending up in insert.
    – an4s911
    Sep 16, 2021 at 18:56

Your Answer

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

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