【问题标题】:Is there any shortcut to change 1 word per line to any #words per line?是否有任何快捷方式可以将每行 1 个单词更改为每行任何 #words?
【发布时间】:2022-01-18 18:54:44
【问题描述】:

例如:

one
two
three
four
five
six
seven
eight
nine
ten
eleven
twelve

one two three
four five six
seven eight nine
ten eleven twelve

我不知道怎么做,只能在 vscode 上反之。

【问题讨论】:

    标签: visual-studio-code notepad++


    【解决方案1】:

    不是捷径,但您可以通过查找和替换轻松完成此类工作。

    • Ctrl+H
    • 查找内容:(\w+)\R(\w+)\R(\w+)
    • 替换为:$1 $2 $3
    • 检查 环绕
    • CHECK 正则表达式
    • 全部替换

    说明:

    (\w+)           # group 1, 1 or more word characters
    \R              # any kind of linebreak
    (\w+)           # group 2, 1 or more word characters
    \R
    \R              # any kind of linebreak
    

    屏幕截图(之前):

    截图(之后):

    【讨论】:

      【解决方案2】:

      不是 VS Code,但你可以使用这个 sn-p:

      如果您再次需要,只需将此答案添加为书签(或将 sn-p 信息复制并粘贴到您设备上的 HTML 文件中)。另外,我认为“复制到剪贴板”按钮不起作用,因为 sn-p 在跨域 iframe 中运行,但它应该在同源上下文中工作。

      function splitWordsPerLine (text, wpl = 1) {
        let result = '';
        wpl = wpl < 1 ? 1 : wpl;
        let count = wpl;
      
        for (const word of text.split(/\s+/)) {
          count -= 1;
          let line = word;
          if (count === 0) {
            line += '\n';
            count = wpl;
          }
          else line += ' ';
          result += line;
        }
      
        return result.trim();
      }
      
      function getWPL (numberInput) {
        if (!numberInput) return 1;
        const wpl = parseInt(numberInput.value, 10);
        return Number.isNaN(wpl) ? 1 : wpl;
      }
      
      function handleInput (event) {
        const wpl = getWPL(event.target);
        const textInput = document.getElementById('text');
        if (!textInput) return;
        textInput.value = splitWordsPerLine(textInput.value, wpl);
      }
      
      async function handleClick (event) {
        let message = 'Copying failed ?';
        const textInput = document.getElementById('text');
        try {
          if (!textInput) throw new Error('No input found');
          await navigator.clipboard.writeText(textInput.value);
          message = 'Text copied ✅';
        }
        catch {}
        textInput?.select();
        const setText = str => event.target.textContent = str;
        setText(message);
        setTimeout(() => setText('Copy to clipboard'), 1500);
      }
      
      function handlePaste (event) {
        const text = event.clipboardData?.getData('text');
        if (!text) return;
      
        const wpl = getWPL(document.getElementById('wpl'));
        event.target.value = splitWordsPerLine(text, wpl);
      
        event.preventDefault();
      }
      
      document.getElementById('wpl')?.addEventListener('input', handleInput);
      document.getElementById('copy')?.addEventListener('click', handleClick);
      document.getElementById('text')?.addEventListener('paste', handlePaste);
      html {
        box-sizing: border-box;
        height: 100%;
      }
      
      *, *:before, *:after {
        box-sizing: inherit;
      }
      
      body {
        font-family: sans-serif;
        height: 100%;
        margin: 0;
        padding: 1rem;
      }
      
      .container {
        display: flex;
        gap: 0.5rem;
      }
      
      .container.vertical {
        flex-direction: column;
        height: 100%;
      }
      
      #copy {
        background-color: black;
        border: 0;
        color: white;
        display: inline-flex;
        align-items: center;
        font-size: 1rem;
        padding: 0.5rem;
      }
      
      #wpl, #text {
        border: 1px solid;
        font-family: monospace;
        padding: 0.5rem;
      }
      
      #wpl {
        font-size: 1.5rem;
        width: 5rem;
      }
      
      #text {
        font-size: 1rem;
        height: 100%;
        width: 100%;
        resize: none;
        white-space: pre;
      }
      <div class="container vertical">
        <div class="container">
          <input id="wpl" type="number" min="1" step="1" value="1" />
          <button id="copy">Copy to clipboard</button>
        </div>
        <textarea id="text" rows="0" cols="0" placeholder="Select number of words per line, then paste your text here"></textarea>
      </div>

      【讨论】:

        【解决方案3】:

        您可以使用扩展名Select By 和命令selectby.lineNr

        • 将光标放在第一行
        • 执行命令:根据行号放置光标,使用布尔表达式
        • 输入表达式:c+3k 每 3 行放置一个光标
        • 也许表达式:c+3k &amp;&amp; n&lt;50 来限制使用结束行
        • 现在根据需要经常使用 End Space Delete
        • Esc 退出多光标模式

        【讨论】:

          猜你喜欢
          • 2011-09-03
          • 1970-01-01
          • 1970-01-01
          • 2021-12-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-12-04
          • 1970-01-01
          相关资源
          最近更新 更多