creating a “:KeepCursor” commandHow to undo last command in command line?How can I repeat last Ex-mode command in normal mode?Copy text based on delimiting stringsHow to simulate Return and Escape for global command without using Control-V?Use pattern of global ex command found on a line to substitute in another lineHow to pipe *characters* to cmd ( `:!` )Search pattern for an if statement that isn't followed by curly brace on the next lineSetting the search pattern to just a portion of a regex patternIs there a setting that hides the last colon-command entered?':norm dtxdty' works different than '0dtxdty', what's happening?
Calculating total slots
Limits and Infinite Integration by Parts
Redundant comparison & "if" before assignment
Does malloc reserve more space while allocating memory?
How to cover method return statement in Apex Class?
How should I respond when I lied about my education and the company finds out through background check?
What is the evidence for the "tyranny of the majority problem" in a direct democracy context?
Probability that THHT occurs in a sequence of 10 coin tosses
This is why we puzzle
Did arcade monitors have same pixel aspect ratio as TV sets?
Why does AES have exactly 10 rounds for a 128-bit key, 12 for 192 bits and 14 for a 256-bit key size?
Fear of getting stuck on one programming language / technology that is not used in my country
What are the balance implications behind making invisible things auto-hide?
Terse Method to Swap Lowest for Highest?
Quoting Keynes in a lecture
Angel of Condemnation - Exile creature with second ability
Add big quotation marks inside my colorbox
Pre-mixing cryogenic fuels and using only one fuel tank
Make a Bowl of Alphabet Soup
What features enable the Su-25 Frogfoot to operate with such a wide variety of fuels?
Can I still be respawned if I die by falling off the map?
Calculate sum of polynomial roots
Is this toilet slogan correct usage of the English language?
What if you are holding an Iron Flask with a demon inside and walk into Antimagic Field?
creating a “:KeepCursor” command
How to undo last command in command line?How can I repeat last Ex-mode command in normal mode?Copy text based on delimiting stringsHow to simulate Return and Escape for global command without using Control-V?Use pattern of global ex command found on a line to substitute in another lineHow to pipe *characters* to cmd ( `:!` )Search pattern for an if statement that isn't followed by curly brace on the next lineSetting the search pattern to just a portion of a regex patternIs there a setting that hides the last colon-command entered?':norm dtxdty' works different than '0dtxdty', what's happening?
I'd like to make a command that works like this: :KeepCursor cmd
will the given execute ex command string, then restore the cursor's position. A primary example is:
:KeepCursor normal! *
which would perform a keyword search without jumping to the next match. This can of course be accomplished in other ways, but it's a good demonstration of the functionality I'm looking for, which I want to work in every possible case.
This is my attempt so far. I'm using feedkeys
+imtx
because I'd like the command to behave exactly as though I typed it. I put the cursor restoration in a finally
so it works even if the command encounters an error.
function! s:keepcursor(qargs)
let l:view = winsaveview()
let l:winid = win_getid()
try
call feedkeys(':'.a:qargs."<cr>", 'imtx')
finally
if win_getid() != l:winid
if !win_id2win(l:winid)
return
endif
call win_gotoid(l:winid)
endif
call winrestview(l:view)
endtry
endfunction
command! -nargs=* KeepCursor call s:keepcursor(<q-args>)
However, it does not seem to work. Using :KeepCursor normal! *
prints the search string but does not highlight anything. Running :hlsearch
afterwards highlights the wrong thing.
My questions are a) can it be explained why this doesn't work and b) can such a :KeepCursor
command be written?
search command-line cursor command
add a comment |
I'd like to make a command that works like this: :KeepCursor cmd
will the given execute ex command string, then restore the cursor's position. A primary example is:
:KeepCursor normal! *
which would perform a keyword search without jumping to the next match. This can of course be accomplished in other ways, but it's a good demonstration of the functionality I'm looking for, which I want to work in every possible case.
This is my attempt so far. I'm using feedkeys
+imtx
because I'd like the command to behave exactly as though I typed it. I put the cursor restoration in a finally
so it works even if the command encounters an error.
function! s:keepcursor(qargs)
let l:view = winsaveview()
let l:winid = win_getid()
try
call feedkeys(':'.a:qargs."<cr>", 'imtx')
finally
if win_getid() != l:winid
if !win_id2win(l:winid)
return
endif
call win_gotoid(l:winid)
endif
call winrestview(l:view)
endtry
endfunction
command! -nargs=* KeepCursor call s:keepcursor(<q-args>)
However, it does not seem to work. Using :KeepCursor normal! *
prints the search string but does not highlight anything. Running :hlsearch
afterwards highlights the wrong thing.
My questions are a) can it be explained why this doesn't work and b) can such a :KeepCursor
command be written?
search command-line cursor command
add a comment |
I'd like to make a command that works like this: :KeepCursor cmd
will the given execute ex command string, then restore the cursor's position. A primary example is:
:KeepCursor normal! *
which would perform a keyword search without jumping to the next match. This can of course be accomplished in other ways, but it's a good demonstration of the functionality I'm looking for, which I want to work in every possible case.
This is my attempt so far. I'm using feedkeys
+imtx
because I'd like the command to behave exactly as though I typed it. I put the cursor restoration in a finally
so it works even if the command encounters an error.
function! s:keepcursor(qargs)
let l:view = winsaveview()
let l:winid = win_getid()
try
call feedkeys(':'.a:qargs."<cr>", 'imtx')
finally
if win_getid() != l:winid
if !win_id2win(l:winid)
return
endif
call win_gotoid(l:winid)
endif
call winrestview(l:view)
endtry
endfunction
command! -nargs=* KeepCursor call s:keepcursor(<q-args>)
However, it does not seem to work. Using :KeepCursor normal! *
prints the search string but does not highlight anything. Running :hlsearch
afterwards highlights the wrong thing.
My questions are a) can it be explained why this doesn't work and b) can such a :KeepCursor
command be written?
search command-line cursor command
I'd like to make a command that works like this: :KeepCursor cmd
will the given execute ex command string, then restore the cursor's position. A primary example is:
:KeepCursor normal! *
which would perform a keyword search without jumping to the next match. This can of course be accomplished in other ways, but it's a good demonstration of the functionality I'm looking for, which I want to work in every possible case.
This is my attempt so far. I'm using feedkeys
+imtx
because I'd like the command to behave exactly as though I typed it. I put the cursor restoration in a finally
so it works even if the command encounters an error.
function! s:keepcursor(qargs)
let l:view = winsaveview()
let l:winid = win_getid()
try
call feedkeys(':'.a:qargs."<cr>", 'imtx')
finally
if win_getid() != l:winid
if !win_id2win(l:winid)
return
endif
call win_gotoid(l:winid)
endif
call winrestview(l:view)
endtry
endfunction
command! -nargs=* KeepCursor call s:keepcursor(<q-args>)
However, it does not seem to work. Using :KeepCursor normal! *
prints the search string but does not highlight anything. Running :hlsearch
afterwards highlights the wrong thing.
My questions are a) can it be explained why this doesn't work and b) can such a :KeepCursor
command be written?
search command-line cursor command
search command-line cursor command
asked 3 hours ago
MassMass
6,4051420
6,4051420
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Can it be written?
Yup.
command! -nargs=* -complete=command KeepCursor
let [s:view, s:win] = [winsaveview(), win_getid()] |
try |
execute <q-args> |
finally |
if win_id2win(s:win) |
call win_gotoid(s:win) |
endif |
keepjumps call winrestview(s:view) |
endtry
Can it be explained why this doesn't work
There is a lot going on here. I haven't debugged anything, but my guess is feedkeys()
is part of your problem. I typically avoid feedkeys()
as it is often easier to debug other methods.
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "599"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fvi.stackexchange.com%2fquestions%2f19277%2fcreating-a-keepcursor-command%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Can it be written?
Yup.
command! -nargs=* -complete=command KeepCursor
let [s:view, s:win] = [winsaveview(), win_getid()] |
try |
execute <q-args> |
finally |
if win_id2win(s:win) |
call win_gotoid(s:win) |
endif |
keepjumps call winrestview(s:view) |
endtry
Can it be explained why this doesn't work
There is a lot going on here. I haven't debugged anything, but my guess is feedkeys()
is part of your problem. I typically avoid feedkeys()
as it is often easier to debug other methods.
add a comment |
Can it be written?
Yup.
command! -nargs=* -complete=command KeepCursor
let [s:view, s:win] = [winsaveview(), win_getid()] |
try |
execute <q-args> |
finally |
if win_id2win(s:win) |
call win_gotoid(s:win) |
endif |
keepjumps call winrestview(s:view) |
endtry
Can it be explained why this doesn't work
There is a lot going on here. I haven't debugged anything, but my guess is feedkeys()
is part of your problem. I typically avoid feedkeys()
as it is often easier to debug other methods.
add a comment |
Can it be written?
Yup.
command! -nargs=* -complete=command KeepCursor
let [s:view, s:win] = [winsaveview(), win_getid()] |
try |
execute <q-args> |
finally |
if win_id2win(s:win) |
call win_gotoid(s:win) |
endif |
keepjumps call winrestview(s:view) |
endtry
Can it be explained why this doesn't work
There is a lot going on here. I haven't debugged anything, but my guess is feedkeys()
is part of your problem. I typically avoid feedkeys()
as it is often easier to debug other methods.
Can it be written?
Yup.
command! -nargs=* -complete=command KeepCursor
let [s:view, s:win] = [winsaveview(), win_getid()] |
try |
execute <q-args> |
finally |
if win_id2win(s:win) |
call win_gotoid(s:win) |
endif |
keepjumps call winrestview(s:view) |
endtry
Can it be explained why this doesn't work
There is a lot going on here. I haven't debugged anything, but my guess is feedkeys()
is part of your problem. I typically avoid feedkeys()
as it is often easier to debug other methods.
edited 1 hour ago
answered 3 hours ago
Peter RinckerPeter Rincker
10.4k11828
10.4k11828
add a comment |
add a comment |
Thanks for contributing an answer to Vi and Vim Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fvi.stackexchange.com%2fquestions%2f19277%2fcreating-a-keepcursor-command%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown