How do I use 'cgn' for word under cursor?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












3















I really like and often use the cgn command for replacing something I was currently searching for, but it sort of lacks a (imho) rather intuitive feature: replace the word (or whatever) under the cursor. My current approach is to search for the word in question, but that feels like a hack, plus the cursor immediately moves onwards to the next hit.



Is there any way to use cgn on the current word?










share|improve this question


























    3















    I really like and often use the cgn command for replacing something I was currently searching for, but it sort of lacks a (imho) rather intuitive feature: replace the word (or whatever) under the cursor. My current approach is to search for the word in question, but that feels like a hack, plus the cursor immediately moves onwards to the next hit.



    Is there any way to use cgn on the current word?










    share|improve this question
























      3












      3








      3








      I really like and often use the cgn command for replacing something I was currently searching for, but it sort of lacks a (imho) rather intuitive feature: replace the word (or whatever) under the cursor. My current approach is to search for the word in question, but that feels like a hack, plus the cursor immediately moves onwards to the next hit.



      Is there any way to use cgn on the current word?










      share|improve this question














      I really like and often use the cgn command for replacing something I was currently searching for, but it sort of lacks a (imho) rather intuitive feature: replace the word (or whatever) under the cursor. My current approach is to search for the word in question, but that feels like a hack, plus the cursor immediately moves onwards to the next hit.



      Is there any way to use cgn on the current word?







      search replace






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Feb 13 at 8:36









      gustafbstromgustafbstrom

      1184




      1184




















          2 Answers
          2






          active

          oldest

          votes


















          3














          There is no built-in way to do it (as an option), as far as I know.



          I have the following mapping:



          " change current word (like ciw) but repeatable with dot . for the same next
          " word
          nnoremap <silent> c<Tab> :let @/=expand('<cword>')<cr>cgn


          Basically what it does is:



          1. make current search term == word under cursor (without actual search and jump)

          2. call cgn -- and it will use current search from 1.)

          For reference:



          • :h @/

          • :h <cword>





          share|improve this answer

























          • Worked like a charm. Thanks!

            – gustafbstrom
            Feb 13 at 9:48











          • Instead of :let @/=expand('<cword>')<cr> can't you simply use * or # in normal mode to search for the word under the cursor, and then use cgn? :h star

            – statox
            Feb 13 at 10:17











          • @statox I can, but the cursor will jump to the next occurrence of the word

            – Maxim Kim
            Feb 13 at 10:43











          • Oh right I use a mapping to avoid the cursor to jump, I forgot about that.

            – statox
            Feb 13 at 12:33


















          3














          Make * stay at the cursor position



          nnoremap * m`:keepjumps normal! *``<cr>


          https://stackoverflow.com/questions/4256697/vim-search-and-highlight-but-do-not-jump



          Plugins for *: vim-asterisk, vim-slash, ...



          There are quite a few (lightweight) plugins which try to enhance searching in vim with the star command.



          Typically, people miss that the star command does not consider a visual selection and secondly, what you want, to not move the cursor when pressing *.



          After installation of one of these, you would press *cgn to achieve what you want.



          In case of vim-asterisk, you have to override the builtin * yourself in your vimrc. vim-asterisk suggests:



          map * <Plug>(asterisk-*)
          map # <Plug>(asterisk-#)
          map g* <Plug>(asterisk-g*)
          map g# <Plug>(asterisk-g#)
          map z* <Plug>(asterisk-z*)
          map gz* <Plug>(asterisk-gz*)
          map z# <Plug>(asterisk-z#)
          map gz# <Plug>(asterisk-gz#)


          where z* means stay. However, you can make * to stay with



          map * <Plug>(asterisk-z*)
          map # <Plug>(asterisk-z#)
          map g* <Plug>(asterisk-gz*)
          map g# <Plug>(asterisk-gz#)


          vim-slash does this for you. This might be a reason to prefer vim-asterisk because you have control of what gets remapped.



          There are more of them. Just to give you an idea a mention a few:



          • vim-evanesco

          • SearchHighlighting

          • vim-visualstar

          Plugin with new operator: sad.vim



          If you like an explicit mapping for this operation, consider https://github.com/hauleth/sad.vim. This remaps s (changes builtin and clashes with a few other plugins, e.g. vim-sneak and vim-sandwich). siw and then press . as often you want to repeat the change for the next search matches.






          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%2f18894%2fhow-do-i-use-cgn-for-word-under-cursor%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            3














            There is no built-in way to do it (as an option), as far as I know.



            I have the following mapping:



            " change current word (like ciw) but repeatable with dot . for the same next
            " word
            nnoremap <silent> c<Tab> :let @/=expand('<cword>')<cr>cgn


            Basically what it does is:



            1. make current search term == word under cursor (without actual search and jump)

            2. call cgn -- and it will use current search from 1.)

            For reference:



            • :h @/

            • :h <cword>





            share|improve this answer

























            • Worked like a charm. Thanks!

              – gustafbstrom
              Feb 13 at 9:48











            • Instead of :let @/=expand('<cword>')<cr> can't you simply use * or # in normal mode to search for the word under the cursor, and then use cgn? :h star

              – statox
              Feb 13 at 10:17











            • @statox I can, but the cursor will jump to the next occurrence of the word

              – Maxim Kim
              Feb 13 at 10:43











            • Oh right I use a mapping to avoid the cursor to jump, I forgot about that.

              – statox
              Feb 13 at 12:33















            3














            There is no built-in way to do it (as an option), as far as I know.



            I have the following mapping:



            " change current word (like ciw) but repeatable with dot . for the same next
            " word
            nnoremap <silent> c<Tab> :let @/=expand('<cword>')<cr>cgn


            Basically what it does is:



            1. make current search term == word under cursor (without actual search and jump)

            2. call cgn -- and it will use current search from 1.)

            For reference:



            • :h @/

            • :h <cword>





            share|improve this answer

























            • Worked like a charm. Thanks!

              – gustafbstrom
              Feb 13 at 9:48











            • Instead of :let @/=expand('<cword>')<cr> can't you simply use * or # in normal mode to search for the word under the cursor, and then use cgn? :h star

              – statox
              Feb 13 at 10:17











            • @statox I can, but the cursor will jump to the next occurrence of the word

              – Maxim Kim
              Feb 13 at 10:43











            • Oh right I use a mapping to avoid the cursor to jump, I forgot about that.

              – statox
              Feb 13 at 12:33













            3












            3








            3







            There is no built-in way to do it (as an option), as far as I know.



            I have the following mapping:



            " change current word (like ciw) but repeatable with dot . for the same next
            " word
            nnoremap <silent> c<Tab> :let @/=expand('<cword>')<cr>cgn


            Basically what it does is:



            1. make current search term == word under cursor (without actual search and jump)

            2. call cgn -- and it will use current search from 1.)

            For reference:



            • :h @/

            • :h <cword>





            share|improve this answer















            There is no built-in way to do it (as an option), as far as I know.



            I have the following mapping:



            " change current word (like ciw) but repeatable with dot . for the same next
            " word
            nnoremap <silent> c<Tab> :let @/=expand('<cword>')<cr>cgn


            Basically what it does is:



            1. make current search term == word under cursor (without actual search and jump)

            2. call cgn -- and it will use current search from 1.)

            For reference:



            • :h @/

            • :h <cword>






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Feb 13 at 9:17

























            answered Feb 13 at 9:11









            Maxim KimMaxim Kim

            63638




            63638












            • Worked like a charm. Thanks!

              – gustafbstrom
              Feb 13 at 9:48











            • Instead of :let @/=expand('<cword>')<cr> can't you simply use * or # in normal mode to search for the word under the cursor, and then use cgn? :h star

              – statox
              Feb 13 at 10:17











            • @statox I can, but the cursor will jump to the next occurrence of the word

              – Maxim Kim
              Feb 13 at 10:43











            • Oh right I use a mapping to avoid the cursor to jump, I forgot about that.

              – statox
              Feb 13 at 12:33

















            • Worked like a charm. Thanks!

              – gustafbstrom
              Feb 13 at 9:48











            • Instead of :let @/=expand('<cword>')<cr> can't you simply use * or # in normal mode to search for the word under the cursor, and then use cgn? :h star

              – statox
              Feb 13 at 10:17











            • @statox I can, but the cursor will jump to the next occurrence of the word

              – Maxim Kim
              Feb 13 at 10:43











            • Oh right I use a mapping to avoid the cursor to jump, I forgot about that.

              – statox
              Feb 13 at 12:33
















            Worked like a charm. Thanks!

            – gustafbstrom
            Feb 13 at 9:48





            Worked like a charm. Thanks!

            – gustafbstrom
            Feb 13 at 9:48













            Instead of :let @/=expand('<cword>')<cr> can't you simply use * or # in normal mode to search for the word under the cursor, and then use cgn? :h star

            – statox
            Feb 13 at 10:17





            Instead of :let @/=expand('<cword>')<cr> can't you simply use * or # in normal mode to search for the word under the cursor, and then use cgn? :h star

            – statox
            Feb 13 at 10:17













            @statox I can, but the cursor will jump to the next occurrence of the word

            – Maxim Kim
            Feb 13 at 10:43





            @statox I can, but the cursor will jump to the next occurrence of the word

            – Maxim Kim
            Feb 13 at 10:43













            Oh right I use a mapping to avoid the cursor to jump, I forgot about that.

            – statox
            Feb 13 at 12:33





            Oh right I use a mapping to avoid the cursor to jump, I forgot about that.

            – statox
            Feb 13 at 12:33











            3














            Make * stay at the cursor position



            nnoremap * m`:keepjumps normal! *``<cr>


            https://stackoverflow.com/questions/4256697/vim-search-and-highlight-but-do-not-jump



            Plugins for *: vim-asterisk, vim-slash, ...



            There are quite a few (lightweight) plugins which try to enhance searching in vim with the star command.



            Typically, people miss that the star command does not consider a visual selection and secondly, what you want, to not move the cursor when pressing *.



            After installation of one of these, you would press *cgn to achieve what you want.



            In case of vim-asterisk, you have to override the builtin * yourself in your vimrc. vim-asterisk suggests:



            map * <Plug>(asterisk-*)
            map # <Plug>(asterisk-#)
            map g* <Plug>(asterisk-g*)
            map g# <Plug>(asterisk-g#)
            map z* <Plug>(asterisk-z*)
            map gz* <Plug>(asterisk-gz*)
            map z# <Plug>(asterisk-z#)
            map gz# <Plug>(asterisk-gz#)


            where z* means stay. However, you can make * to stay with



            map * <Plug>(asterisk-z*)
            map # <Plug>(asterisk-z#)
            map g* <Plug>(asterisk-gz*)
            map g# <Plug>(asterisk-gz#)


            vim-slash does this for you. This might be a reason to prefer vim-asterisk because you have control of what gets remapped.



            There are more of them. Just to give you an idea a mention a few:



            • vim-evanesco

            • SearchHighlighting

            • vim-visualstar

            Plugin with new operator: sad.vim



            If you like an explicit mapping for this operation, consider https://github.com/hauleth/sad.vim. This remaps s (changes builtin and clashes with a few other plugins, e.g. vim-sneak and vim-sandwich). siw and then press . as often you want to repeat the change for the next search matches.






            share|improve this answer





























              3














              Make * stay at the cursor position



              nnoremap * m`:keepjumps normal! *``<cr>


              https://stackoverflow.com/questions/4256697/vim-search-and-highlight-but-do-not-jump



              Plugins for *: vim-asterisk, vim-slash, ...



              There are quite a few (lightweight) plugins which try to enhance searching in vim with the star command.



              Typically, people miss that the star command does not consider a visual selection and secondly, what you want, to not move the cursor when pressing *.



              After installation of one of these, you would press *cgn to achieve what you want.



              In case of vim-asterisk, you have to override the builtin * yourself in your vimrc. vim-asterisk suggests:



              map * <Plug>(asterisk-*)
              map # <Plug>(asterisk-#)
              map g* <Plug>(asterisk-g*)
              map g# <Plug>(asterisk-g#)
              map z* <Plug>(asterisk-z*)
              map gz* <Plug>(asterisk-gz*)
              map z# <Plug>(asterisk-z#)
              map gz# <Plug>(asterisk-gz#)


              where z* means stay. However, you can make * to stay with



              map * <Plug>(asterisk-z*)
              map # <Plug>(asterisk-z#)
              map g* <Plug>(asterisk-gz*)
              map g# <Plug>(asterisk-gz#)


              vim-slash does this for you. This might be a reason to prefer vim-asterisk because you have control of what gets remapped.



              There are more of them. Just to give you an idea a mention a few:



              • vim-evanesco

              • SearchHighlighting

              • vim-visualstar

              Plugin with new operator: sad.vim



              If you like an explicit mapping for this operation, consider https://github.com/hauleth/sad.vim. This remaps s (changes builtin and clashes with a few other plugins, e.g. vim-sneak and vim-sandwich). siw and then press . as often you want to repeat the change for the next search matches.






              share|improve this answer



























                3












                3








                3







                Make * stay at the cursor position



                nnoremap * m`:keepjumps normal! *``<cr>


                https://stackoverflow.com/questions/4256697/vim-search-and-highlight-but-do-not-jump



                Plugins for *: vim-asterisk, vim-slash, ...



                There are quite a few (lightweight) plugins which try to enhance searching in vim with the star command.



                Typically, people miss that the star command does not consider a visual selection and secondly, what you want, to not move the cursor when pressing *.



                After installation of one of these, you would press *cgn to achieve what you want.



                In case of vim-asterisk, you have to override the builtin * yourself in your vimrc. vim-asterisk suggests:



                map * <Plug>(asterisk-*)
                map # <Plug>(asterisk-#)
                map g* <Plug>(asterisk-g*)
                map g# <Plug>(asterisk-g#)
                map z* <Plug>(asterisk-z*)
                map gz* <Plug>(asterisk-gz*)
                map z# <Plug>(asterisk-z#)
                map gz# <Plug>(asterisk-gz#)


                where z* means stay. However, you can make * to stay with



                map * <Plug>(asterisk-z*)
                map # <Plug>(asterisk-z#)
                map g* <Plug>(asterisk-gz*)
                map g# <Plug>(asterisk-gz#)


                vim-slash does this for you. This might be a reason to prefer vim-asterisk because you have control of what gets remapped.



                There are more of them. Just to give you an idea a mention a few:



                • vim-evanesco

                • SearchHighlighting

                • vim-visualstar

                Plugin with new operator: sad.vim



                If you like an explicit mapping for this operation, consider https://github.com/hauleth/sad.vim. This remaps s (changes builtin and clashes with a few other plugins, e.g. vim-sneak and vim-sandwich). siw and then press . as often you want to repeat the change for the next search matches.






                share|improve this answer















                Make * stay at the cursor position



                nnoremap * m`:keepjumps normal! *``<cr>


                https://stackoverflow.com/questions/4256697/vim-search-and-highlight-but-do-not-jump



                Plugins for *: vim-asterisk, vim-slash, ...



                There are quite a few (lightweight) plugins which try to enhance searching in vim with the star command.



                Typically, people miss that the star command does not consider a visual selection and secondly, what you want, to not move the cursor when pressing *.



                After installation of one of these, you would press *cgn to achieve what you want.



                In case of vim-asterisk, you have to override the builtin * yourself in your vimrc. vim-asterisk suggests:



                map * <Plug>(asterisk-*)
                map # <Plug>(asterisk-#)
                map g* <Plug>(asterisk-g*)
                map g# <Plug>(asterisk-g#)
                map z* <Plug>(asterisk-z*)
                map gz* <Plug>(asterisk-gz*)
                map z# <Plug>(asterisk-z#)
                map gz# <Plug>(asterisk-gz#)


                where z* means stay. However, you can make * to stay with



                map * <Plug>(asterisk-z*)
                map # <Plug>(asterisk-z#)
                map g* <Plug>(asterisk-gz*)
                map g# <Plug>(asterisk-gz#)


                vim-slash does this for you. This might be a reason to prefer vim-asterisk because you have control of what gets remapped.



                There are more of them. Just to give you an idea a mention a few:



                • vim-evanesco

                • SearchHighlighting

                • vim-visualstar

                Plugin with new operator: sad.vim



                If you like an explicit mapping for this operation, consider https://github.com/hauleth/sad.vim. This remaps s (changes builtin and clashes with a few other plugins, e.g. vim-sneak and vim-sandwich). siw and then press . as often you want to repeat the change for the next search matches.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Feb 13 at 11:13

























                answered Feb 13 at 10:34









                HotschkeHotschke

                1,814922




                1,814922



























                    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%2f18894%2fhow-do-i-use-cgn-for-word-under-cursor%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

                    How to check contact read email or not when send email to Individual?

                    Displaying single band from multi-band raster using QGIS

                    How many registers does an x86_64 CPU actually have?