[this] []、[that] []、...、[other] []のような単語のマークダウンファイルがあります。私はMacVimでこれらの単語を見つける方法を知っていますが、どのようにそれらを[この] [1]、[2]、...、[その他] [n]場合?Vim/sed/awkインクリメンタル整数での検索と置換
また、MacVimを使用するより簡単なことが判明した場合は、sedまたはawkまたはRubyを使用してソリューションを受け入れることもできます。
[this] []、[that] []、...、[other] []のような単語のマークダウンファイルがあります。私はMacVimでこれらの単語を見つける方法を知っていますが、どのようにそれらを[この] [1]、[2]、...、[その他] [n]場合?Vim/sed/awkインクリメンタル整数での検索と置換
また、MacVimを使用するより簡単なことが判明した場合は、sedまたはawkまたはRubyを使用してソリューションを受け入れることもできます。
ruby -p -e \
'begin t=$_.clone; $_.sub! "][]", "][#{@[email protected]_i+1}]";end until t==$_' \
< somefile
あるいは、編集ファイルインプレースバージョンの:
ruby -i.tmp -p -e \
'begin t = $_.clone; $_.sub! "][]", "][#{@[email protected]_i+1}]"; end until t == $_' \
somefile
あなたは一度だけそれをしなければならない、と二度と、その後、エディタでそれをやっては罰金の場合。繰り返し行う必要がある場合は、手動で行うことが大きな苦痛になります。それは自動化が必要なときです。
ターゲットを含むテキストのサンプルがないと、暗闇の中での撮影に似ていますこれは、Rubyを使ってあなたの説明に近いようだ:
text = %{
[Lorem][] ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut
labore [et][] dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco
laboris nisi [ut][] aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in
voluptate velit esse [cillum][] dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat
non proident, sunt in culpa qui [officia deserunt][] mollit anim id est laborum.
}
text.scan(/\[[^\]]+\]\[\]/).each_with_index{ |t, i| text[t] = t.sub('[]', "[#{1 + i}]") }
puts text
# >>
# >> [Lorem][1] ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut
# >> labore [et][2] dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco
# >> laboris nisi [ut][3] aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in
# >> voluptate velit esse [cillum][4] dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat
# >> non proident, sunt in culpa qui [officia deserunt][5] mollit anim id est laborum.
は、この試してみて:
awk 'BEGIN{c=1}{for(w=1;w<=NF;w++){s=sub("\\[\\]","["c"]",$w);if(s)c++};print}' inputfile
小さな提案:awk 'BEGIN {c = 1} {(w = 1; w <= NF; w ++){c + = sub( "\\\ [\\\]"、 "[" c "]" $ w)}; print} ' – Beta
@ベータ:ありがとう。私はそれを見落とした。 –
perl -p -i -e 's/(\[.*?\])\[\]/"$1\[".(++$i)."]"/ge' /path/to/file
のVim:
を:let g:lastcount=0
:function PlusPlus()
let g:lastcount+=1
return g:lastcount
endfunction
:%g/./s/\V[\.\{-}][\zs\ze]/\=PlusPlus()/g
さて、これをVimで解決することは可能です。私は物事のこれらの並べ替えのために、今しばらくの間、このインクリメンタオブジェクトを使用している:
--- 8 < --- vimのコード
function! Incrementor(start, step)
let incrementor = {}
let incrementor.initial_value = a:start
let incrementor.value = incrementor.initial_value
let incrementor.step = a:step
function incrementor.val() dict
return self.value
endfunction
function incrementor.next() dict
let self.value += self.step
return self.value
endfunction
function incrementor.reset() dict
let self.value = self.initial_value
return self.value
endfunction
return incrementor
endfunction
" With the Incrementor function above saved in, say,
" ~/.vim/plugin/incrementor.vim, you can then create incrementors as you need
" them and use them in substitutions, like this:
let inc = Incrementor(0,1)
28,$s/\v\[(\w+)\]\[\]/\="[".submatch(1)."][".inc.next()."]"/
finish
" test case
foo
[this][]
[that][]
[theother][]
bar
コピーその権利「バー」までの全コードサンプル最後にファイルに保存し、保存してVimからテストするソース(:so%)を指定します。
投稿を編集して、忘れてしまったことを追加できます。 – ZyX
あなたはわずか数vimのコマンドを使用して、かなり簡単にこれを行うには、マクロことができます。
/\[\]<cr>a1<esc>qqyi[np<C-a>[email protected]
、文字列の検索である「[]」は、最初の1の番号1を追加します。その後、マクロの記録を開始します。 []の中のすべてをヤンクし、次の試合に行って貼り付けます。その後、番号を増やします。記録を停止し、必要な回数だけマクロを再生します。それは毎回挿入する番号をインクリメントします。
vimで行うことを自動化したい場合は、マッピング(またはより複雑なタスクのためのスクリプト)を書くだけです。ここに痛みはありません、これがなぜvimのような多くのプログラマです。 – ZyX
ちなみに、vimでこれを行うと、簡単に元に戻すことができます(エディタを終了しても、元に戻すことができます)。 – ZyX
私は職場や家で私の編集の99%をvim/gvim/macvimに住んでいますが、私はまだそれをオートメーションツールとして使ってみません。 –