Loading ...
Sorry, an error occurred while loading the content.

Remapping space to toggle folds breaks 4c commands

Expand Messages
  • David Fishburn
    I am trying out this Vim tip: Toggle fold state between closed and opened. Based on Vim tip: Tip #108: Toggle a fold with a single keystroke
    Message 1 of 5 , May 31, 2003
    • 0 Attachment
      I am trying out this Vim tip:

      " Toggle fold state between closed and opened.
      " Based on Vim tip:
      " Tip #108: Toggle a fold with a single keystroke
      " http://www.vim.org/tips/tip.php?tip_id=108
      "
      " If there is no fold at current line, just moves forward.
      " If it is present, reverse it's state.
      function! ToggleFold()
      if foldlevel('.') == 0
      normal! l
      else
      if foldclosed('.') < 0
      . foldclose
      else
      . foldopen
      endif
      endif
      " Clear status line
      echo
      return "\<Space>"
      endfunction

      " Map this function to Space key.
      noremap <space> :call ToggleFold()<CR>

      I added the return "\<Space>" at the end to try and solve a problem
      I have with it.

      I frequently use commands like this:
      If I have a variable named:
      prev_line_nbr

      I would be on the p and type "4c ". That would change 4 characters.
      Unfortunately, the space is eaten by the toggle command. So I tried to
      return a "\<Space>" hoping that would provide the usual behaviour.

      I checked the help on c-ctrl-r and i-crtl-r and ctrl-r thinking that
      might provide an option, but I didn't get anywhere.

      Can anyone think of way to get both of these working?

      Thanks,
      Dave
    • Piet Delport
      ... [snip] ... [snip] Have you tried using :nnoremap instead? In general, to avoid these kind of problems, never use :noremap unless you re sure that a mapping
      Message 2 of 5 , May 31, 2003
      • 0 Attachment
        On Sat, 31 May 2003 at 16:30:44 -0400, David Fishburn wrote:
        >
        > I am trying out this Vim tip:
        >
        > " Toggle fold state between closed and opened.
        > " Based on Vim tip:
        > " Tip #108: Toggle a fold with a single keystroke
        > " http://www.vim.org/tips/tip.php?tip_id=108
        > "
        [snip]
        >
        > " Map this function to Space key.
        > noremap <space> :call ToggleFold()<CR>
        >
        [snip]

        Have you tried using :nnoremap instead?

        In general, to avoid these kind of problems, never use :noremap unless
        you're sure that a mapping makes sense in Visual and Operater-pending
        modes as well.

        --
        Piet Delport
        Today's subliminal thought is:
      • Benji Fisher
        ... For more details, read ... HTH --Benji Fisher
        Message 3 of 5 , Jun 1 5:46 PM
        • 0 Attachment
          Piet Delport wrote:
          > On Sat, 31 May 2003 at 16:30:44 -0400, David Fishburn wrote:
          >
          >>I am trying out this Vim tip:
          >>
          >>" Toggle fold state between closed and opened.
          >>" Based on Vim tip:
          >>" Tip #108: Toggle a fold with a single keystroke
          >>" http://www.vim.org/tips/tip.php?tip_id=108
          >>"
          >
          > [snip]
          >
          >>" Map this function to Space key.
          >>noremap <space> :call ToggleFold()<CR>
          >>
          > [snip]
          >
          > Have you tried using :nnoremap instead?
          >
          > In general, to avoid these kind of problems, never use :noremap unless
          > you're sure that a mapping makes sense in Visual and Operater-pending
          > modes as well.

          For more details, read

          :help map-modes
          :help vim-modes

          HTH --Benji Fisher
        • Pavel A. Papushev
          Hi! Sunday, June 1, 2003, 3:30:44 AM, you wrote: DF I am trying out this Vim tip: DF [...] DF If there is no fold at current line, just moves forward. DF
          Message 4 of 5 , Jun 2 11:13 PM
          • 0 Attachment
            Hi!
            Sunday, June 1, 2003, 3:30:44 AM, you wrote:
            DF> I am trying out this Vim tip:
            DF> [...]
            DF> " If there is no fold at current line, just moves forward.
            DF> " If it is present, reverse it's state.
            DF> function! ToggleFold()
            DF> if foldlevel('.') == 0
            DF> normal! l
            DF> else
            DF> if foldclosed('.') < 0
            DF> . foldclose
            DF> else
            DF> . foldopen
            DF> endif
            DF> endif
            DF> " Clear status line
            DF> echo
            DF> return "\<Space>"
            DF> endfunction

            DF> " Map this function to Space key.
            DF> noremap <space> :call ToggleFold()<CR>
            DF> [...]
            DF> I frequently use commands like this:
            DF> If I have a variable named:
            DF> prev_line_nbr
            DF> I would be on the p and type "4c ". That would change 4 characters.
            DF> Unfortunately, the space is eaten by the toggle command. So I tried to
            DF> return a "\<Space>" hoping that would provide the usual behaviour.

            Piet Delport already explained you that you shouldn't redefine
            <space> in operator-pending mode. It's useless to return "\Space"
            because it is not used anyhow at all. I want to add, that there is
            more shorter way to do exactly what you want:

            nnoremap <silent> <space>
            \ :exe 'silent! normal! za'.(foldlevel('.')?'':'l')<cr>

            Explanation:
            - 'normal! za' toggles folds.
            - 'silent!' allows to avoid error message when current line doesn't
            delong to fold.
            - (foldlevel('.')?'':'l') adds forward moving only if current line
            doesn't delong to fold.

            --
            Best regards,
            Pavel ;)
          • David Fishburn
            Excellent, and thanks for updating the tip online as well. ... From: Pavel A. Papushev [mailto:ppa-nsu@mail.ru] Sent: Tuesday, June 03, 2003 2:14 AM To: David
            Message 5 of 5 , Jun 3 8:54 AM
            • 0 Attachment
              Excellent, and thanks for updating the tip online as well.

              -----Original Message-----
              From: Pavel A. Papushev [mailto:ppa-nsu@...]
              Sent: Tuesday, June 03, 2003 2:14 AM
              To: David Fishburn
              Cc: vim@...
              Subject: Re: Remapping space to toggle folds breaks 4c<space> commands


              Hi!
              Sunday, June 1, 2003, 3:30:44 AM, you wrote:
              DF> I am trying out this Vim tip:
              DF> [...]
              DF> " If there is no fold at current line, just moves forward. " If it
              DF> is present, reverse it's state. function! ToggleFold()
              DF> if foldlevel('.') == 0
              DF> normal! l
              DF> else
              DF> if foldclosed('.') < 0
              DF> . foldclose
              DF> else
              DF> . foldopen
              DF> endif
              DF> endif
              DF> " Clear status line
              DF> echo
              DF> return "\<Space>"
              DF> endfunction

              DF> " Map this function to Space key.
              DF> noremap <space> :call ToggleFold()<CR>
              DF> [...]
              DF> I frequently use commands like this:
              DF> If I have a variable named:
              DF> prev_line_nbr
              DF> I would be on the p and type "4c ". That would change 4 characters.

              DF> Unfortunately, the space is eaten by the toggle command. So I tried

              DF> to return a "\<Space>" hoping that would provide the usual
              DF> behaviour.

              Piet Delport already explained you that you shouldn't redefine <space>
              in operator-pending mode. It's useless to return "\Space" because it is
              not used anyhow at all. I want to add, that there is more shorter way to
              do exactly what you want:

              nnoremap <silent> <space>
              \ :exe 'silent! normal! za'.(foldlevel('.')?'':'l')<cr>

              Explanation:
              - 'normal! za' toggles folds.
              - 'silent!' allows to avoid error message when current line doesn't
              delong to fold.
              - (foldlevel('.')?'':'l') adds forward moving only if current line
              doesn't delong to fold.

              --
              Best regards,
              Pavel ;)
            Your message has been successfully submitted and would be delivered to recipients shortly.