簡體   English   中英

vim-如何選擇多行?

[英]vim - how to select multiple lines?

說我有這段文字:

#!/bin/bash
# Get host
db_host=$(echo "dbhost") 
# Get DB name
db_name=$(echo "dbname")
# Get user
db_user=$(echo "dbuser")
# Get password
db_pass=$(echo "dbpass")

我想選擇每個變量名稱並在文本下方產生此輸出:

echo "db_host: $db_host"
echo "db_name: $db_name"
echo "db_user: $db_user"
echo "db_pass: $db_pass"

例如,在崇高下 ,我突出顯示=$(並多次按 + d ,返回到行的開頭,使用SHIFT + 復制,轉到最后一行並創建一個新行,粘貼並突出顯示所有新行。行,按 + SHIFT + l ,然后添加我想要的任何內容。

我正在使用vim multiple cursors但是我不確定這是要走的路。 有方向嗎?

有很多方法可以做到。 這是一個:從第一個db_host開始處的游標開始:

yGGp              Yank everything, paste at the bottom
:.,$g/^#/d<CR>    In the pasted part, remove all comment lines
<C-O>             Back to start of the pasted part
<C-V>GI           Select the first column, prepend
echo "<Esc>
f=<C-V>           Then select the block from equals to quote, change
f"Gc
: $<Esc>
f)<C-V>           Then select the block at the closing brace, change
G$c
;<Esc>

使用宏和寄存器的另一種方法是從同一位置開始:

qqq               Clear register q
qwq               Clear register w
qq                Start recording macro in q
yaw               yank a word (db_name) to default register
o                 open a new line below, and start insert
echo "
<C-R>"            insert the content of the default register (db_name)
: $
<C-R>"            insert the content of the default register (db_name) again
"<Esc>
"Wdd              the line is done; yank-append to register w
j0                skip the comment line, position at the start of the next variable
@q                execute the q macro (which is currently empty, but not for long)
q                 save the q macro
@q                execute the q macro (which will recurse, and slurp up all lines)
"wp               paste all the accumulated lines at the bottom

第三,使用regexp:

:.,$v/^#/t$<CR>   copy all non-comment lines to the end
:-3,.s/\(\w\+\).*/echo "\1: $\1"/<CR>
                  I didn't set a mark so manual range from 3 lines above:
                  capture the first word, discard everything else,
                  replace with what we want (obviously, could have done
                  visual selection instead of manually setting range)

這是我的處理方式:

首先,用替換創建回聲線:

:%s/\(db_\w\+\)=\$(echo "\w\+")/&\recho "\1: $\1"

注意替換中的& ,它是整個匹配的行。 緊跟\\r以換行。

然后,清空將用於它的寄存器(在我的情況下為a ):

qaq

然后在所述寄存器中剪切所有回顯線:

:g/^echo /d A

最后,轉到您想要的位置,然后粘貼寄存器的內容:

"ap

稍微不同的解決方案:

:g/echo/t$
:-3,.normal! ywccecho "<Ctrl-v><Ctrl-r>0: $<Ctrl-v><Ctrl-r0"

說明

:g ................... global command
/echo/ ............... applied on each line that has "echo"
t$ ................... copy to the end

光標將移至末尾,因此我們將間隔設置為負三行,直到當前行-3,.

yw ................... copy the first word
cc ................... start chnanging the whole line
echo " ............... inserts a literal `echo "`
Ctrl-v Ctrl-r 0 ...... inserts the word we copied

注意:要插入寄存器零0我們必須輸入Ctrl-v Ctrl-r 0

使用替代命令代替普通命令

第二個命令可以是替代

:-3,.s/\v(^\w+).*/echo "\1: $\1"

vim解決方案如何成為更智能的解決方案?

如果您要更改成百上千的行,那么我認為多個游標將無濟於事,尤其是當模式在多行上交錯時。

也許我錯過了您想做的事情,但是如果我理解正確的話,您只是想更新輸出的格式。

嘗試這個:

:g/[(]echo "/s/echo "\([^"]*\)"\(.*\)$/echo "\1"\2^Mecho "\1: $\1"/

注意:^ M是單個控制字符,不是克拉,然后是M。
見下文。

之前-

#!/bin/bash
# Get host
db_host=$(echo "dbhost")
# Get DB name
db_name=$(echo "dbname")
# Get user
db_user=$(echo "dbuser")
# Get password
db_pass=$(echo "dbpass")

之后-

#!/bin/bash
# Get host
db_host=$(echo "dbhost")
echo "dbhost: $dbhost"
# Get DB name
db_name=$(echo "dbname")
echo "dbname: $dbname"
# Get user
db_user=$(echo "dbuser")
echo "dbuser: $dbuser"
# Get password
db_pass=$(echo "dbpass")
echo "dbpass: $dbpass"

說明

:g/[(]echo "/s/echo "\([^"]*\)"\(.*\)$/echo "\1"\2^Mecho "\1: $\1"/

碎片-

:g/pat/cmd

這表示在與“ pat”匹配的每行上“全局”執行“ cmd”。 我的“拍子”是您的(echo " ,而我的“ cmd”是一個替換。

s/echo "\([^"]*\)"\(.*\)$/echo "\1"\2^Mecho "\1: $\1"/

我說用echo "..."...代替,並記住引號之間是1,之后是2。

替換部分說放回那里的任何東西( echo "\\1"\\2 ),然后放一點鞭子。

通過按CTRL-V,我進入了quote-mode,這使我可以按CTRL-M插入一個回車符vim在執行時轉換為換行符,至少在我使用的版本上。

這是一個把戲。 知道和使用它可能很方便,但是請始終牢記,這些東西基本上都是黑客。 警告腳本。

接下來,我使用格式化的echo "\\1: $\\1"添加所需的行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM