VIM: Replace [aeiou]' with the respective accented letter - vim

I know that VIM support digraph, and it would be perfect if it's possible to use with :s command, but I can't find a way to use it!
I think something like:
:%s/\([aeiouAEIOU]\)'/\=digraph(submatch(1)."!")/g
Would be perfect, but I didn't find a digraph function.
Thanks in advance.
EDIT
Ok, after a bit of diggin in the built-in VIM's functions, I've found tr and a first solution to the problem:
:%s/\([aeiouAEIOU]\)'/\=tr(submatch(1), 'aeiouAEIOU', 'àèìòùÀÈÌÒÙ')/g
However, I still want to know if there's a way to use digraph in expressions :)

function! Digraph(letter, type)
silent exec "normal! :let l:s = '\<c-k>".a:letter.a:type."'\<cr>"
return l:s
endfunction
This function will allow you to generate any digraph you want.
It simulates typing <c-k><char><char> by running it with the normal command and assigning it to the local variable s. And then it returns s.
After this function is defined and you can use it like this.
:%s/\([aeiouAEIOU]\)'/\=Digraph(submatch(1), "!")/g
Note: This was based off of the source code for EasyDigraph

Here's another approach using a hand-coded vim function (add to your vimrc):
" get a matching digraph for a given ASCII character
function! GetDigraph(var1)
"incomplete dictionary of digraphs, add your own....
let DigDict = {'a': 'à', 'e': 'è', 'i': 'ì'}
"get the matching digraph. If no match, just return the given character
let DictEntry = get(DigDict, a:var1, a:var1)
return DictEntry
endfunction
Call it like this :%s/\([aeiouAEIOU]\)'/\=GetDigraph(submatch(1))/g

Related

Case insensitive f key in vim?

Does anyone know of any way to get the 'f' key in vim normal command mode to operate case insensitive
Example
function! OpenCurrentFileInMarked()
In the above line, if I am at the start of the line I want to be able to type 'fi'and get the first 'i' and then ';' to toggle to the capital 'I' in the middle of the function name. I would prefer that the 'f' key is always bound to case insensitivity. Would work much better for me as a default.
The easy answer here is: use a plugin. Others have had the idea before.
Fanf,ingTastic;: Find a char across lines
The main purpose of this plugin is to make f and t cross the line boundary.
To make them ignore case you need to let g:fanfingtastic_ignorecase = 1 in your vimrc.
ft_improved: improved f/t command
Same here.
To make it ignore case you again need to set a variable in your vimrc, this time let g:ft_improved_ignorecase = 1.
The first part of this (case insensitive f) is actually in the reference manual as an example of how to use the getchar() function:
This example redefines "f" to ignore case:
:nmap f :call FindChar()<CR>
:function FindChar()
: let c = nr2char(getchar())
: while col('.') < col('$') - 1
: normal l
: if getline('.')[col('.') - 1] ==? c
: break
: endif
: endwhile
:endfunction
See :help getchar().
You'll need to save the character returned and write a similar map for ; if you want that to work too.
You can't do this with regular vim commands. However, you can write your own function and bind the f key to it.

How to repeatedly add text on both sides of a word in vim?

I have a bunch of local variable references in a Python script that I want to pull from a dictionary instead. So, I need to essentially change foo, bar, and others into env['foo'], env['bar'] and so on. Do I need to write a regular expression and match each variable name to transform, or is there a more direct approach that I could just repeat with the . command?
You can use a macro: type these commands in one go (with spacing just to insert comments)
" first move to start of the relevant word (ie via search)
qa " record macro into the a register.
ienv['<esc> " insert relevant piece
ea'] " move to end of word and insert relevant piece
q " stop recording
then, when you're on the next word, just hit #a to replay the macro (or even ## to repeat the last replay after that).
There's an easier way - you can use a regex search and replace. Go into cmdline mode by typing a colon and then run this command:
%s/\\(foo\|bar\|baz\\)/env['\1']/
Replacing foo, bar, and baz with whatever your actual variable names are. You can add as many additional variables as you'd like, just be sure to escape your OR pipes with a backslash. Hope that helps.
you could write a function that would do this pretty well, add this to your .vimrc file:
function! s:surround()
let word = expand("<cword>")
let command = "%s/".word."/env[\'".word."\']/g"
execute command
endfunction
map cx :call <SID>surround()<CR>
This will surround every occurance of the word currently under the cursor.
If you wanted to specify what went before and after each instance you could use this:
function! s:surround()
let word = expand("<cword>")
let before = input("what should go before? ")
let after = input("what should go after? ")
let command = "%s/".word."/".before.word.after."/g"
execute command
endfunction
map cx :call <SID>surround()<CR>
If you only want to confirm each instance of the variable you could use this:
function! s:surround()
let word = expand("<cword>")
let before = input("what should go before? ")
let after = input("what should go after? ")
let command = "%s/".word."/".before.word.after."/c"
execute command
endfunction
map cx :call <SID>surround()<CR>
I figured out one way to do what I need. Use q{0-9a-zA-Z"} to record key strokes into a buffer. Position the cursor at the begging of the variable name, then cw and type env['']. Next move the cursor back one space to the last quote and paste the buffer filled from the cw command with P. Finally, reuse the recording with #{0-9a-z".=*} for each variable.

vim codewalk through

Following code has been picked up from this blog
function! Privatize()
let priorMethod = PriorMethodDefinition()
exec "normal iprivate :" . priorMethod . "\<Esc>=="
endfunction
function! PriorMethodDefinition()
let lineNumber = search('def', 'bn')
let line = getline(lineNumber)
if line == 0
echo "No prior method definition found"
endif
return matchlist(line, 'def \(\w\+\).*')[1]
endfunction
map <Leader>p :call Privatize()<CR>
I tried but I fail to understand PriorMethodDefinition method. Can someone walk me through this code?
PriorMethodDefinition returns the name of the first method definition above the cursor.
It does this by searching backwards for a line containing the text 'def'. The search function returns the line number and getline is used to retrieve the content of that line.
The function checks that it has found a valid line, before using a regular expression to get the name of the method and return it.
You can read more about these functions if you're curious about the specifics - see:
:help search
:help getline
:help matchlist
Edit: you can also read about the regular expression pattern
:help pattern
But I found it a little confusing at first, so allow me to explain it a little. Here's the expression used:
'def \(\w\+\).*'
This will search for any text matching the following pattern: "the text def followed by one or more 'word' characters \w\+ followed by zero or more characters .*". The part matching the word characters is placed into a group (or atom), designated by the escaped parens \( & \). More info on the definitions of word characters etc can be found in the help link above.
The matchlist function returns a list of matches, the first [0] of which is the full text matching the regex, followed by submatches (ie our group). We are interested in the first such submatch, hence the [1].

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

How do you select the entire PHP function definition?

In PHP, if I have a function such as:
function test($b) {
var $a = 0;
while ($a < b) {
$a += 3;
}
return $a;
}
and the cursor is on the $a += 3 line, is it possible to quickly select the entire function?
"v2aB" would select everything including the function braces but not the declaration function test($b)
Press V after the selection command you post, to convert the selection to line selection, and it will select the function declaration:
v2aBV
It's been a long time since this question was asked and answered, but I will add my own answer because it's the one I was looking for and none of the others work exactly like this one:
nnoremap vaf ?func.*\n*\s*{<cr>ma/{<cr>%mb`av`b
vmap af o<esc>kvaf
The first mapping, "Visual around function" or vaf, will jump back to the start of the function definition, regardless that the { is in the same line or the next one, and even if it's a lambda function, and visually select it characterwise to it's ending bracket. This works in PHP, Javascript and Go.
The user can then press V to turn to linewise select mode if she wants to.
The only problem that I found is that when I am in the body of a big function, but below a line that uses a lambda (let's say "small") function, this will stop searching at the beginning of the small function and select it's body instead of reaching the start of the big function and select all of its body.
function show_video_server(v_server) {
// this whole function should get selected
var something = function(){ /* this function gets selected */ };
// | the cursor is here when I type "vaf"
}
As a workaround I use the second mapping: vmap af o<esc>kvaf. It feels like a repetition or expansion of the selection. What it really does is abandon the selection and go to the line before it, and then try it agan. If the "big" function uses several lambda functions the user has to repeat the af several times to reach the big one.
Usually, vaf es enough. Sometimes vaf af or vaf af af is needed. Anyway, it's the closest I could get to what I wanted, so this is the version I'm using.
Here's a mapping that seems to work very well, no matter the nesting level.
:map t ? function <CR>f{vaBV
Here's another method that will work if you have function-level folding turned on: z c v
That closes the current fold and selects it, but it leaves it closed. If you want it to remain open: z c v $
If you have block-level folding turned on, you would have to close twice, since you're inside the while loop, so: 2 z c v
To enable PHP class/function folding: let php_folding = 1
simple way
nmap vaf va}V
I like this
nmap vaf [{?function<CR>:nohl<CR>vf{]}
if ‘{’ is in new line
nmap vaF [{?function<CR>:nohl<CR>v/{<CR>]}
Yet another way. This should select the entire function definition regardless of your cursor position within the definition, not just when you're at the $a += 3 line.
Use this in normal mode (<CR> means press enter)
?func<CR>V/{%
Explanation of each part:
?func search backward for the word "func" (the idea is to get to the first line of the function definition)
V go to visual line mode
/{ search forward for the opening brace (I didn't use f{ because the opening brace might be on a separate line)
% go to the matching brace
If you are using OOP programming this works (it looks for extra words before function[public, private, protected])
nmap vaf [{?\S* function<CR>:nohl<CR>v/{<CR>]}
As a bonus here is a wrapper around if
nmap vai [{?if<CR>:nohl<CR>v/{<CR>]}

Resources