vim: strange behavior with search-replace - vim

Here's my code:
public function setFileAvatar($fileAvatar)
{
$this->fileAvatar=$fileAvatar;
}
I select the whole code with "V" (note the upcase) then I type: ":'<,'>s/ileAvatar/ileNameAvatar" which is supposed to replace ALL "ileAvatar" by "ileNameAvatar"
But here's the result:
public function setFileNameAvatar($fileAvatar)
{
$this->fileNameAvatar=$fileAvatar;
}
The $fileAvatar are not replaced! How comes?

Your substitution is missing the /g flag; only the first occurrence in each line is replaced.
If you make this mistake frequently, you can consider changing the default via :set gdefault.

Related

Is there extension or function to know bracket declaration when focued close bracket

I want to know declaration of open bracket when focused close bracket.(ex. if (...) ).
I know emacs, vscode, vim are has goto declaration function. But, they needs 1 action(type M-.(emacs),F12(vscode),%(vim)). I don't want to type some key each time. So, I want to know declaration of bracket with 0-action.
I don't care how displays in declaration(pop-up, mini buffer, status bar)
Background:
I'm in fixing legacy code. The code is too much nested with ifs and fors and whiles.
By much nested, end of code are many continus close bracket(}) like below.
for (var item in list){
if (cond1) {
...
while( cond2 ) {
...
if (cond3) {
...
} else {
...
}
}
}
list.append(item)
}
}
I usually mistake cond2 and cond3, created bugs, don't show log messages, and spent much time.
This question was translated by google translator. so, if you couldn't recognise this, please comment.
When your cursor is on a bracket, the other one is highlighted automatically if you have :help matchparen enabled.
When your cursor is on a bracket, you can jump to the opening one with :help %.
To quote Mass:
Yes- the plugin match-up has this feature:
https://github.com/andymass/vim-matchup
Using the option
let g:matchup_matchparen_offscreen = { 'method': 'popup' }
There is also the ability to show the match in the statusline (the
default):
let g:matchup_matchparen_offscreen = { 'method': 'status' }`

Vim delete parent parenthesis and reindent child

I'm trying to go from here:
const f = function() {
if (exists) { // delete this
const a = 'apple'
}
}
to:
const f = function() {
const a = 'apple'
}
What's the fastest way to delete and reindent everything in between?
Assuming that cursor is inside the braces; any number of lines and nested operators; "else"-branch is not supported:
[{"_dd<]}']"_dd
Explanation:
[{ go to previous unmatched brace
"_dd delete the "{"-line (now the cursor is in the first line of the block)
<]} decrease identation until the next unmatched "}"
'] go to the last changed line (i.e. "}"-line)
"_dd and delete it
If the cursor is initially set on the "{"-line and you don't care for 1-9 registers, the command can be simplified to dd<]}']dd
Assuming your cursor is somewhere on the line containing const a
?{^M%dd^Odd== (where ^M is you hitting the Enter key and ^O is you hitting Ctrl+O).
Broken down this is:
?{^M - search backwards/upwards for the opening brace
% - jump to the corresponding brace (closing brace)
dd - delete the current line
^O - jump to previous location (the opening brace)
dd - delete the line
== - indent current line
You don't need a special macro or function or anything to do this since vim gives you all the powerful text manipulation tools to do the task. If you find yourself doing this an awful lot then you could always map it to a key combination if you want.
The above only works for single lines inside curly braces, but the one below will work for multiple lines (again assuming you are on some line inside the curly braces)
<i{0f{%dd^Odd I'll leave you to figure out how this one works. Type the command slowly and see what happens.
Great answers all around, and, as pointed out, you can always map these keys to a shortcut. If you'd like to try a slightly more generic solution, you could check my "deleft" plugin: https://github.com/AndrewRadev/deleft.vim

Vim's custom foldmarker {,} doesn't work

I have a js file with following content:
function do_this(){
a = '{1}';
}
function do_that(a){
b = b + 1;
}
// vim: set fdm=marker fmr={,} :
When it folds it shows following:
function do_this(){
a = '{1}';
}
function do_that(a){ +-- 3 lines_____________
// vim: set fdm=marker fmr={,} :
I expect both functions to be folded. I guess "a = '{1}';" is getting in the way.
Is there a way to fix this using only the custom marker "{,}" within the modeline?
Unfortunately foldmarker does not allow regex matching, as specified by :h fmr. Therefore it will only match a literal string, so there's no way getting around the a = '{1}' in your example. However, it seems like what you really want is
// vim: set fdm=syntax fdls=1 :
with
let javaScript_fold=1
in your ~/.vimrc.

GVim/Vim. How to delete all white space up to a certain word

I'm a .Net dev but recently started dabbling with Vim (or in my instance GVim) for when I need to do repetitive text editor type tasks.
My experience is basically non-existent. So please bear with me. Also I realize there are GUI tools or things I can make use of inside Visual Studio, but I'm trying out the Vim route as I'd like to master a new util/app every now and then.
Say I've got a text file which contains a lot of properties (could be any text though) like so:
public string AccountNumber { get; set; }
public string CustomerName { get; set; }
public string ExpiryDate { get; set; }
public string IdentityNumber { get; set; }
public string OfferDate { get; set; }
I'd like to make use of the string replace method to delete everything up to, and after the property name.
e.g. end with:
AccountNumber,
CustomerName, ... etc.
So far I've had success with
1) Alt + left click + drag select all the preceding white space & delete
2) :% s/public\ string\ //
3) :% s/\ {\ get;\ set;\ }/,/
It's purely out of curiosity I'd like to find out if its possible to update my 2nd step to include the removal of the white space.
I realize the ^ character means beginning of the line and that (I think) \s means white space, but that's about where my knowledge ends.
I'm thinking something like?
:% s/^\s+string//
one way to solve this is to record a macro
qq # start recording macro into register q
0w # move to first non-whitespace caracter. Omit this if no WS at start of line
d2w # delete 2 words
w # move a word forward
D # delete to en of line
q # quit macro recording
standing at the beginning of a line, do #q
For subsequent lines, repeat the macro using .
or, try the following substitution
:%s/^\s*public string\s*\([a-zA-Z]*\).*$/\1/
I came up with this,
%s/^\v\s*(\s*\w+){2}\s?(\w+).*/\2/g
precisely speaking, this line doesn't know which word is your "property". it just leave the 3rd word in the line there, remove anything else.
Using :normal would be an alternative to :s or a macro in this case:
:%norm 03dwelD
You may want to use a different range other than the whole file, %. I would suggest visually selecting the lines with V then execute :norm 03dwelD. After you type : your prompt will look like :'<,'>. This is correct.
For more help see:
:h :norm

How to automatically insert braces after starting a code block in vim?

It's really easy to insert a closing brace after typing the opening one:
inoremap { {<CR>}<Esc>ko
This way
if (true) {
converts to
if (true) {
|
}
But I'd like to save time and to type 1 character less:
if (true)<CR>
So I'd like to create the following rule: if return is pressed and the line starts with if/for/while, execute {<CR>}<Esc>ko
Is this doable?
Thanks
Building on your previous mapping, this should do what you want:
inoremap )<CR> ) {<CR>}<Esc>ko
However, you should try a snippet expansion plugin like SnipMate or Ultisnips. Both plugins allow you to define snippets with placeholders and miroring (lots of them are included by default) that are expanded when a <Tab> is pressed after a trigger.
For example, you have a snippet associated with the trigger if that expands into:
if ([condition]) {
}
condition is selected, ready for you to type, and, once you are done, you can hit <Tab> again to jump the cursor between the curly braces, ready to type:
if (myVar == 5) {
|
}
This is incredibly handy.

Resources