Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Move forward/backwards one word in command mode?

Tags:

vim

vi

Lets say I am copying a file with the vim command mode and my cursor is at the end of the line.

:!cp path/to/original/file path/to/new/file

Is there a way I can jump back a word like I can in the shell by typing Esc b?

like image 303
David Tuite Avatar asked Jan 15 '23 15:01

David Tuite


2 Answers

You cannot use "Esc b" because, obviously, that would discard the command you where typing. However you can bind some keys to move around.

The question as already be answered here : Navigating in Vim's Command Mode

The easy way is just to add :

cnoremap <C-a> <Home>
cnoremap <C-e> <End>
cnoremap <C-p> <Up>
cnoremap <C-n> <Down>
cnoremap <C-b> <Left>
cnoremap <C-f> <Right>
cnoremap <M-b> <S-Left>
cnoremap <M-f> <S-Right>

In your .vimrc

like image 88
rks Avatar answered Jan 18 '23 06:01

rks


For entering and editing complex commands, you may like working directly in the command line window which is accessed with the normal mode command q:. See :h 20.5 and :h q:. Or if you are already in command mode, you can access the command line window with C-f.

  • For example, in normal mode type q: to get into the command line window. (or type C-f from command line mode.
  • You can move around previous commands using standard motions and you can edit as usual.
  • When you want to execute a command that you just edited, press enter in normal mode in this command line window. The line your cursor is on will be executed as a command in
    the window you were in before you opened the command line window.

Another option to consider is to edit/yank the command from another buffer. You can do this by yanking the desired text and pasting it in command mode by typing C-R n, where n is the register you yanked to.

BTW: I like the mappings that @rks provided. But if you don't have these mappings, you can use the out of the box commands. Look up :h c_<S-Left> and :h c_<S-Right> and :h 20.1.

like image 42
darcyparker Avatar answered Jan 18 '23 06:01

darcyparker