1

I have a peculiar situation where the value of a number on the nvim screen on which the cursor is sitting gets decremented by 1 when I (often accidentally) hit <C-x><C-s> in commandline mode (I moved a few months ago after over 25 years with emacs. So, I was wanting to map my <C-x><C-s> to save the file in commandline mode.

I tried adding:

cnoremap <C-x><C-s> :w<cr> 

to map my <C-x><C-s> to save the file, and put this line in my .config/nvim/init.vim but it does not seem to be making any difference in the behavior of the key combination.

What am I doing wrong. At this point, I would be fine disabling <C-x><C-s> altogether as a last resort.

6
  • 1
    What's console mode? cnoremap works in command-line mode. Did you mean to use :nnoremap instead?
    – Friedrich
    Jan 31 at 22:28
  • OK, command line mode, will edit. Jan 31 at 23:41
  • 1
    I don't see how this mapping would make sense in command-line mode. Is the root cause maybe confusion of Vim's modes? See :help vim-modes or Vivian's answer.
    – Friedrich
    Feb 1 at 7:10
  • 1
    There are some (largely historical, I believe) references to « command mode », which we now usually call Normal mode.
    – D. Ben Knoble
    Feb 1 at 14:24
  • Do you still have something open in your question? How can we help you further? Otherwise maybe could you accept one of the solution using the v button next to the arrow voting buttons. It allow the question to rest :-) Feb 1 at 14:29

1 Answer 1

2

One risk with the mapping:

cnoremap <C-x><C-s> :w<cr> 

is that that it assume that the command line is empty. If your command line is not empty it will merge the two.

E.g. if the command line contains :b it will run :b:w<CR>.

To avoid that confusion it is usually recommended to clear the command line first:

cnoremap <C-x><C-s> <c-u>w<cr>

Or in modern Vim:

cnoremap <C-x><C-s> <cmd>w<cr>

That being said the proposition of @Friedrich to do:

nnoremap <C-x><C-s> <cmd>w<cr>

will probably bring you closer to the Emacs experience.

From what you wrote ("the value of a number get decremented") make me believe that you actually hit Ctrl xCtrl s in normal mode. Indeed Ctrl x decrement the next integer on the line in normal mode.

And Ctrl a increases the next integer on the line (that I also found by accident).

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.