The way you edit text in vim is pretty much by running commands, you have a few shortcuts here and there, but most of the time is just running commands.
While I really like this approach, I do miss a replace word under cursor shortcut. To do that in "plain" vim, you have to type:
:%s/<C-r><C-w>/type_new_word_here/gc
(Where <C-r> stands for Ctrl+R, and <C-w> for Ctrl+W. That's a bit of a stretch on the mac where you only have a Left Ctrl key).
What the previous command does is to look for the word under cursor in the current file and ask you if you want to replace it with the word you have entered in the second part of the command. The flag gc tells vim that the action is global and that it must to ask you for confirmation before replacing matches.
There is not a whole lotta of typing but we can do better.
The good news is that with a bit of vimscript, you can avoid acrobatic motions and start replacing words with a copule of keystrokes.
This is the code you have to add to your vimrc:
" Replace Function
function ReplaceWordUnderCursor()
call inputsave()
let word = expand("<cword>")
if (word != "")
let newword = input("Replace [".word."] with: ")
call inputrestore()
execute "%s/".word."/".newword."/gc"
else
echo "No word under cursor."
end
endfunction
Type the new word, hit enter and you are ready to go!
While I really like this approach, I do miss a replace word under cursor shortcut. To do that in "plain" vim, you have to type:
:%s/<C-r><C-w>/type_new_word_here/gc
(Where <C-r> stands for Ctrl+R, and <C-w> for Ctrl+W. That's a bit of a stretch on the mac where you only have a Left Ctrl key).
What the previous command does is to look for the word under cursor in the current file and ask you if you want to replace it with the word you have entered in the second part of the command. The flag gc tells vim that the action is global and that it must to ask you for confirmation before replacing matches.
There is not a whole lotta of typing but we can do better.
The good news is that with a bit of vimscript, you can avoid acrobatic motions and start replacing words with a copule of keystrokes.
This is the code you have to add to your vimrc:
" Replace Function
function ReplaceWordUnderCursor()
call inputsave()
let word = expand("<cword>")
if (word != "")
let newword = input("Replace [".word."] with: ")
call inputrestore()
execute "%s/".word."/".newword."/gc"
else
echo "No word under cursor."
end
endfunction
" Maps the replace function to a command
command ReplaceWordUnderCursor call ReplaceWordUnderCursor()
" Maps the shortcut <leader> r to run the command.
nmap <leader>r :ReplaceWordUnderCursor<CR>
So, with that code in place, when you want to replace a word under the cursor you just hit <leader> r and the editor will prompt you for a replacement word, and offer you the usual replacement options (replace, replace all, quit, etc...)
Something like this:
Replace [whatever_word_under_cursor] with:
Something like this:
Replace [whatever_word_under_cursor] with:
Type the new word, hit enter and you are ready to go!
Nice post.
ReplyDeleteUnbelievable Deals at Algodones Mexico Dentist
Keep it up.
ReplyDeleteHigh Visibility Safety Vest
great job
ReplyDeleteVisit Site for Reflective Safety Vest
This post helps me a lot. Thank you. Keep posting.
ReplyDelete308 Modular Weapon System
Very nice work you done .I just found a website where you find and download all kind of software just like getintopc.
ReplyDelete