How to repeat a simple nonrmap in vim? - vim

I am trying to write slightly enhanced version of VIM’s mark function, which uses the combination of m and any capital letter to mark a file.
What I want to implement is, instead of using only a single letter, set a mark allowing the combination of two letters, so that if I have files named, test_views, test_models, test_forms, I could use tv, tm, and tf, then don’t have to bother what to map to which file.
What I come up with is so far is:
nnoremap <some-prefix>tv :let tv = expand("%")<cr>
nnoremap <leader>tv :execute 'edit' tv<cr>
but couldn’t figure out to write this more effectively. (I shouldn’t repeat this 26 * 26 times, should I?).
Creating function should be one way, but even if I could manage to finish writing this, I don’t think the usability would meet what I expect.
nnoremap , :call StoreFile(k_map)
fun! StoreFile(k_map)
let k_map = expand("%”)
endfunl
Any suggestion would be appreciated.

You could create all those mappings with a couple of :help :for loop:
let alphabet = split('abcdefghijklmnopqrstuvwxyz', '\zs')
for first_key in alphabet
for second_key in alphabet
let pair = first_key . second_key
execute "nnoremap <Space>" . pair . " :<C-u>let " . pair . " = expand('%')<CR>"
execute "nnoremap <leader>" . pair . " :execute 'edit '" . pair . "<cr>"
endfor
endfor

Related

Is there a way to map key range in VIM?

I would like to map any key to a custom function call. The function will determine what to do based on the specific key pressed, or the last key sequence.
Is there a way to map a key range (e.g., "any key" or "a-z0-9")?
You can't do this with a single mapping. The best you can do is a loop and a bunch of mappings. For example to remap all normal mode lower case characters:
for c in range(char2nr('a'), char2nr('z'))
execute 'nnoremap ' . nr2char(c) . ' :echo " Pressed: ' . nr2char(c) . '"<CR>'
endfor
If you want [a-z0-9] you need to use two range() calls to get the right key codes, since the key codes for the numbers and lowercase letters don't line up (See the ASCII table):
for c in range(char2nr('0'), char2nr('9')) + range(char2nr('a'), char2nr('z'))
execute 'nnoremap ' . nr2char(c) . ' :echo " Pressed: ' . nr2char(c) . '"<CR>'
endfor
If you want to act on characters typed individually in INSERT mode, you can use the InsertCharPre event. For example:
autocmd InsertCharPre * call FilterChar()
function! FilterChar()
if (v:char == 'a')
let v:char = 'ouch'
echo v:char
endif
endfunction
This subverts inserting a by replacing it with ouch, on the fly, while also echoing the same text (which means you can do other stuff there, such as calling a function).

In vim, how to split a word and flip the halves? FooBar => BarFoo

I sometimes write a multi-word identifier in one order, then decide the other order makes more sense. Sometimes there is a separator character, sometimes there is case boundary, and sometimes the separation is positional. For example:
$foobar becomes $barfoo
$FooBar becomes $BarFoo
$foo_bar becomes $bar_foo
How would I accomplish this in vim? I want to put my cursor on the word, hit a key combo that cuts the first half, then appends it to the end of the current word. Something like cw, but also yanking into the cut buffer and then appending to the current word (eg ea).
Nothing general and obvious comes to mind. This is more a novelty question than one of daily practical use, but preference is given to shortest answer with fewest plugins. (Hmm, like code golf for vim.)
You can use this function, it swaps any word of the form FooBar, foo_bar, or fooBar:
function! SwapWord()
" Swap the word under the cursor, ex:
" 'foo_bar' --> 'bar_foo',
" 'FooBar' --> 'BarFoo',
" 'fooBar' --> 'barFoo' (keeps case style)
let save_cursor = getcurpos()
let word = expand("<cword>")
let match_ = match(word, '_')
if match_ != -1
let repl = strpart(word, match_ + 1) . '_' . strpart(word, 0, match_)
else
let matchU = match(word, '\u', 1)
if matchU != -1
let was_lower = (match(word, '^\l') != -1)
if was_lower
let word = substitute(word, '^.', '\U\0', '')
endif
let repl = strpart(word, matchU) . strpart(word, 0, matchU)
if was_lower
let repl = substitute(repl, '^.', '\L\0', '')
endif
else
return
endif
endif
silent exe "normal ciw\<c-r>=repl\<cr>"
call setpos('.', save_cursor)
endf
Mapping example:
noremap <silent> gs :call SwapWord()<cr>
Are you talking about a single instance, globally across a file, or generically?
I would tend to just do a global search and replace, e.g.:
:1,$:s/$foobar/$barfoo/g
(for all lines, change $foobar to $barfoo, every instance on each line)
EDIT (single occurrence with cursor on the 'f'):
3xep
3xep (had some ~ in there before the re-edit of the question)
4xea_[ESC]px
Best I got for now. :)
nnoremap <Leader>s dwbP
Using Leader, s should now work.
dw : cut until the end of the word from cursor position
b : move cursor at the beginning of the word
P : paste the previously cut part at the front
It won't work for you last example though, you have to add another mapping to deal with _ .
(If you don't know what Leader is, see :help mapleader)

How to give nmap a key variable in Vim?

I just got answer to jump to the line start with given character by typing
/ + ^[character]
But I thought it's not as fast as f to jump to character in a line. so i'm want to map it to a key combination like
go + [character]
by doing something like in the .vimrc
nmap go<expr> /^<expre>
See :help map-expression; you can query a single character with getchar():
:nnoremap <expr> go '/^' . nr2char(getchar()) . '<CR>'
it is hard to map this function directly. because the letter/character could be anything.
But this small function may work for you:
function! GoToLine()
call inputsave()
let c= input('Enter chars:')
call inputrestore()
call search ('^' . c)
let #/ = '^'.c
endfunction
you can map for example:
nnoremap <leader>go call GoToLine()
then enter chars you need, the function will bring you there. In this way you could enter more than one chars.
hope it helps.

Mapping/macro to 'smartly' auto-create pairs of apostrophes in vim (and ignore contractions)

I'm currently using closepairs for my auto-closing needs, and it works pretty well. However, there is one caveat -- apostrophes. Don't get me wrong, I need apostrophes closed all the time. I don't want to just disable them. But whenever I type in plain text, whenever there are any contractions (I'm, Don't, Can't)...these apostrophes get made.
Now I could just type to delete them as soon as they can, but doing it every time is a bit impractical.
Does anyone know how I can possibly modify the closepairs script to only autoclose single quotes/apostrophes if they are the start of a word? That is, they are preceded by a whitespace character?
Here is the current code:
inoremap <expr> " <SID>pairquotes('"')
inoremap <expr> ' <SID>pairquotes("'")
function! s:pairquotes(pair)
let l:col = col('.')
let l:line = getline('.')
let l:chr = l:line[l:col-1]
if a:pair == l:chr
return "\<right>"
else
return a:pair.a:pair."\<left>"
endf
I don't know closepairs, but the AutoClose - Inserts matching bracket, paren, brace or quote plugin handles this well. You'll find a list of plugin alternatives on the Vim Tips Wiki.
Are you sure you want to autocomplete only after whitespace? In that case, something like function('string') would not autocomplete after the parenthesis.
Regardless, you can check the previous character against some regex. For example, to avoid autocompletion after letters:
function! s:pairquotes(pair)
let l:line = getline('.')
let l:col = col('.')
let l:chr = l:line[l:col - 1]
let l:prev = l:line[l:col - 2]
if l:chr == a:pair
return "\<right>"
elseif l:prev !~ "[A-Za-z]"
return a:pair . a:pair . "\<left>"
else
return a:pair
endif
endfunction
Note that there are exceptions even with this conservative example, like typing r'regex' in Python, so it might also make sense to define filetype-specific behavior.

What is the easiest way to swap occurrences of two strings in Vim?

What is the easiest way to replace all occurrences of string_a with string_b while at the same time changing anything that was already string_b into string_a? My current method is as follows:
:s/string_a/string_c/g
:s/string_b/string_a/g
:s/string_c/string_b/g
Although this works, it requires extra typing and seems inefficient. Does anybody know of a better way to do this?
I'd do it like this:
:%s/\v(foo|bar)/\={'foo':'bar','bar':'foo'}[submatch(0)]/g
But that's too much typing, so I'd do this:
function! Mirror(dict)
for [key, value] in items(a:dict)
let a:dict[value] = key
endfor
return a:dict
endfunction
function! S(number)
return submatch(a:number)
endfunction
:%s/\v(foo|bar)/\=Mirror({'foo':'bar'})[S(0)]/g
But that still requires typing foo and bar twice, so I'd do something like this:
function! SwapWords(dict, ...)
let words = keys(a:dict) + values(a:dict)
let words = map(words, 'escape(v:val, "|")')
if(a:0 == 1)
let delimiter = a:1
else
let delimiter = '/'
endif
let pattern = '\v(' . join(words, '|') . ')'
exe '%s' . delimiter . pattern . delimiter
\ . '\=' . string(Mirror(a:dict)) . '[S(0)]'
\ . delimiter . 'g'
endfunction
:call SwapWords({'foo':'bar'})
If one of your words contains a /, you have to pass in a delimiter which you know none of your words contains, .e.g
:call SwapWords({'foo/bar':'foo/baz'}, '#')
This also has the benefit of being able to swap multiple pairs of words at once.
:call SwapWords({'foo':'bar', 'baz':'quux'})
You can do this easily with Tim Pope's Abolish plugin
:%S/{transmit,receive}/{receive,transmit}
Here is how I swap two words skip & limit:
%s/skip/xxxxx/g | %s/limit/skip/g | %s/xxxxx/limit/g
Pretty sure someone could turn it into a shorter command which accepts two arguments.
The swapstrings plugin provides a handy command for this:
:SwapStrings string_a string_b
You can do it with a single command as shown in my code below:
:%s/\<\(string_a\|string_b\)\>/\=strpart("string_bstring_a", 8 * ("string_b" == submatch(0)), 8)/g

Resources