I have to format a lot of files in markdown manually, and I often have to wrap some isolated words in backticks to get them in a code span, ie. : object.method -> `object.method`
I'm using vim and I was wondering how I could write and map to some key a command which would put backticks around the word under cursor, by just pressing F1 for instance ?
Thanks !
The canonical answer is to use surround plugin by Time Pope that allows to surround easily a selection.(Unless you don't want to install any plugin)
Another (crude) no-plugin solution for the sake of diversity:
nnoremap <key> ciw`<C-r>"`<Esc>
xnoremap <key> c`<C-r>"`<Esc>
but yeah, just install surround.
lh-brackets has several mappings already defined for markdown:
backtick will insert a pair of backticks in insert mode, or surround the current word or the current selection (what you were looking for, and that surround also provides with its own "syntax")
* -> *<cursor>* ; twice for **<cursor>** (<localleader>* for surrounding)
_ -> _<cursor>_ ; twice for __<cursor>__ (_ for surrounding)
~ -> <del><cursor></del> (<localleader>~ for surrounding)
<BS> -> in INSERT mode, deletes an empty pair when the cursor is within it.
Here is a one liner you can try:
:nmap <F4> :s/\(<c-r>=expand("<cword>")<cr>\)/`\1`/<cr>
After this command pressing F4 key will do what you want, i.e. replace the word under cursor with the same word surrounded by backticks.
[UPDATE]
This may not work for something like object.Method. For this here is a new mapping in visual mode. Select the block of text you want to surround with backtick and press F3
:vnoremap <F3> <Esc>`>a`<Esc>`<i`<Esc
Related
I have the following keyboard shortcut in VIM
nnoremap C vi"
This enable me to select text between "" by clicking C.
Now I want this same shortcut, to do the same but for ''.
nnoremap C vi'
Putting these two rules doesn't work, as the last replaces the one before it.
Is there a way to make both of them work?
Yes, but you need to build the intelligence into the mapping. This can be done via a :help :map-expr:
nnoremap <expr> C 'vi' . (getline('.') =~ '"' ? '"' : "'")
This simplistic example will check whether the current line contains a double quote, and then select those, else single quotes. For a useful mapping, you probably need to ensure surrounding quotes on both sides (using search()), and if both types match select the "closer" one. With a :function, you can make that as complex as you like...
I want to create a command that replaces the word under the cursor by another one, say I have the word have under my cursor and it replaces it with the word had and vice versa. How to accomplish that?
This could easily be accomplished by ciw and then entering the word that you would like to replace it with. Assuming that you want to replace words with different values.
Another solution is you could use a plugin like switch.vim. You would have to define the words/regular expressions you would want to replace.
If you actually want to do this with longer words, then this method may help. First, position the cursor on the word "had" and use yiw to yank (copy) it into the #0 register (and also the unnamed register, but we are about to overwrite that). Then move the cursor to "have" and use ciw<C-R>0<Esc> to replace it with the yanked word.
Do not type <C-R> as five characters: I mean hold down the CTRL key and type r. Similarly, <Esc> means the escape key. Do type each as five characters if you want to make a map out of it, for example
:nmap <F2> ciw<C-R>0<Esc>
If you want to replace all occurrences of the word under the cursor, you can add this into your _vimrc:
" search and replace all occurrences of word under cursor
:nnoremap <C-h> :%s/\<<C-r><C-w>\>/
:inoremap <C-h> <ESC>:%s/\<<C-r><C-w>\>/
Usage of this:
1) Press Ctrl+h (under the cursor is the word "have"), and Vim will enter this in the command line:
:%s/\<have\>/
2) Now just complete the replacing statement:
:%s/\<have\>/had/g
3) And press ENTER...
The SwapIt - Extensible keyword swapper allows you to configure sets of words (e.g. have and had) and toggle them via <C-a> / <C-x> mappings.
I find myself often repeating the following pattern of operations.
I usually go into visual mode, select some lines or block. Then I yank them using y, and paste them using p or P. The next step is to select the pasted text, to replace a variable or function name or change indentation.
I know that I can use gvto reselect the "origin" but what I would like is a similar command to select the "destination".
:help gv mentions :
After using "p" or "P" in Visual mode the text that was put will be selected.
but it is only useful when you are replacing a selection by the content of register, not when you are inserting a whole new block.
You are looking for
`[v`]
'[ and '] are marks automatically set by vim to the start and the end of the "previously changed or yanked text". v switches to visual mode in between.
I prefer the following simple mapping to Benoit's function
nnoremap <expr> g<c-v> '`[' . strpart(getregtype(), 0, 1) . '`]'
Learn more about expression maps:
:h :map-expression
As #ZyX pointed out the strpart is not needed and can be rewritten as:
nnoremap <expr> g<c-v> '`[' . getregtype()[0] . '`]'
One of your use cases is to change indentation after pasting.
I use the following maps to achieve this:
nnoremap <leader>[ `[V`]<
nnoremap <leader>] `[V`]>
They do the following:
de-indent the recently pasted block
indent the recently pasted block
I find these very useful and well used maps.
is there some way to tell VIM place the cursor at some position after abbreviation expansion?
Say you have something like this in .gvimrc
iabberv <? <?=|?>
and you want to place cursor where pipe character is automatically.
A quick solution that I'd use in this case is to insert some key presses to the abbreviation:
iabbrev <? <?=?><Left><Left>
Would place the cursor two places left, after the =.
In this manner, you can use various movement keys such as <End>, <Home>, or even return to normal mode with <Esc> and command usual normal-mode commands. The example would then be
iabbrev <? <?=?><Esc>hha
and it does the same as the first example. If you expand an abbreviation with space, it will have one extra space. which you can get rid of by using a <BS> (backspace) in the abbreviation. Or expand with <C-]> which will leave no space.
Correction: since the abbreviation is first expanded, and after that the space inserted, you need a small function found in the help (map.txt):
func Eatchar(pat)
let c = nr2char(getchar(0))
return (c =~ a:pat) ? '' : c
endfunc
This is best put in .vimrc. Now the following abbreviation will work fully as intented:
:iabbrev <silent> <? <?=?><Left><Left><C-R>=Eatchar('\s')<CR>
It is a bit messy, but has an external function call that removes the white space and it should work well.
What you want are snippets. I use snipmate for that.
It can be done with lh-map-tools:
"
" in {rtp}/ftpluvin/vim/vim_snippets.vim
inoreab <buffer> <silent> if
\ <C-R>=InsertSeq('if', 'if!cursorhere!\nendif!mark!')<CR>
Other plugins offer a similar feature.
I use the following Vim macro a lot (it puts the current line inside XML tags):
I<e>^[A</e>
So I saved it into my .vimrc
let #e='I<e>^[A</e>'
But it does not work.
The ^[ part means "Escape" but it is not understood as such in .vimrc
How can I save this macro, or any macro that contains "Escape" ?
Try entering the escape with <Ctrl-v><Esc> in insert mode.
See :help i_CTRL-V in vim.
For a macro:
:let #e='^[I<e>^[A</e>'
Where ^[ is just one char formed by hitting CTRL+VESC (CTRL+QESC on Windows). Note the escape right at the beginning of the macro.
See :help c_CTRL-V in Vim for more information.
For those who've landed on this post looking for how to add ESC to a mapping, the answer is different.
Literally type <ESC>, thus:
:nnoremap <Leader>E I<e><ESC>A</e><ESC>
See :help key-notation in Vim for more info.
For readability purpose, it's possible to use the proper key-notation tags such as <Esc> or <CR> instead of ^[ or ^M
You would need to escape the tag <Esc> with a single \ and use double quotes instead of single quotes which would result in "\<Esc>".
The following examples are equivalent
:let #e='^[I<e>^[A</e>'
:let #e="\<Esc><e>\<Esc>A</e>"
A list of all key notations can be found by typing :help key-notation or here.
If you're using Windows behavior for vim where Ctrl+V is the Paste command, the equivalent sequence is Ctrl+Q
Today I discovered a vim plug-in called MARVIM (http://www.vim.org/scripts/script.php?script_id=2154).
It is capable of storing macros and executing them later using shortcuts.
Saving macros in plain file without plugins.
Having for example macro m . In insert mode, at e.g beginning of new line, then type 3 keys
ctrl-r ctrl-r m
the macro with escapes will be put in that line. (editable as needed)
In another line name/comment it.
Add other macros if you wish - then save this file as e.g.
:w my_vim_macros
When you want to reuse such saved macro(s):
place cursor on the beginning of macro string, then 4keys
"xy$
and your macro (this line) will be yanked to register x.
I'm answering what I do to add Escape in macro.
Using in vim editor
:let #a='iabcjj'
here I map <ESC> to jj by
.vimrc file
imap jj <esc>