如何更改Vim中的连接字符?

时间:2012-03-26 18:55:31

标签: vim

在Vim中,可以通过键入J来加入两行。 但是,这些通常由空格连接。

我似乎记得有一种方法可以通过设置一些变量来改变用于加入的字符,但我似乎无法再找到它。

如果有人能提醒我,或者确认无法完成,我会很感激。

6 个答案:

答案 0 :(得分:10)

当我想加入几行时,我使用3键组合(普通模式):

Jr,

成为,加入角色。

如果我想加入更多行甚至连接组中的行,我将前一个组合用于宏。

例如,要转换3列CSV表格中的3行,我会记录此宏(当然分配给字母j):

qjJr,Jr,jq

因此,使用@j使用,加入3行并转到下一行。

10@j转换10行。

答案 1 :(得分:5)

没有允许您直接执行此操作的设置,请参阅:

:help J

特别是命令列表下面的文字。

有两种方法可以做到这一点:

:nnoremap J gJi.<ESC>
" or
let joinchar = ';'
nnoremap J :s/\n/\=joinchar/<CR>

后一个选项允许您通过更改joinchar选项来动态更改它。

答案 2 :(得分:1)

在.vimrc中尝试这样的事情:

nnoremap Y Jxi*<Esc>

它会重新映射Y以使用*加入行。

答案 3 :(得分:0)

来自http://vim.wikia.com/wiki/Remap_join_to_merge_comment_lines

把它放在你的.vimrc中:

function! JoinWithLeader(count, leaderText)
  let l:linecount = a:count
  " default number of lines to join is 2
  if l:linecount < 2
    let l:linecount = 2
  endif
  echo l:linecount . " lines joined"
  " clear errmsg so we can determine if the search fails
  let v:errmsg = ''

  " save off the search register to restore it later because we will clobber
  " it with a substitute command
  let l:savsearch = @/

  while l:linecount > 1
    " do a J for each line (no mappings)
    normal! J
    " remove the comment leader from the current cursor position
    silent! execute 'substitute/\%#\s*\%('.a:leaderText.'\)\s*/ /'
    " check v:errmsg for status of the substitute command
    if v:errmsg=~'Pattern not found'
      " just means the line wasn't a comment - do nothing
    elseif v:errmsg!=''
      echo "Problem with leader pattern for JoinWithLeader()!"
    else
      " a successful substitute will move the cursor to line beginning,
      " so move it back
      normal! ``
    endif
    let l:linecount = l:linecount - 1
  endwhile
  " restore the @/ register
  let @/ = l:savsearch
endfunction

nnoremap <space> :<C-U>call JoinWithLeader(v:count, '"')<CR>

这也允许您将J重新映射到其他内容。

答案 4 :(得分:0)

如果用逗号(或连接字符)

替换行尾,会更快
:%s/$/,

然后通过提供范围或通过在可视模式下选择线并使用连接命令来连接多条线

10J

答案 5 :(得分:-1)

这是映射。你可以阅读vim wikia中的教程: Mapping keys in vim

在命令模式下尝试以下命令,然后尝试按。这应该工作:)

:map <space> J