I recently fell in love with the f, F, t, and T commands in vim. Now I find myself frequently wanting to insert something at a position that can easily be navigated to with one of these commands, at least frequently enough that I would want to make the entire action "find character and insert text" repeatable via .. Currently, I can repeat the insert action, but I have to retype the find-character movement at every line where I want to repeat the insert.
Is there a command that combines the actions of these movement commands with the action of dropping into insert mode? Or, if not, is it possible to define such a command in my .vimrc?
First, you can repeat the last f/t/F/T motion via ; (reverse via ,), so you can repeat with two keys: ;.
If that's not good enough, the repeat.vim plugin can be used to build a custom mapping that repeats just like the built-in commands:
"<Leader>it{char} Insert text before the [count]'th occurrence of {char}
" to the right.
"<Leader>if{char} Insert text after the [count]'th occurrence of {char}
" to the right.
" These mappings can be repeated atomically, this is
" faster than ";."
function! s:InsertAtCharPrepare( motion, moveOffMotion, repeatMapping )
augroup InsertAtChar
autocmd!
" Enter insert mode automatically after the f/t motion.
" XXX: :startinsert doesn't work on the first movement somehow, use
" feedkeys() instead.
autocmd CursorMoved <buffer> call feedkeys('a', 'n')
" Prime repeat.vim after insertion is done.
execute printf('autocmd InsertLeave <buffer> %scall repeat#set(%s, %d) | autocmd! InsertAtChar',
\ (v:count1 <= 1 || empty(a:moveOffMotion) ? '' : 'execute "normal!" ' . string(a:moveOffMotion) . '|'),
\ string(a:repeatMapping),
\ v:count1
\)
" Abort in case something unexpected happens.
autocmd WinLeave,BufLeave <buffer> autocmd! InsertAtChar
augroup END
return a:motion
endfunction
function! s:InsertAtCharRepeat( moveOffMotion, repeatMapping )
let l:count = v:count1 " Save the original count to pass this on to repeat.vim.
execute 'normal!' l:count . ';.' . (l:count <= 1 ? '' : a:moveOffMotion)
call repeat#set(a:repeatMapping, l:count)
endfunction
" With "t" and [count] > 1, we need to move off from before {char} (where we're
" left when leaving insert mode) onto {char}, so that a repeat will happen
" before the next occurrence, not on the same again.
nnoremap <silent> <Plug>(InsertUntilCharRepeat) :<C-u>call <SID>InsertAtCharRepeat('l', "\<lt>Plug>(InsertUntilCharRepeat)")<CR>
nnoremap <silent> <Plug>(InsertFromCharRepeat) :<C-u>call <SID>InsertAtCharRepeat('', "\<lt>Plug>(InsertFromCharRepeat)")<CR>
nnoremap <expr> <Leader>it <SID>InsertAtCharPrepare('t', 'l', "\<lt>Plug>(InsertUntilCharRepeat)")
nnoremap <expr> <Leader>if <SID>InsertAtCharPrepare('f', '', "\<lt>Plug>(InsertFromCharRepeat)")
Related
Is there a way to make the 'f' and 't' command wrap around the line? For example, if I have
Hello, my name is _intz,
where _ denotes my cursor position, I would like to be able to press fl for vim to place my cursor on the first l on the line.
Similarly, I would ideally like the , and ; commands to also wrap on the current line.
Thank you
No, this is not possible without implementing the feature yourself.
Note that fF are universally expected to mean "next on the line" and tT to mean "previous on the line", both of which being extremely useful in their own right. Instead of changing their meaning, and thus reducing the overall usefulness of Vim, you should consider making new commands.
Something like these quick and dirty mappings:
" move the cursor on first occurrence of character on the line
nnoremap <expr> <key> '0f' . nr2char(getchar())
" move the cursor before first occurrence of character on the line
nnoremap <expr> <key> '0t' . nr2char(getchar())
See :help <expr>, :help nr2char(), :help getchar().
With the help of this answer https://vi.stackexchange.com/questions/29167/determine-if-there-is-a-matching-character-on-the-current-line-past-the-cursor, the following maps <c-f> to allow that gives it the functionality of f with same line wrapping.
function!Neweff()
let character = nr2char(getchar())
let beforejump = getpos('.')
execute 'norm! f'.character.''
let afterjump = getpos('.')
if beforejump == afterjump
let firstcharacter = getline(".")[0]
execute 'norm! 0'
if character !=# firstcharacter
execute 'norm! f'.character.''
endif
endif
endfunction
nnoremap <c-f> :call Neweff()<CR>
I have this function:
function Test()
echom "call " . v:count
endfunction
nnoremap a :call Test()<cr>
If I type, let's say 4a, it will print out
call 4
call 4
call 4
call 4
However, I want it only to be executed once, when I use a count. How can I achieve that?
You need <C-U> before calling the function.
function Test()
echom "call " . v:count
endfunction
nnoremap a :<C-U>call Test()<cr>
<C-U> removes all the characters before the cursor on command line. From help:
c_CTRL-U
CTRL-U Remove all characters between the cursor position and
the beginning of the line. Previous versions of vim
deleted all characters on the line. If that is the
preferred behavior, add the following to your .vimrc:
:cnoremap <C-U> <C-E><C-U>
Is it possible to do this with a Vim command?
[] = Cursor Normal Mode
[ = Cursor Insert Mode
Before
Text []
After
Text
[
Before
Text []
After
[
Text
I've changed the [count] behavior of o / O with the following mapping. I think this does what you want:
" o/O Start insert mode with [count] blank lines.
" The default behavior repeats the insertion [count]
" times, which is not so useful.
function! s:NewLineInsertExpr( isUndoCount, command )
if ! v:count
return a:command
endif
let l:reverse = { 'o': 'O', 'O' : 'o' }
" First insert a temporary '$' marker at the next line (which is necessary
" to keep the indent from the current line), then insert <count> empty lines
" in between. Finally, go back to the previously inserted temporary '$' and
" enter insert mode by substituting this character.
" Note: <C-\><C-n> prevents a move back into insert mode when triggered via
" |i_CTRL-O|.
return (a:isUndoCount && v:count ? "\<C-\>\<C-n>" : '') .
\ a:command . "$\<Esc>m`" .
\ v:count . l:reverse[a:command] . "\<Esc>" .
\ 'g``"_s'
endfunction
nnoremap <silent> <expr> o <SID>NewLineInsertExpr(1, 'o')
nnoremap <silent> <expr> O <SID>NewLineInsertExpr(1, 'O')
do these two mapping help?
nnoremap <leader>O O<ESC>O
nnoremap <leader>o o<cr>
the first by pressing <leader>O will add two empty lines above current line, and bring you to INSERT mode. The 2nd one by pressing <leader>o will add two lines after your current.
I generally like the shiftround option in Vim, but there are a couple of
situations where it doesn't work very well. For instance, take this example:
f(x,
y)
Selecting the two lines and shifting with > and the two lines selected gives
me (and shiftwidth set to 4):
f(x,
y)
When I really wanted:
f(x,
y)
In other words, Vim advanced each line of the block to the next tabstop, when I
really wanted it to insert the same amount of inserted on each line--but I want
the least indented line to end up on the next tabstop.
Is there an easy way to get this behavior in Vim? My goal is to have this work
for < and > (with a visual selection) rather than other workarounds.
Ctrl+V, select vertical column before the text you need to shift (j in your case of just two lines), Shift+I, insert needed number of tabs or spaces, Esc.
You can actually use this method to insert arbitrary text, for example you can insert # or // to comment some code. More info in :help blockwise-operators.
I would solve this via a separate set of mappings that enclose the >> commands with a temporary clearing of 'shiftround'. Here's an implementation:
" g>> Shift [count] lines one 'shiftwidth' rightwards, without
" 'shiftround'.
"{Visual}[count]> Shift the highlighted lines [count] 'shiftwidth'
" rightwards (for {Visual} see |Visual-mode|), without
" 'shiftround'.
" g<<, {Visual}[count]<
function! s:Shift( command )
let s:save_shiftround = &shiftround
set noshiftround
return a:command
endfunction
function! s:RestoreShiftRound()
let &shiftround = s:save_shiftround
return ''
endfunction
nnoremap <expr> <SID>(RestoreShiftRound) <SID>RestoreShiftRound()
nnoremap <expr> <SID>(ShiftRight) <SID>Shift('>>')
xnoremap <expr> <SID>(ShiftRight) <SID>Shift('>')
nnoremap <expr> <SID>(ShiftLeft) <SID>Shift('<<')
xnoremap <expr> <SID>(ShiftLeft) <SID>Shift('<')
nnoremap <silent> <script> <Plug>(ShiftRightNoRound) <SID>(ShiftRight)<SID>(RestoreShiftRound)
xnoremap <silent> <script> <Plug>(ShiftRightNoRoundSelection) <SID>(ShiftRight)<SID>(RestoreShiftRound)
nnoremap <silent> <script> <Plug>(ShiftLeftNoRound) <SID>(ShiftLeft)<SID>(RestoreShiftRound)
xnoremap <silent> <script> <Plug>(ShiftLeftNoRoundSelection) <SID>(ShiftLeft)<SID>(RestoreShiftRound)
nmap g>> <Plug>(ShiftRightNoRound)
xmap g> <Plug>(ShiftRightNoRoundSelection)
nmap g<< <Plug>(ShiftLeftNoRound)
xmap g< <Plug>(ShiftLeftNoRoundSelection)
I didn't cover the >{motion} command, as that would be more complex.
When I'm using vim I generally never want to move to a punctuation mark when I press w or b to go forwards or backwards. So I'm wondering if there's a setting or something to change this functionality?
e.g. If I've got some code like
object.method(args)
and my cursor is at the [o] in "object" then I want w to move to the [m] in "method", and another w to move to the [a] in "args". I don't want it to land on the [.] or the [(]. If I've ever wanted to move to a punctuation char I've always used f or F to jump straight to it. I've never personally wanted to move to a punctuation char when I move through words and I just realized this is really bugging me.
I too find that I would like a movement that is more inclusive that w, but not as inclusive as W. In particular, I would like a movement that only considers tokens beginning with alphanumeric characters as significant.
So I came up with the following:
" <SPACE> : forward to next word beginning with alphanumeric char
" <S-SPACE> : backward to prev word beginning with alphanumeric char
" <C-SPACE> : same as above (as <S-SPACE> not available in console Vim
" <BS> : back to prev word ending with alphanumeric char
function! <SID>GotoPattern(pattern, dir) range
let g:_saved_search_reg = #/
let l:flags = "We"
if a:dir == "b"
let l:flags .= "b"
endif
for i in range(v:count1)
call search(a:pattern, l:flags)
endfor
let #/ = g:_saved_search_reg
endfunction
nnoremap <silent> <SPACE> :<C-U>call <SID>GotoPattern('\(^\\|\<\)[A-Za-z0-9_]', 'f')<CR>
vnoremap <silent> <SPACE> :<C-U>let g:_saved_search_reg=#/<CR>gv/\(^\\|\<\)[A-Za-z0-9_]<CR>:<C-U>let #/=g:_saved_search_reg<CR>gv
nnoremap <silent> <S-SPACE> :<C-U>call <SID>GotoPattern('\(^\\|\<\)[A-Za-z0-9_]', 'b')<CR>
vnoremap <silent> <S-SPACE> :<C-U>let g:_saved_search_reg=#/<CR>gv?\(^\\|\<\)[A-Za-z0-9_]<CR>:<C-U>let #/=g:_saved_search_reg<CR>gv
nnoremap <silent> <BS> :call <SID>GotoPattern('[A-Za-z0-9_]\(\>\\|$\)', 'b')<CR>
vnoremap <silent> <BS> :<C-U>let g:_saved_search_reg=#/<CR>gv?[A-Za-z0-9_]\(\>\\|$\)<CR>:<C-U>let #/=g:_saved_search_reg<CR>gv
" Redundant mapping of <C-SPACE> to <S-SPACE> so that
" above mappings are available in console Vim.
"noremap <C-#> <C-B>
if has("gui_running")
map <silent> <C-Space> <S-SPACE>
else
if has("unix")
map <Nul> <S-SPACE>
else
map <C-#> <S-SPACE>
endif
endif
I have had this for a long time now, and I find that I use <SPACE>/<C-SPACE> movements so much more than w and W; it just seems more useful when coding. You can, of course, map the commands to whatever keys you find useful or more appropriate.
Even running the risk of creating a script for something that's built-in (like
I did last time), here is a little function that may help accomplishing
this.
function! JumpToNextWord()
normal w
while strpart(getline('.'), col('.')-1, 1) !~ '\w'
normal w
endwhile
endfunction
Basically, what it does is executing the standard w and repeating it
if the character under the cursor is not in a word character (feel free to
change that pattern.
If you add that and a little map in your .vimrc:
nnoremap <silent> ,w :call JumpToNextWord()<CR>
It should work.