首页 \ 问答 \ 从Vim中的用户定义命令切换到** insert **模式(Switch to **insert** mode from a user-defined command in Vim)

从Vim中的用户定义命令切换到** insert **模式(Switch to **insert** mode from a user-defined command in Vim)

我在我的vimrc定义了自己的命令:

command! Tcs :normal lvf`hc

目的是我继续进行反击,它会将内部内容从此处删除到下一个反引号。 喜欢:

`hi there` -> ``

问题是仍处于正常模式,我想复制c命令行为,以便我可以在插入模式下立即开始键入。

我试过command! Tcs :normal lvf`hc :startinsert command! Tcs :normal lvf`hc :startinsert但是我在最后一次c之后输入的内容将直接在编辑器中输入。

谢谢


I'm defining my own command in my vimrc:

command! Tcs :normal lvf`hc

The aim is that I go on a backtick and it will delete the inner content from here to the next backtick. Like:

`hi there` -> ``

Problem is that is remains in normal mode, I would like to replicate the c command behaviour so that I can start typing immediately in insert mode.

I've tried command! Tcs :normal lvf`hc :startinsert but whatever I put after the last c will be typed directly in the editor.

Thanks


原文:https://stackoverflow.com/questions/41805215
更新时间:2023-05-27 09:05

最满意答案

我的一个朋友在做类似的事情,并帮助我解决了这个问题。

在第2步和第3步,我试图编写输入并使用进程标准输入和输出读取输出,而且,我试图使用.lnk来运行命令提示符。 所有这些都导致了错误。

解决方案是:

  1. 创建两个批处理文件,一个用于启动命令提示符,另一个用于编译.c文件(这些文件是在程序代码之外创建的)。
  2. (运行时)使用编写的代码创建.c文件。 如果文件存在,请将其删除并创建一个新文件。
  3. 使用cmd.exe启动该过程。
  4. 运行批处理文件,使用Stream Writer将它们写入cmd.exe。
  5. 使用Stream Reader检索输出。

幸运的是,这有效! 代码结束如下:

string CompileC (string code)
{
    string path = @"C:\sample.c";
    string results = "";

    try
    {
        if (File.Exists(path))
            File.Delete(path);

        using (FileStream fs = File.Create(path))
        {
            byte[] codeText = new UTF8Encoding(true).GetBytes(code);
            fs.Write(codeText, 0, codeText.Length);
        }

        Process process = new Process();
        process.StartInfo.FileName = @"C:\Windows\system32\cmd.exe";
        process.StartInfo.UseShellExecute = false;
        process.StartInfo.RedirectStandardInput = true;
        process.StartInfo.RedirectStandardOutput = true;
        process.Start();

        using (StreamWriter sw = process.StandardInput)
        {
            if (sw.BaseStream.CanWrite)
            {
                //This batch starts up the Visual Studio Command Prompt.
                sw.WriteLine(@"C:\Startup.bat");
                //This batch does the compilation, once the Command Prompt
                //is running, using the 'cl' command.
                sw.WriteLine(@"C:\Compile.bat");
            }
        }

        using (StreamReader sr = process.StandardOutput)
        {
            if (sr.BaseStream.CanRead)
                results = sr.ReadToEnd();
        }
    }
    catch (Exception ex) { MessageBox.Show(ex.ToString()); }

    return results;
}

A friend of mine worked on something similar, and helped me to solve this problem.

On steps 2 and 3, I was trying to write the input and read the output using simply the process standard input and output, and also, I was trying to use an .lnk to run the Command Prompt. All those things caused the error.

The solution was:

  1. Create two batch files, one to start up the Command Prompt and the other to compile the .c file (those were created outside the program's code).
  2. (On runtime) Create the .c file, using the code written. If file exists, delete it and create a new one.
  3. Start the process with cmd.exe.
  4. Run the batch files, using a Stream Writer to write them in the cmd.exe.
  5. Retrieve the output using a Stream Reader.

Fortunately, this worked! Code ended like this:

string CompileC (string code)
{
    string path = @"C:\sample.c";
    string results = "";

    try
    {
        if (File.Exists(path))
            File.Delete(path);

        using (FileStream fs = File.Create(path))
        {
            byte[] codeText = new UTF8Encoding(true).GetBytes(code);
            fs.Write(codeText, 0, codeText.Length);
        }

        Process process = new Process();
        process.StartInfo.FileName = @"C:\Windows\system32\cmd.exe";
        process.StartInfo.UseShellExecute = false;
        process.StartInfo.RedirectStandardInput = true;
        process.StartInfo.RedirectStandardOutput = true;
        process.Start();

        using (StreamWriter sw = process.StandardInput)
        {
            if (sw.BaseStream.CanWrite)
            {
                //This batch starts up the Visual Studio Command Prompt.
                sw.WriteLine(@"C:\Startup.bat");
                //This batch does the compilation, once the Command Prompt
                //is running, using the 'cl' command.
                sw.WriteLine(@"C:\Compile.bat");
            }
        }

        using (StreamReader sr = process.StandardOutput)
        {
            if (sr.BaseStream.CanRead)
                results = sr.ReadToEnd();
        }
    }
    catch (Exception ex) { MessageBox.Show(ex.ToString()); }

    return results;
}

相关问答

更多

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。