Selecting a Javascript arrow function in Vim - vim

It's been a challenge for me to select a whole arrow function via Vim without using relative line numbers. Any thoughts on how I can accomplish that?
Here is an example:
const myFunction = () => {
console.log('hello')
}

As always in Vim, there are lots of ways to do that but the right way is very context-dependent. For example, this problem:
const myFunction = () => {
console.log('hello')
}
is different from this problem:
console.log('---')
const myFunction = () => {
console.log('hello')
}
console.log('---')
or this one:
console.log('---')
const myFunction = () => {
console.log('hello')
}
console.log('---')
and the location of the cursor might be important as well.
Assuming your cursor is somewhere on the first or last line of the snippet, the following command should do what you want:
$V% " move cursor to end of line,
" enter visual-line mode,
" extend the selection to the matching pair
If the cursor is somewhere in the body, you could simply do Vip to visually select the current paragraph but that won't work if there are empty lines in the body or if you have other lines above and below. This is why context matters.
With the cursor somewhere in the body, you could do:
[mV% " jump to previous start of method,
" enter visual-line mode,
" extend the selection to the matching pair
But there are various ways in which it might not work, like when you are in a conditional or a switch.
FWIW, I have used (and updated) the following snippet for many years because the built-in ways have always felt limited to me:
" in after/ftplugin/javascript.vim
function! SelectFunction() abort
call search('\(function\|=>\)', "bWc")
call search("{", "Wc")
normal v%V
endfunction
xnoremap <buffer> af :<C-u>call SelectFunction()<CR>
onoremap <buffer> af :normal vaf<CR>
SelectFunction() searches for a typical JS function declaration at or before the cursor, then it searches for the opening brace at or after the cursor, then it visually selects until the matched pair.
The two mappings essentially define a custom pseudo-text object af that uses SelectFunction() under the hood. It is not a real text object in the sense that it doesn't support count or nesting, but it is nevertheless pretty damn useful:
vaf " visually select current function
daf " cut current function
yaf " yank current function
" etc.
vaf with the cursor somewhere in the body, then daf with the cursor on the closing brace:
Note that the function above probably has shortcomings. From previous interactions with the community, it seems that "select the current function" is not exactly a solved problem.

Related

Move Cursor Immediately Before a Character on a Line in Vim

Say I have the following:
text function(contents) text
and I wanted it to be
text function() text
Placing the cursor right after the opening parenthesis, I thought the following command would work df); however, what I ended up with was the following
text function( text
So I would need someway to specify that I want the character just before the closing parenthesis, but I'm not sure how to do that. There may also be a better way to do this.
What would be the best way to go about this?
You were close! You need dt) as in delete till )
The f motion places the cursor on the ) (remember it like find)
As for the 'best' way to do it, there is at least a more general way: if the
cursor were somewhere in the middle of the ( and ) (or on one of them), you
can do di) (or equivalently di() to delete inside )
If you do da) (or equivalently da() to delete around ), you would
delete the stuff in between and including the brackets.
The same goes for di[, di{, di<, di', di" etc. Using these so-called
text objects, as opposed to the d{motion} way, has the advantage that you can
repeat the edit on other pairs of brackets/quotes without the cursor needing to
be in precisely the same place - it just needs to be on or in between them.
In the following you could position the cursor on e.g. the 'i' of 'initial' in
the first line, do di) to delete the words 'some initial text', then move the
cursor to the 'e' in 'more' in the second line and just do . to also delete
the words 'some more text'):
(some initial text)
(some more text)
This way also works when the brackets (or quotes) are on different lines. For
example, with the cursor somewhere between the {}, doing di} will change
this:
function( args ) {
body of function
}
to this:
function( args ) {
}

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

Best way to create substitution macros in vim

I'd like to set up some custom auto-complete macros in vim. I'm thinking something like this (| represents the cursor position):
it<TAB>|
immediately becomes:
it("|", function () {
});
Is this possible using straight vim, or would I need a plugin? If so, is there a preferred plugin out there?
Using an abbreviation you could write something like this:
inorea it it("", function () {<cr>});<c-o>k<c-o>f"
The purpose of <c-o>k<c-o>f" at the end is to reposition your cursor inside the double quotes, but it may not work all the time.
Using a mapping, you could try this:
ino <expr> <tab> <sid>expand_last_word()
let s:your_expansions = {
\ 'it': '\<c-w>it(\"\", function () {\<cr>});\<c-o>k\<right>',
\ }
fu! s:expand_last_word() abort
let last_word = matchstr(getline('.'), '\v<\k+%'.col('.').'c')
return has_key(s:your_expansions, last_word)
\ ? eval('"'.s:your_expansions[last_word].'"')
\ : "\<tab>"
endfu
You would have to add your abbreviations and their expansions inside the dictionary s:your_expansions.
Using the :read command, you could define larger snippets of code, and split them across several files:
ino <expr> <tab> <sid>expand_last_word()
fu! s:expand_last_word() abort
let last_word = matchstr(getline('.'), '\v<\k+%'.col('.').'c')
if last_word ==# 'it'
return "\<c-w>\<c-o>:r /path/to/some_file\<cr>\<c-o>f\"\<right>"
endif
return "\<tab>"
endfu
Here /path/to/some_file should contain your snippet:
it("", function () {
});
They are very simple solutions, if you want something more robust, you probably need a snippets plugin. One of them is UltiSnips, which requires that your Vim version has been compiled with Python support (:echo has('python') or :echo has('python3') returns 1).
With UltiSnips, you would write your snippet like this:
snippet it "your description" b
it("$0", function () {
});
endsnippet
Here the definition is included between the keywords snippet and endsnippet. On the 1st line, you can describe the purpose of your snippet, inside the string in double quotes. It will be displayed by UltiSnips inside a list, if you've defined several snippets with the same tab trigger, and there's an ambiguity.
The ending b is an option to prevent the tab trigger it from being expanded anywhere else than the beginning of a line.
$0 is a tabstop, it stands for the position in which you want the cursor to be, once the snippet has been fully expanded.
The readme page on github gives a quick start, and some links to videos.
If you want to have a look at the snippets written by other people, you can install vim-snippets.
There are other snippet managers but I don't know them well enough to describe the syntax of their snippets. If you want a comparison, here's one, and here are links for some of them:
snipmate
mu-template
neosnippet
xptemplate
Here is a abbreviation that you can use for your particular example
:inoreabbrev it it("", function () {<cr>});<esc>kf"a
Typing it followed by ctrl + ] in insert mode will render
it("|", function () {
});
and keep you in insert mode.
But I would definitely go for ultisnips and there is a screencast for creating snippets on the same page. That's why I am omitting the snippet here as you can do it yourself.

abstract and reuse parts of a map command in vimrc

I wrote a map command in vim that verges on the line of being too complicated for my taste. I'd like to:
Abstract away parts, or at least spit them into multiple lines and explain each.
Reuse most of it with only 2 things, changing only 2 things in it each time.
The command uses the vim-pandoc plugin to create an Html file out of the current pandoc file and copy it to clipboard.
nnoremap <leader><leader>11 :w<cr>:Pandoc html<cr>:sleep 100m<cr>:e <c-r>=expand('%:r')<cr>.html<cr>ggVG"+y:b#<cr>:bd #<cr>
My first attempt is thus:
nnoremap <expr> <leader><leader>22 MyFunc()
func! MyFunc()
let save = ':w<cr>'
let runPandoc = ':Pandoc html<cr>'
let sleep = ':sleep 100m<cr>'
let viewResult = ':e ' . expand('%:r') . '.html' . '<cr>'
let copyAll = 'ggVG"+y'
let backToPrevBuf = ':b#<cr>'
return save . runPandoc . sleep . viewResult . copyAll . backToPrevBuf
endfunc
However, this does not work because it has <cr> in the returned value instead of actually executing enter.
Using this I can pass an arg to MyFunc that will change the html portions to other filetypes.
My two questions are:
Is there a better, more direct way of abstracting away portions of long maps?
How can I solve the issue with the <cr> appearing in the result instead of actually meaning "Press the enter button".
Thanks for the help!
EDIT:
After getting suggestions I now have the current version, it seems a bit verbose.
While I can wrap the feedkeys to be a shorter name with the 'n' parameter, is there anything else I can do?
func! MyFunc()
call feedkeys(":w\<cr>",'n')
call feedkeys(":Pandoc html\<cr>",'n')
call feedkeys(":sleep 100m\<cr>",'n')
call feedkeys(':e ' . expand('%:r') . '.html' . "\<cr>",'n')
"TODO read about :%y+ understand why it copies the entire file
call feedkeys('ggVG"+y','n')
call feedkeys(":b#\<cr>",'n')
endfunc
EDIT:
Finally, I've understood what it means to get rid of the feedkeys, and now I reached the final destination:
nnoremap <silent> <leader><leader>22 :call MyFunc()<cr><cr>
func! MyFunc()
w
Pandoc html
sleep 100m
execute 'e' (expand('%:r') . '.html')
%y+
b#
endfunc
Thanks for the help!
Is there a better, more direct way of abstracting away portions of
long maps?
Since you have already called a function, and the function just concatenate the rhs mappings, why not just do the actual work in your function, and just map to execute the function like :nnoremap whatever :call yourFunc()<cr>
How can I solve the issue with the <cr> appearing in the result
instead of actually meaning "Press the enter button".
Use "\<cr>"

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