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?

What is the evidence for the "tyranny of the majority problem" in a direct democracy context?

It grows, but water kills it

How much character growth crosses the line into breaking the character

Plot of a tornado-shaped surface

What are some good ways to treat frozen vegetables such that they behave like fresh vegetables when stir frying them?

Quoting Keynes in a lecture

Is there an injective, monotonically increasing, strictly concave function from the reals, to the reals?

Unexpected behavior of the procedure `Area` on the object 'Polygon'

Why is the "ls" command showing permissions of files in a FAT32 partition?

Why can Carol Danvers change her suit colours in the first place?

Is there a RAID 0 Equivalent for RAM?

Does IPv6 have similar concept of network mask?

User Story breakdown - Technical Task + User Feature

Novel, Lo Tech SF/Fantasy

Non-trope happy ending?

Angel of Condemnation - Exile creature with second ability

How to say when an application is taking the half of your screen on a computer

The IT department bottlenecks progress, how should I handle this?

Do the primes contain an infinite almost arithmetic progression?

Does an advisor owe his/her student anything? Will an advisor keep a PhD student only out of pity?

Why is it that I can sometimes guess the next note?

Lowest total scrabble score

What is the highest possible scrabble score for placing a single tile

putting logo on same line but after title, latex



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?













2















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?










share|improve this question


























    2















    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?










    share|improve this question
























      2












      2








      2








      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?










      share|improve this question














      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 5 hours ago









      MassMass

      6,4101420




      6,4101420




















          1 Answer
          1






          active

          oldest

          votes


















          4















          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.






          share|improve this answer
























            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
            );



            );













            draft saved

            draft discarded


















            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









            4















            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.






            share|improve this answer





























              4















              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.






              share|improve this answer



























                4












                4








                4








                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.






                share|improve this answer
















                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.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited 2 hours ago

























                answered 4 hours ago









                Peter RinckerPeter Rincker

                10.4k11828




                10.4k11828



























                    draft saved

                    draft discarded
















































                    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.




                    draft saved


                    draft discarded














                    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





















































                    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







                    Popular posts from this blog

                    Reverse int within the 32-bit signed integer range: [−2^31, 2^31 − 1]Combining two 32-bit integers into one 64-bit integerDetermine if an int is within rangeLossy packing 32 bit integer to 16 bitComputing the square root of a 64-bit integerKeeping integer addition within boundsSafe multiplication of two 64-bit signed integersLeetcode 10: Regular Expression MatchingSigned integer-to-ascii x86_64 assembler macroReverse the digits of an Integer“Add two numbers given in reverse order from a linked list”

                    Category:Fedor von Bock Media in category "Fedor von Bock"Navigation menuUpload mediaISNI: 0000 0000 5511 3417VIAF ID: 24712551GND ID: 119294796Library of Congress authority ID: n96068363BnF ID: 12534305fSUDOC authorities ID: 034604189Open Library ID: OL338253ANKCR AUT ID: jn19990000869National Library of Israel ID: 000514068National Thesaurus for Author Names ID: 341574317ReasonatorScholiaStatistics

                    Kiel Indholdsfortegnelse Historie | Transport og færgeforbindelser | Sejlsport og anden sport | Kultur | Kendte personer fra Kiel | Noter | Litteratur | Eksterne henvisninger | Navigationsmenuwww.kiel.de54°19′31″N 10°8′26″Ø / 54.32528°N 10.14056°Ø / 54.32528; 10.14056Oberbürgermeister Dr. Ulf Kämpferwww.statistik-nord.deDen danske Stats StatistikKiels hjemmesiderrrWorldCat312794080n790547494030481-4