I am a long time emacs user learning Vim. Emacs lets me navigate in the mini-buffer (where I issue commands like C-x C-s) using the same navigation keyboard shortcuts as in any other buffer. For example, I can navigate forward one character using C-f, even while in the mini-buffer. I could also use the arrow keys, but they are too far away.
Is there any keyboard shortcut to navigate in Vim's command mode (:), without using the arrow keys -- equivalent to emacs C-f, C-b? Thanks.
Adding to Greg Hewgill's answer, you can use q: to open the command-line window, where you have any Vim editing power at your hand.
Some from the Vim help:
CTRL-B or <Home>
cursor to beginning of command-line
CTRL-E or <End>
cursor to end of command-line
CTRL-H
<BS> Delete the character in front of the cursor (see |:fixdel| if
your <BS> key does not do what you want).
<Del> Delete the character under the cursor (at end of line:
character before the cursor).
CTRL-W Delete the |word| before the cursor. This depends on the
'iskeyword' option.
CTRL-U Remove all characters between the cursor position and
the beginning of the line.
I have these in my .vimrc
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>
With the default key bindings, vim does not offer non-arrow-key navigation of the command line editing. However, see :help cmdline-editing for an example of how to use the :cnoremap command to set up alternate key bindings.
I achieved that with <C-p> and <C-n> to navigate previous and next commands respectively.
P.S I'm not making any custom binding like Tassos did.
Related
When opening the command line and pressing the up arrow or down arrow keys, it shows the commands there were typed the last time. Is there a way to map this behaviour? For example when I press ctrl p, I want vim to show me my previous command (make vim act as if I pressed the up arrow). The same thing for ctrl n for the next command.
How can I make this happen?
The CTRL-P and CTRL-N keystrokes already do what you want, they search your command history. See :help c_CTRL-P, which explains how it will "recall older command-line from history."
The way CTRL-P and CTRL-N work differs slightly from the up and down arrow, in that the arrows will only go through the items in history that start with the characters you typed. So :e, space, up arrow will go to the last command you used to open a file for editing. See :help c_<Up> for details.
You can remap them so that they do the same as their counterpart, by using the cnoremap command, which creates mappings for keystrokes typed while in the Vim command-line.
For example, to make CTRL-P and CTRL-N behave the same as the arrows (complete respecting the prefix), you can use the following commands to create a (somewhat naive) mapping:
cnoremap <C-P> <Up>
cnoremap <C-N> <Down>
The shortcoming of this approach is that CTRL-P and CTRL-N behave differently on a wildmenu, so a more complete mapping would be:
cnoremap <expr> <C-P> wildmenumode() ? "\<C-P>" : "\<Up>"
cnoremap <expr> <C-N> wildmenumode() ? "\<C-N>" : "\<Down>"
That will preserve the original behavior of CTRL-P and CTRL-N in the wildmenu.
When auto-completing words in Vim using C-P and pressing Enter to select the first candidate, without first using the arrow-keys to select between candidates, Vim will jump the cursor to the next line.
Conversely, if I first use the arrow-keys to select between candidates, Vim won't jump to the next line.
How can I prevent Vim from jumping to the next line, when selecting first candidate using Vim's auto-completion feature C-P?
When popup (completion) menu is active, you're supposed to use <C-N> <C-P> <C-E> <C-Y> instead of <Down> <Up> <Esc> <CR> respectively (note that all these keys behave a little differently). But if you really like it you can use a mapping:
inoremap <expr><CR> pumvisible() ? "\<C-Y>" : "\<CR>"
See also Improve completion popup menu Vim tip.
I'd like to use gvim to view files with long lines. It's a table, so I'm not wrapping the lines.
Is this possible to configure gvim so arrows navigation will be like in "most" tool? Arrow key will move the whole screen 1 character lef/right/top/bottom?
Thanks a lot.
I think this should do what you want.
set nocompatible
set nowrap
set virtualedit=all
nnoremap <Left> zh
nnoremap <Right> zl
nnoremap <Up> <C-y>
nnoremap <Down> <C-e>
If you want the same behavior in insert mode, add the same mappings again as a second set, but use inoremap instead of nnoremap.
The virtualedit setting will allow the cursor to move beyond the end of the line and continue on as if the line had infinite whitespace to the right.
NOTE: virtualedit is only available if Vim was compiled with that feature. You can check with :version. If this feature is available, you should see a + next to it, e.g. +virtualedit.
I chose to use gvim as my primary text editor, but still would like it to behave more like other visual editors I'm used to. One aspect of that is that when I have wrap enabled (set linebreak) and use the arrow keys <Up> and <Down> in insert mode, I'd like to move the cursor to the previous / next screen line, not logical line. This can be achieved using the mappings:
inoremap <Up> <C-O>gk
inoremap <Down> <C-O>gj
...and everything is fine.
Except, in select mode. While using <S-Right>, <S-Left> works as expected, <S-Up> and <S-Down> still move in terms of logical lines. On http://vim.wikia.com I found the following suggestion for additional mappings:
noremap <S-Up> vgk
noremap <S-Down> vgj
inoremap <S-Up> <C-O>vgk
inoremap <S-Down> <C-O>vgj
The two latter mappings now enable that when I start a selection by pressing <S-Down> in insert mode, I get a selection from the previous position to the same position in the next screen line. But when I already have a selection (already am in select mode), pressing <S-Down> moves one line down but loses the selection.
I would expect to achieve this it would be necessary to have specific mapping for select mode (snoremap), but wasn't able to figure out how to do it.
Because of the discussion with glts whether select mode is useless or not, maybe some background information is in order: Select mode appears to be vim's attempt to provide something close to the selection behavior found in most other visual editors on MS Windows, Mac OS, and even Linux, which in turn is inspired by IBM's CUA. Since it is only really useful with the accompanying keyboard mappings ^C, ^X, ^V, it is meant to be used in conjunction with mswin.vim which provides these mappings. My question is motivated by an attempt to complement these mappings such that select mode works as expected also for wrapped text.
For Select mode, if that is really what you mean, these mappings would work:
vnoremap <S-Up> gk
vnoremap <S-Down> gj
imap <S-Up> <Esc>gh<S-Up>
imap <S-Down> <Esc><Right>gh<S-Down>
nmap <S-Up> gh<S-Up>
nmap <S-Down> gh<S-Down>
Note the gh command (Select mode) instead of v (Visual mode).
But be warned that – and this is an assumption on my part – the general populace of Vim users shun Select mode, seeing as it runs counter to the Vim way.
Visual mode is much more powerful, since in addition to replacing text, you can also yank it into a register, make it uppercase or lowercase, change the extent of the Visual selection, etc. etc. Have a look at :h vim-modes.
Here is what I came up with myself:
Make <Up> and <Down> move to previous / next screen line. (In insert mode, <C-O> switches to normal mode for one command. In normal mode, gj and gk are the 'move by screen line' commands.)
inoremap <Up> <C-O>gk
inoremap <Down> <C-O>gj
Same for <S-Up> and <S-Down> in insert mode, entering select mode. (In normal mode, v enters visual mode. gj and gk work also in visual mode. In visual mode, <C-G> enters select mode.)
inoremap <S-Up> <C-O>vgk<C-G>
inoremap <S-Down> <C-O>vgj<C-G>
Same for <S-Up> and <S-Down> in select mode. (In select mode, <C-O> switches to visual mode for one command.)
snoremap <S-Up> <C-O>gk
snoremap <S-Down> <C-O>gj
Right now I am using:
nnoremap <cr> :nohlsearch<cr><cr>k
But after I press Enter my cursor goes to beginning of line.
I have additional <cr>k because I want to use default <Enter> behaviour for example when I try to open file in Ack results quickview.
If you want Enter to keep the default behavior, then this mapping should take care of it.
nnoremap <CR> :nohlsearch<CR><CR>
It turns off search highlighting, moves the cursor down, and only applies to normal mode.
An alternative approach is to locally override the new behavior in the quickfix window and the command-line window, where <CR> has special meaning:
:nnoremap <CR> :nohlsearch<CR>
:autocmd BufReadPost quickfix nnoremap <buffer> <CR> <CR>
:autocmd CmdwinEnter * nnoremap <buffer> <CR> <CR>