How can I move around in the Vim command line?

Vim

Vim Problem Overview


If you're typing a command in Vim (I mean you've started with : and you're working in the bar at the bottom of the screen) is there a way to move the cursor around other than tapping the arrow keys? In particular, can you move it to the beginning, end, back n characters, or back one word?

Vim Solutions


Solution 1 - Vim

Tap Ctrl+F while in command-line mode (just after :). There you'll get command-line window which could be edited&navigated as a regular vim window (hjkl etc.).

See :h cmdline-window for details.

Solution 2 - Vim

Type

:h cmdline-editing

for details. I am listing a few of the interesting non-arrow commands that do something similar to what you want.

  • ctrl-B: cursor to beginning of command-line
  • ctrl-E: cursor to end of command-line
  • ctrl-W: delete the word before the cursor
  • ctrl-U: remove all characters between the cursor position and the beginning of the line

Solution 3 - Vim

To add to Maxim Kim's Answer,

In the Normal Mode ..

q: -> cmdline window for commands

q/ -> cmdline window for search forward

q? -> cmdline window for search backward

Ctrl-C or <CR> will take you out of cmdline-window

Solution 4 - Vim

  • ctrl+left arrow: move back a word
  • ctrl+right arrow - move forward a word
  • ctrl+b - back to the beginning of the line
  • ctrl+e - go to the end of the line
  • ctrl+w - remove one word before the cursor
  • ctrl+u - remove line
  • ctrl+f - if you need more editing power use ctrl+f and you will edit your command in normal mode. For example, if you want to move 5 characters to the left, use ctrl+f and then 5h.

Solution 5 - Vim

nnoremap q; q: to facilitate typing. usr_20.txt and cmdline.txt contains all useful infos.

Solution 6 - Vim

You can actually add your own movement keys. For example, I use the following in my .vimrc to make moving around the command mode finger-friendly in an hjkl way (abusing the ctrl key):

 " moving aroung in command mode
 cnoremap <c-h> <left>
 cnoremap <c-j> <down>
 cnoremap <c-k> <up>
 cnoremap <c-l> <right>
 cnoremap ^     <home>
 cnoremap $     <end>

where ^ and $ are really < ctrl-^ > and < ctrl-$ > respectivelly, typed as < c-v >< c-^ > and < c-v >< c-$ > in the .vimrc (for some reason < c-^ > and < c-$ > won't work, at least in my setting, but the former do)

Solution 7 - Vim

On Mac OS,

  • Shift+left arrow: move back a word
  • Shift+right arrow: move forward a word

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEthanView Question on Stackoverflow
Solution 1 - VimMaxim KimView Answer on Stackoverflow
Solution 2 - VimAlok SinghalView Answer on Stackoverflow
Solution 3 - VimJeffrey JoseView Answer on Stackoverflow
Solution 4 - VimkernixView Answer on Stackoverflow
Solution 5 - VimqeatzyView Answer on Stackoverflow
Solution 6 - VimDalkerView Answer on Stackoverflow
Solution 7 - VimMauView Answer on Stackoverflow