简体   繁体   English

如何在 Visual Studio 代码中的行尾添加分号

[英]How to add semicolon to the end of the line in visual studio code

I press Shift + Enter , but not working, Ctrl + Enter start a new line, but not add semicolon at the end of previous line.我按Shift + Enter ,但不起作用, Ctrl + Enter开始新行,但不在上一行的末尾添加分号。 Is there a shortcut?有捷径吗?

Now there is an extension called Colonize :现在有一个名为Colonize的扩展:

Shift + Enter Insert semicolon at the end of line and continue on the same line Shift + Enter在行尾插入分号并在同一行继续

Alt + Enter Insert semicolon at the end of line and continue on the new line Alt + Enter在行尾插入分号并在新行继续

Ctrl + Alt + Enter Insert semicolon and stay at the same position Ctrl + Alt + Enter插入分号并保持在同一位置

Visual Studio Code doesn't appear to directly support adding a semi-colon to the end of a line via a keyboard shortcut, meaning it'll need a custom extension to support it. Visual Studio Code 似乎不直接支持通过键盘快捷键在行尾添加分号,这意味着它需要自定义扩展来支持它。 Fortunately, Sergii Naumov has created one for us, named (appropriately) 'Trailing Semicolon'.幸运的是,Sergii Naumov 为我们创建了一个,命名为(恰当地)“尾随分号”。

To install this extension:要安装此扩展程序:

  1. Launch Visual Studio Code启动 Visual Studio 代码
  2. Press "Ctrl+P" (or "Command+P" on the Mac)按“Ctrl+P”(或 Mac 上的“Command+P”)
  3. type "ext install Trailing Semicolon" to search for the extension键入“ext install Trailing Semicolon”以搜索扩展名
  4. With the extension visible below the search box, click the Install Extension icon in the bottom right of the extension.在搜索框下方显示扩展程序的情况下,单击扩展程序右下角的安装扩展程序图标。 (It's a cloud with an arrow pointing down...) (这是一个箭头指向下方的云......)

You will need to restart Visual Studio Code to enable the extension (Visual Studio Code should prompt you to restart once the extension has been successfully downloaded.)您将需要重新启动 Visual Studio Code 以启用扩展(成功下载扩展后,Visual Studio Code 应提示您重新启动。)

According to Sergii, the default keybinding for the extension is "cmd+;".根据 Sergii 的说法,扩展的默认键绑定是“cmd+;”。 On my Mac, this meant pressing "Command+;", which worked great.在我的 Mac 上,这意味着按下“Command+;”,效果很好。

On my Windows 10 PC, I tried several key combinations ("Window+;" "Ctrl+;" "Alt+;" etc.), and nothing worked.在我的 Windows 10 PC 上,我尝试了几个组合键(“Window+;”“Ctrl+;”“Alt+;”等),但没有任何效果。 I opened the Keyboard Shortcut preferences (File > Preferences > Keyboard Shortcuts) and searched for the new extension.我打开了键盘快捷键首选项(文件 > 首选项 > 键盘快捷键)并搜索了新的扩展名。 I found it listed at the end of the keybindings:我发现它列在键绑定的末尾:

{ "key": "win+;",                 "command": "extension.trailing-semicolon",
                                  "when": "editorTextFocus" }

The "win" key binding apparently doesn't work. “win”键绑定显然不起作用。 I copied this binding to the keybindings.json file, changed "win+;"我将此绑定复制到 keybindings.json 文件,更改为“win+;” to "ctrl+;"到 "ctrl+;" and it worked like a charm!它就像一个魅力!

Hope that helps.希望有帮助。

I wrote an extension to mimic IntelliJ's complete statement.我写了一个扩展来模仿 IntelliJ 的完整声明。 Pressing ctrl+;ctrl+; ( cmd+; on mac) appends ; ( cmd+;在 mac 上) 附加; to the line end, and moves cursor to line end.到行尾,并将光标移动到行尾。 If the line already ends with ;如果该行已经以;结尾, pressing ctrl+; , 按ctrl+; just moves cursor to line end.只是将光标移动到行尾。

There is also experimental support for complete structures like class , interface , function , if , switch , for , and while .还有对完整结构的实验性支持,例如classinterfacefunctionifswitchforwhile (Not understanding semantic of languages, so it may not work as you expected.) (不了解语言的语义,因此它可能无法按您的预期工作。)

You can install it as VSIX at GitHub .您可以在GitHub将其安装为 VSIX。

The version vscode marketplace is outdated. vscode 市场版本已过时。 (Unfortunately my M$ account has been suspended, thus I cannot update it.) (不幸的是,我的 M$ 帐户已被暂停,因此我无法更新。)

There is no way to do it by default that I could find.默认情况下,我无法找到它。 I just had to make do as best I could.我只能尽力而为。

I ended up adding a binding via File>Preferences>Keyboard Shortcuts, and then pressing ;我最终通过 File>Preferences>Keyboard Shortcuts 添加了一个绑定,然后按 ; after the shortcut.快捷方式之后。 Having the semicolon is part of the binding is as close as I could get... at least my finger is already over that key将分号作为绑定的一部分是我能得到的最接近的......至少我的手指已经在那个键上

// Place your key bindings in this file to overwrite the defaults
[
    {
        "key": "ctrl+;",
        "command": "cursorEnd",
        "when": "editorTextFocus"
    }
]

This simply means that Ctrl+;这只是意味着 Ctrl+; brings you to the end of the line, and then tap ;将您带到行尾,然后点击 ; again.再次。

Update (May 2021)更新(2021 年 5 月)

Started using VSCode Vim keybindings extension a few months ago.几个月前开始使用VSCode Vim键绑定扩展。 Highly recommend it.强烈推荐它。 Out of interest, semi-colon end of the line is A;出于兴趣,分号结尾是A; with vim keys⌨️用 vim 键⌨️

at 2021 2021 年

settings >search for colon You will find a javascript option to insert the semi-colon.设置> 搜索冒号您将找到一个用于插入分号的 javascript 选项。

Try install the following ext then use shortcut: Ctr+;尝试安装以下分机,然后使用快捷方式: Ctr+; (Cmd+;) (Cmd+;)

I just started using Visual Studio Code and felt this requirement myself yesterday.我刚开始使用 Visual Studio Code,昨天自己也感受到了这个要求。 After a quick google search I found this nice extension called "Prettier".经过快速的谷歌搜索后,我发现了这个名为“Prettier”的不错的扩展。 Being a little new to VSCode it took me a few hours to get it all setup but it works like a charm now.作为 VSCode 的新手,我花了几个小时来完成所有设置,但现在它就像一个魅力。 Here are the steps and my setup.这是步骤和我的设置。 I hope it helps others.我希望它可以帮助其他人。

My coding environment : VSCode running on a Windows 10 desktop environment connecting to my codebase SMB share hosted on my development machine which is running Ubuntu server 18.04.我的编码环境:在 Windows 10 桌面环境上运行的 VSCode 连接到我在运行 Ubuntu 服务器 18.04 的开发机器上托管的代码库 SMB 共享。

Solution Steps解决步骤

  • Install node on the Windows desktop在 Windows 桌面上安装node
  • Run, npm install -g prettier运行, npm install -g prettier
  • Install the Prettier extension in VSCode在 VSCode 中安装Prettier扩展
  • Edit the settings.json file for VSCode and add the following编辑 VSCode 的settings.json文件并添加以下内容

     "editor.defaultFormatter": "esbenp.prettier-vscode", "[javascript]": { "editor.defaultFormatter": "esbenp.prettier-vscode" } // Set the default "editor.formatOnSave": true
  • Add the .prettierrc file at the root of my codebase on the ubuntu host (eg: /var/www/html/tutorials ) with the following basic styling configuration.prettierrc文件添加到 ubuntu 主机上我的代码库的根目录(例如: /var/www/html/tutorials ),并具有以下基本样式配置

    {
      "semi": true,
      "trailingComma": "all",
      "singleQuote": true,
      "printWidth": 80
    }
  • Restart VSCode and open the code file重启VSCode并打开代码文件
  • Use the existing VSCode keyboard shortcuts to apply formatting to the entire file (Ctrl+K Ctrl+F) or to a selection (Shift+Alt+F)使用现有的 VSCode 键盘快捷键将格式应用于整个文件(Ctrl+K Ctrl+F)或选定内容(Shift+Alt+F)
  • Or simply saving the file Ctrl+S adds the formatting while saving the file with no additional work required或者直接保存文件Ctrl+S添加格式,同时保存文件,无需额外工作
  • Viola!中提琴!

我知道这是一篇很老的帖子,但 Prettier - vs code 的代码格式化程序添加了分号(在其他格式中)。

ext install esbenp.prettier-vscode

(March 2022) Missing semi colons can be added automatically without the needing of an extension (*with a little catch ;) They are added every time your code is formatted. (2022 年 3 月)可以自动添加缺少的分号,而无需扩展名(* 带有一点点 ;)每次格式化代码时都会添加它们。

Go to settings: Look for this settings if you are writing javascript:转到设置:如果您正在编写 javascript,请查找此设置: 在此处输入图像描述

And/or go to settings: Look for this settings if you are writing typescript:和/或转到设置:如果您正在编写打字稿,请查找此设置: 在此处输入图像描述 Go to settings: Look for this settings if you are writing javascript:转到设置:如果您正在编写 javascript,请查找此设置:

Format your code and semi colons will be added \o/ Yeah格式化您的代码并添加分号 \o/ 是的

*The catch: As the settings says: Your workspace needs to have typescript 3.7 or newer. *问题:正如设置所说:您的工作区需要有 typescript 3.7 或更高版本。

Ctrl+Shift+P and open Preferences:Open keyboard shortcuts (JSON) in VS Code Ctrl+Shift+P并打开 Preferences:Open keyboard shortcuts (JSON) in VS Code

and insert并插入

{
    "key": "tab",
    "command":"cursorEnd",
    "when":"editorTextFocus"
}

tab is optional, ofc. tab是可选的,ofc。 Put whatever you want from the keys on the keyboard.把你想要的任何东西放在键盘上的键上。

SHORT ANSWER:简短回答:

Shift + Enter Shift +输入

LONG ANSWER:长答案:

As ST7 mentioned , you can add the Colonize extension, but apparently Visual Studio (I'm currently using 2017) contains Shift + Enter shortcut by default, which unlike Colonize, continues on the new line.正如 ST7提到的,您可以添加 Colonize 扩展,但显然 Visual Studio(我目前使用的是 2017)默认包含Shift + Enter快捷方式,与 Colonize 不同,它在行上继续。

There is a way to have semicolon appended automatically by VSCode .有一种方法可以让 VSCode 自动附加分号

By default this property is disabled:默认情况下,此属性被禁用:

  1. For Windows ctrl + ,对于 Windows ctrl + ,
  2. For Mac cmd + ,对于 Mac cmd + ,

Settings -> in Search bar type: 'semicolon' -> c hange values to 'insert' for both JS and TS , save and close .设置->在搜索栏中输入: 'semicolon' -> 将JSTS值更改为 'insert'保存并关闭

Now every time on save, VSCode will look for missing semicolons inside the file you're in and append them.现在每次保存时,VSCode 都会在您所在的文件中查找丢失的分号并附加它们。

设置

Go to settings: Look for this settings if you are writing javascript: enter image description here Go 到设置:如果您正在编写 javascript,请查找此设置:在此处输入图像描述

And/or go to settings: Look for this settings if you are writing typescript: enter image description hereGo to settings: Look for this settings if you are writing javascript:和/或 go 设置:如果您正在编写 typescript,请查找此设置:在此处输入图像描述转到设置:如果您正在编写 javascript,请查找此设置:

Format your code and semi colons will be added \o/ Yeah格式化您的代码并添加分号 \o/ 是的

*The catch: As the settings says: Your workspace needs to have typescript 3.7 or newer. *问题:正如设置所说:您的工作区需要具有 typescript 3.7 或更高版本。

This solution works for me这个解决方案对我有用

Add the following configuration in settings.json:在settings.json中添加如下配置:

"css.completion.completePropertyWithSemicolon": true

or you can cancel by:或者您可以通过以下方式取消:

"css.completion.completePropertyWithSemicolon": false

other其他

"scss.completion.completePropertyWithSemicolon": true,
"less.completion.completePropertyWithSemicolon": true

Add semicolon to the end of the line and go to the next line在行尾添加分号,在下一行添加 go

Shortcode: ctrl+;简码: ctrl+;

Install plugin: multicomando安装插件: multicomando

ext install ryuta46.multi-command

Edit file: keybindings.json编辑文件: keybindings.json

// Place your key bindings in this file to override the defaults
[
    {
        "key": "ctrl+;",
        "command": "extension.multiCommand.execute",
        "args": { 
            "sequence": [
                "cursorEnd",
                { "command": "type", "args": { "text": ";" } },
                "lineBreakInsert",
                "cursorDown"
            ]
        },
        "when": "editorTextFocus"
    }
]
  • This plugin promotes a sequence of commands.这个插件促进了一系列命令。 Infinite possibilities!无限可能!

I hope it helps someone!我希望它能帮助别人!

It's in Visual Studio's Preferences.它在 Visual Studio 的首选项中。 Solution from sbi's answer here:来自sbi 的答案的解决方案:

  • Go to Tools/Options/Environment/Keyboard .转到Tools/Options/Environment/Keyboard
  • Switch the "Use new shortcut in:" dropdown to "Text Editor".将“使用新的快捷方式:”下拉菜单切换到“文本编辑器”。
  • Pick the Edit.BreakLine command.选择Edit.BreakLine命令。
  • In the Press shortcut keys edit pane press Shift + Enter .Press shortcut keys编辑窗格中,按Shift + Enter

Worked for me!为我工作!

If you need semicolons on Javascript/Typescript using vscode do:如果您需要使用 vscode 在 Javascript/Typescript 上使用分号,请执行以下操作:

Settings设置

type "semicolon"输入“分号”

you will have options for Javascript & Tyepscript您将有 Javascript 和 Typscript 的选项

select the "insert" option as desired根据需要选择“插入”选项

You can also select semicolons for css, less, or scss您还可以为 css、less 或 scss 选择分号

/*******/ /*******/

You can select the "Show matching extensions" and get some extra help with extensions that address the same issue.您可以选择“显示匹配的扩展程序”并获得一些关于解决相同问题的扩展程序的额外帮助。

I reckon this is an old question, but Ctrl + Shift + Enter does it for me. 我认为这是一个老问题,但Ctrl + Shift + Enter为我做了。 It puts a semicolon at the end of the line even when you're not at the end of the line and sets the cursor to a new line below. 即使您不在行的末尾并将光标设置为下面的新行,它也会在行尾添加分号。 See if it helps you. 看看它是否对你有所帮助。 Chherio! Chherio!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM