Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/vim/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Vim中连接两行,但不移动光标_Vim - Fatal编程技术网

在Vim中连接两行,但不移动光标

在Vim中连接两行,但不移动光标,vim,Vim,如何在Vim中连接两行,并将光标保持在其原始位置,而不是跳到合并点 例如,将光标放在插入符号所指示的位置,在以下两行中进行选择: this is ^line one this is line two 由J合并产生: this is line one ^this is line two 我如何制作: this is ^line one this is line two 我尝试过Ctrl+O和“”的变体。这些似乎都不起作用。它们指向行的开头,而不是原始光标位置。您可以按如下方式操作: :nno

如何在Vim中连接两行,并将光标保持在其原始位置,而不是跳到合并点

例如,将光标放在插入符号所指示的位置,在以下两行中进行选择:

this is ^line one
this is line two
由J合并产生:

this is line one ^this is line two
我如何制作:

this is ^line one this is line two
我尝试过Ctrl+O和“”的变体。这些似乎都不起作用。它们指向行的开头,而不是原始光标位置。

您可以按如下方式操作:

:nnoremap <F2> mbJ`b
:nnoremap mbJ`b
这将为F2键指定以下操作:

  • 也就是说,创建一个标记(mb,但是如果您以前设置了
    b
    标记,那么它会被覆盖!)
  • 排队
  • 跳回上一个标记(`b)

  • 另一种不会踩到标记的方法是:

    :nnoremap <silent> J :let p=getpos('.')<bar>join<bar>call setpos('.', p)<cr>
    
    :nnoremap J:let p=getpos('.')joincall setpos('.',p)
    
    更详细,但它可以防止你失去分数

    • :nnoremap
      -非递归映射
    • -按下映射时不要回显任何内容
    • J
      -映射键
    • :让p=getpos('.')
      -存储光标位置
    • -命令分隔符(
      |
      有关地图,请参阅
      :帮助地图栏
    • join
      -用于正常的
      J
    • -
    • 调用setpos('.',p)
      -恢复光标位置
    • -运行命令

    可用于其他目的的实用功能:

    " Utility function that save last search and cursor position
    " http://technotales.wordpress.com/2010/03/31/preserve-a-vim-function-that-keeps-your-state/
    " video from vimcasts.org: http://vimcasts.org/episodes/tidying-whitespace
    if !exists('*Preserve')
        function! Preserve(command)
            try
                " Preparation: save last search, and cursor position.
                let l:win_view = winsaveview()
                let l:old_query = getreg('/')
                silent! execute 'keepjumps ' . a:command
             finally
                " Clean up: restore previous search history, and cursor position
                call winrestview(l:win_view)
                call setreg('/', l:old_query)
             endtry
        endfunction
    endif
    
    使用上述函数的解决方案的优点是:不占用任何寄存器

    " Join lines without moving the cursor (gJ prevent adding spaces between lines joined)
    nnoremap J :call Preserve("exec 'normal! J'")<cr>
    nnoremap gJ :call Preserve("exec 'normal! gJ'")<cr>
    

    也许值得一提的是,如果某人没有想到,如果设置了
    b
    标记,这将覆盖该标记。如果我想在行的开头结束,这将起作用。然而,这不是什么。不,单引号'带你到行的开头,反勾'带你到确切的位置啊,在我的浏览器中无法分辨两者之间的区别。感谢@Alexander使用文字,并没有假设我能看到差异。为什么不使用`(backtick)标记而不用b标记呢
    nnoremap m`J`
    这正是我要找的。谢谢不过,我还有一个要求。你能不能快速解释一下J后面的部分在做什么?这是一个愚蠢的vimscript问题,但我觉得它是合适的:变量
    p
    污染了什么范围?这是否可能导致奇怪的配置错误?有没有一个很好的理由不把它命名为比
    p
    更具体(也不太容易发生变量名称冲突)的名称?另外,在我所有的扶手椅旅行中,我从未接触过
    。谢谢你给我看。由于需要在一个绑定中运行多个命令,我有许多实现函数的vimrc代码段,这使得内联许多绑定成为可能。
    " Remove extra spaces at the end of the line
    
        fun! CleanExtraSpaces()
            call Preserve('%s/\s\+$//ge')
        endfun
        com! Cls :call CleanExtraSpaces()
        au! BufwritePre * :call CleanExtraSpaces()
    
    " Reident the whole file
    
        call Preserve('exec "normal! gg=G"')