problem to create a shortcut

23 views
Skip to first unread message

Andreas Otto

unread,
May 25, 2024, 4:27:16 AMMay 25
to vim_use
the following  code does NOT work.
The goal is to use a shortcut like "Gmx" to jump to a mark "MARK_X" in the test.
to make it more useable U want to create a function with the last string (in the case above "X" as argument.

question: HOW I write a "goto-mark" function in vim?

" old → works
command! -nargs=0 Gmu    silent g/MARK_U\>/
command! -nargs=0 Gmv    silent g/MARK_V\>/
command! -nargs=0 Gmw    silent g/MARK_W\>/
command! -nargs=0 Gmx    silent g/MARK_X\>/
command! -nargs=0 Gmy    silent g/MARK_Y\>/
command! -nargs=0 Gmz    silent g/MARK_Z\>/

" new → doesn't work
function GotoMark(str)
  "execute("g/MARK_" . a:str . "/")
  "execute("g/MARK_" . "PN" . "/")
  g/MARK_PN/
endfunction

command! -nargs=1 Gm     silent call GotoMark(<f-args>)
"command! -nargs=1 GM     silent execute("g/MARK_" . string(<f-args>) . "/")
"command! -nargs=1 GM     silent g/MARK_PN/

Christian Brabandt

unread,
May 25, 2024, 4:32:13 AMMay 25
to vim...@googlegroups.com
Note: The very first message to this list is always moderated, so
sometimes it takes a bit of time until a moderator notices a new message
in the queue waiting for approval.

On Sa, 25 Mai 2024, Andreas Otto wrote:

> the following  code does NOT work.
> The goal is to use a shortcut like "Gmx" to jump to a mark "MARK_X" in the test.
> to make it more useable U want to create a function with the last string (in the case above "X" as argument.
>
> question: HOW I write a "goto-mark" function in vim?

In its simplest form the following should work:

function GotoMark(str)
exe 'g/MARK_' .. a:str
endfunction


Thanks,
Christian
--
Line Printer paper is strongest at the perforations.

D. Ben Knoble

unread,
May 27, 2024, 2:13:05 AMMay 27
to vim_use
For the record, the following worked for me:

```
function GotoMark(str) abort
  execute printf('/MARK_%s', a:str)
endfunction

command -nargs=* Gm call GotoMark(<f-args>)

" test
new | put =['abc', 'MARK_X', 'def']
Gm X
```

Note that `:/{pattern}` is a shorter way of saying `:global/{pattern}/` if all you care about is the side-effect of moving the cursor; technically, the latter moves to the last occurrence of the pattern while `:/` moves to the next occurrence (with wrap-around controlled by the 'wrapscan' option).
Reply all
Reply to author
Forward
0 new messages