I have following vimrc file
map u <Up>
map n <Left>
map e <Down>
map i <Right>
noremap h i
noremap t w
noremap s b
Delete or changing word with dt or ct works, but when I want to delete inner word with dht or cht it doesn't work but dhw or chw works. Am I missing something in this remaping of the keys?
I have tried with map instead noreamp but it didn't worked.
Vim's operator-pending mode is likely not expecting two consecutive mappings of individual characters. However you can do a multi-character mapping.
noremap ht iw
noremap at aw
This will let you do dht, dat, cht, and cat, as well as vht and vat.
For multi-character maps, please be aware of the 'timeoutlen' option setting. This is normally defaulted to 1000ms. If you have a mapping for both h and for ht, then this will slow down your h mapping because Vim must wait up to 1000ms for you to possibly type t. You can reduce 'timeoutlen' to a shorter time but it must still be long enough to let you type both h and t.
The noremap command applies to Normal, Visual and Operator-Pending modes.
You probably want to specify only Operator-Pending and Visual modes and exclude Normal mode so that you don't slow down your other Normal mode mappings.
onoremap ht iw
onoremap at aw
vnoremap ht iw
vnoremap at aw
Related
Consider the unbinding of the arrow keys using
noremap <Left> <NOP>
noremap <Right> <NOP>
noremap <Up> <NOP>
noremap <Down> <NOP>
This works in normal mode, but it does not work in insert mode: one can still navigate with the arrow keys. As a countermeasure, one must include
inoremap <Left> <NOP>
inoremap <Right> <NOP>
inoremap <Up> <NOP>
inoremap <Down> <NOP>
But this doesn't really make sense to me, since I assume map and noremap should work in all modes, while prepending n/v/x/s/o/i/l/c specifies the mapping to work only within that specific mode. Is there a reason for this?
why there isn't an all-inclusive modal map, rather than issuing both map and map!
That's easy to explain: In insert mode mappings, Vim doesn't automatically switch to normal mode (you may want to stay in insert mode, though text translations are typically done via :iabb, not via :imap), so the set of applicable commands is totally different. For example, in normal mode Ctrl-U scrolls upwards, but in insert mode it deletes the entered characters in the line!
Prefixes like <C-O> temporarily switch from insert mode to normal mode. Actually, one often even has to define a different prefix for command line mode, too, as shown by this example:
noremap <C-Tab> :<C-U>tabnext<CR>
inoremap <C-Tab> <C-O>:tabnext<CR>
cnoremap <C-Tab> <C-C>:tabnext<CR>
So when defining mappings, always consider in which modes they are needed and whether they need remapping (:nmap vs. :noremap, prefer the latter).
:help map-overview
map (and noremap) are for normal, visual, select and operator-pending modes.
Contrary to what you might expect, noremap and map do not actually apply to all modes. Based on the very useful summary from :help map-listing, here is a list of the characters that can be prefixed (or suffixed in the case of !) to map, noremap, unmap, and mapclear, along with the modes that they apply to:
(none) – Normal, Visual, Select, and Operator-pending
n – Normal
v – Visual and Select
x – Visual
s – Select
o – Operator-pending
! – Insert and Command-line
i – Insert
c – Command-line
l – ":lmap" mappings for Insert, Command-line, and Lang-Arg
So a noremap mapping will have no effect in Insert or Command-line mode, and without consideration, may not work as intended in Visual, Select, or Operator-pending mode either.
However, mappings can be adapted to work in different modes, simply by changing mode and back in the mapping. For example, noremap mappings that issue command-line commands but only work in Normal mode can adapted to also work in the other modes as shown by this example:
noremap <C-Tab> :<C-U>set list!<CR>
inoremap <C-Tab> <C-O>:set list!<CR>
cnoremap <C-Tab> <C-C>:set list!<CR>:<Up>
noremap applies to the Normal, Visual, Select, and Operator-pending modes, for which :<C-U> enters Command-line mode then clears the current line in case Vim inserts a range; inoremap applies to Insert mode, for which <C-O>: temporarily exits to Normal mode then enters Command-line mode; and cnoremap applies to Command-line mode, for which <C-C>: exits and re-enters Command-line mode to clear the line but, unlike <C-U>, retain it in the command history so that :<Up> can bring it back.
These three mappings cover all six modes. (Apparently ‘Lang-Arg’ isn't a mode.) There are some corner-cases where it doesn't work, but then there are also some cases it works when I'd have thought it wouldn't, and I don't understand why. Also, most of the modes will loose little things like selections and pending operators, even if the mapped command wouldn't otherwise loose these things. For instance, when in Insert mode, I don't see why the example I've given would need to break the current edit into separate changes in the undo/redo history (try typing i123<C-O><Esc>456<Esc>u). To be honest using key mappings to run commands in this way seems like a bit of a hack to me, but I don't know another way.
Vim is great, but like many people I get really annoyed when I want to copy, delete, then paste -- the yank buffer gets overwritten by the delete action.
Now I know there are 101 work-arounds and mappings, some of which are enumerated in posts like this one: Any way to delete in vim without overwriting your last yank?
But all of these solutions have drawbacks -- even I were a buffer-guru (which I'm not). For instance, excess keystrokes -- whereas I normally xxxx to quickly delete 4 characters (just one keystroke cuz I hold it down and wait for autorepeat), it is not practical for me to now switch to ,x,x,x,x or whatever mapping I have to use a different buffer.
What would really be ideal is simply a mode toggle, whereby you can toggle on and off the side-effect behavior of the D, d, X, and x keys so that they alternately do or do not also write their text to a buffer. That way I can simply enter the "no side-effect" mode and delete away to heart's content, then paste when I'm ready. And re-enable side-effects if desired.
Does anyone know a way to do this?
[UPDATE: SOLUTION] OK I got it: I wrote a function that toggles a "no side-effects" mode... works perfectly! See my accepted correct answer below
[UPDATE #2] My solution still works great and I use it all the time when I'm doing a lot of deleting and pasting. But meanwhile I also found a lighter way to meet the specific use-case of copy, paste, delete for simple cases where the text to delete is contiguous.
After yanking text normally, I then visually highlight the text to delete using the v command, and then simply paste over it with the p command. That achieves the desired effect without any special mapping.
Only problem with this workflow is that if I wanted to paste again, the original paste buffer is overwritten by the act of pasting over the highlighted text, but this behavior is easily changed with the following mapping in .vimrc:
vnoremap p "_dp
vnoremap P "_dP
OK, I got it -- this script in .vimrc lets me effectively toggle a "no buffer side-effects" mode whereby the d and x keys no longer overwrite the buffer when "no buffer side-effects" mode is activated.
Add this in .vimrc
function! ToggleSideEffects()
if mapcheck("dd", "n") == ""
noremap dd "_dd
noremap D "_D
noremap d "_d
noremap X "_X
noremap x "_x
echo 'side effects off'
else
unmap dd
unmap D
unmap d
unmap X
unmap x
echo 'side effects on'
endif
endfunction
nnoremap ,, :call ToggleSideEffects()<CR>
Then to toggle in and out of this mode use the key combination ,, (two commas)
I think trying to "turn-off" the side effects for every delete/change command would be overly difficult if not impossible. The basic ways to handle this situation:
Use the black hole ("_) register with your delete or change commands. e.g. "_dd
Use the "0 register which contains the most recent yank with your paste commands. e.g. "0p
Yanking the text to a named register. e.g. "ayy then later doing "ap
I personally lean toward the "0p approach as this is fits with how my mind works.
Now seeing you asked for no such work-arounds I have provided some functions that alter the paste commands to toggle between my so called paste_copy and nopaste_copy mode. nopaste_copy being Vim's default behavior. Put the following in your ~/.vimrc:
function! PasteCopy(cmd, mode)
let reg = ""
if exists('g:paste_copy') && g:paste_copy == 1 && v:register == '"'
let reg = '"0'
elseif v:register != '"'
let reg = '"' . v:register
endif
let mode = ''
if a:mode == 'v'
let mode = 'gv'
endif
exe "norm! " . mode . reg . a:cmd
endfunction
command! -bar -nargs=0 TogglePasteCopy let g:paste_copy = exists('g:paste_copy') && g:paste_copy == 1 ? 0 : 1<bar>echo g:paste_copy ? ' paste_copy' : 'nopaste_copy'
nnoremap <silent> p :call PasteCopy('p', 'n')<cr>
nnoremap <silent> P :call PasteCopy('P', 'n')<cr>
nnoremap <silent> ]p :call PasteCopy(']p', 'n')<cr>
nnoremap <silent> [p :call PasteCopy('[p', 'n')<cr>
nnoremap <silent> ]P :call PasteCopy(']P', 'n')<cr>
nnoremap <silent> [P :call PasteCopy('[P', 'n')<cr>
vnoremap <silent> p :<c-u>call PasteCopy('p', 'v')<cr>
vnoremap <silent> P :<c-u>call PasteCopy('P', 'v')<cr>
You can toggle your paste_copy mode via :TogglePasteCopy. You may prefer a mapping like so
nnoremap <leader>tp :TogglePasteCopy<cr>
As a closing piece of advice I would highly suggest using "0p or using a named register over this approach as they are native to vim and there is one less "mode" to worry about.
if some lines are too long, it will be forced to be newlined.
for example, normally a long line will looks like this
1 first line
2 this is the long second line of the file
3 third line.
but, if the window of a vim are too narrow, it will looks like this
1 first line
2 this is the long
second line of the file
3 third line
the problem arise from this.
let's assume the vim cursor are located at before 't' in 'third line'. if i type 'k', cursor will move to before 's' in 'second line of the file'. after that, if i type 'k' again, cursor will move to 'f' in 'first line'!, not 't' in 'this is the long'. what i want is that the cursor move to 't' in 'this is the long', it is more intuitive process for me. how can set my vim to works like this?
In Vim, the gj and gk commands move by line on the screen rather than by line in the file. This sounds like it probably matches your description.
You can modify your keys like this:
:map j gj
:map k gk
No, if some lines are too long and you have set wrap on they will be shown on "two lines", so to say, but there won't be a newline character between them. If you turn off wrap with set nowrap you'll see the effect.
Normally, k and j move you up and down. If you want to navigate wrapped lines use gk or gj, or just as some like it, map it to for example, the cursor keys.
nmap <up> gk
nmap <down> gj
To move in vim in a natural way is possible.
What I did was, and I suggest you, to modify (or create) your "~/.vimrc" and add these two lines:
map <C-Up> g<Up>
map <C-Down> g<Down>
This will map you control-up and control-down to the movements commands (this is coherent with control-right and control-left to move around long lines)
If you add these other two lines, you can use the same command to move in insertmode:
imap <C-Up> <C-[> g<Up> i
imap <C-Down> <C-[> g<Down> i
(VIM is great !)
Greg Ruo
This answer is derived from #mario-rossi 's answer (Kudo to him), with minor midification.
I use the normal UP and DOWN arrow key, rather than CTRL+up and CTRL+down. And somehow I need to remove one excess space in the INSERT mode mapping to avoid an off-by-one behavior.
Put the following into your ~/.vimrc:
" When a long line is wrapped, the "gk" and "gj" allow you to move up and down
" a visual line, while normal "k" and "j" move a physical line.
" The following settings map "gk" and "gj" to cursor <up> and <down>.
map <up> gk
map <down> gj
" And the following lines enables same <up> and <down> behavior in INSERT mode
imap <up> <C-[> <up>i
imap <down> <C-[> <down>i
Took this from vim.fandom.com:
There are several cases to remap up and down movements. First, you probably should remap both k/j and / arrows. Second, you should coose vim modes that requires remap. Basic modes are Normal (nmap), Insert (as after i command, imap) and Select (as after v command, vmap). To remap all three:
nnoremap j gj
nnoremap k gk
vnoremap j gj
vnoremap k gk
nnoremap <Down> gj
nnoremap <Up> gk
vnoremap <Down> gj
vnoremap <Up> gk
inoremap <Down> <C-o>gj
inoremap <Up> <C-o>gk
There's also one significant Operator mode (as in dj or, to say, y4k), remapping operator (omap) breaks vim experience/habits dramatically and not recommended.
Personally I prefer to remap Insert mode only, to keep my editorial habits intact.
I am using colemak and thus I have remapped my direction keys like so
noremap n h
noremap e j
noremap i k
noremap o l
noremap h n
noremap j e
noremap k i
noremap l o
The problem is that these keybindings are not honored by the viewport navigation. Eg. when I press ^w+n I want to shift focus to the viewport to the left but instead a new empty viewport is opened.
How can I help rebinding all the viewport navigation keys individually?
This subject has been somewhat beaten to death over at the colemak forums:
http://colemak.com/forum/viewtopic.php?id=50 (5 pages, with active contributions by the designer of colemak, who (used to be) an avid vim user too).
There are a number of listed links to
keymaps
plugin scripts
I feel Shai's own script is going way too far - doing way more than just integrate Colemak into vim (remapping C-w to Close Tab e.g., showing some bias towards tab-oriented editors, people have complained about in-/exclusive motions (diw, daB etc) not working any more etc).
I guess the best advice, as has been given before is to stick with the defaults and do:
Keyboard bindings for Vim
Vim is an extremely efficient text editor that I use for writing emails and
editing any sort of text file. Vim's commands are all controlled from the
keyboard: 'd' for delete, 'w' to move the cursor forward one word, 'dw' to
delete the text moved over by 'w', '2dw' to delete two words, etc.
Left/Down/Up/Right navigation is located on the QWERTY keys H/J/K/L so that
the typist's hands never need to reach for arrow keys or the mouse.
Unfortunately, these navigation keys are not so intuitive under Colemak and
so I needed to find some more appropriate mappings. After experimenting with
many alternatives, here is what I finally decided on and am very happy with:
noremap n j|noremap <C-w>n <C-w>j|noremap <C-w><C-n> <C-w>j
noremap e k|noremap <C-w>e <C-w>k|noremap <C-w><C-e> <C-w>k
noremap s h
noremap t l
noremap f e
noremap k n
noremap K N
noremap U <C-r>
I think you'll have to map the <C-W> window navigation commands explicitly, by doing something like:
noremap <C-W>n <C-W>h
noremap <C-W>e <C-W>j
noremap <C-W>i <C-W>k
noremap <C-W>o <C-W>l
noremap <C-W>h <C-W>n
noremap <C-W>j <C-W>e
noremap <C-W>k <C-W>i
noremap <C-W>l <C-W>o
I really like this vim trick to use the left and right arrows to flip between buffers:
"left/right arrows to switch buffers in normal mode
map <right> :bn<cr>
map <left> :bp<cr>
(Put that in ~/.vimrc)
But sometimes I'm munching on a sandwich or something when scrolling around a file and I really want the arrow keys to work normally.
I think what would make most sense is for the arrow keys to have the above buffer-flipping functionality only if there are actually multiple buffers open.
Is there a way to extend the above to accomplish that?
I'd rather have a completely different mapping because:
cursors are really useful, and not having them because you have a hidden buffer will annoy you a lot
some plugins use <left> and <right> because they are less obfuscated than l and h; those plugins are likely to break with such mappings
Anyway, you can try this:
nnoremap <expr> <right> (len(filter(range(0, bufnr('$')), 'buflisted(v:val)')) > 1 ? ":bn\<cr>" : "\<right>")
nnoremap <expr> <left> (len(filter(range(0, bufnr('$')), 'buflisted(v:val)')) > 1 ? ":bp\<cr>" : "\<left>")
To see documentation on the pieces above:
:h :map-<expr>
:h len()
:h filter()
:h range()
:h bufnr()
:h buflisted()
I use alt-direction to switch between buffers.
nmap <A-Left> :bp<CR>
nmap <A-Right> :bn<CR>
If you modifying hl's defaults, then the arrows would feel more useful. (Like changing whichwrap to allow hl to go past the end of line.)
I do something similar with jk to make them different from my arrows:
" work more logically with wrapped lines
set wrap
set linebreak
noremap j gj
noremap k gk
noremap gj j
noremap gk k
That will wrap long lines and jk will move to what looks like the line below. (If you have one long line, then you'll move to the part of that line below the cursor.) Great for editing prose or long comments.
See also
help showbreak
I map Tab and Shift+Tab to switch buffers when in normal mode (makes sense to my brain and the keys are not doing anything useful otherwise).
Add this to your .vimrc
" Use Tab and Shift-Tab to cycle through buffers
nnoremap <Tab> bnext<CR>
nnoremap <S-Tab> :bprevious<CR>