{"id":81335,"date":"2017-01-11T00:01:00","date_gmt":"2017-01-11T08:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/?p=81335"},"modified":"2019-02-18T09:10:15","modified_gmt":"2019-02-18T16:10:15","slug":"visual-studio-code-editing-features-for-powershell-development-part-1","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/visual-studio-code-editing-features-for-powershell-development-part-1\/","title":{"rendered":"Visual Studio Code editing features for PowerShell development \u2013 Part 1"},"content":{"rendered":"<p><strong>Summary<\/strong>: The editing features of Visual Studio Code come in handy as you develop your PowerShell scripts.<\/p>\n<p style=\"padding-left: 30px\"><strong>Note<\/strong>: This post includes animated gifs. For best results, view the post in Internet Explorer or Microsoft Edge.<\/p>\n<p>In the <a target=\"_blank\" href=\"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2016\/12\/05\/get-started-with-powershell-development-in-visual-studio-code\/\">previous blog post<\/a>, we explained how to get started with PowerShell development in Visual Studio Code. If you don\u2019t have Visual Studio Code configured with the PowerShell extension, read that blog post first to get caught up. Since that blog post, there have been two important updates. First, Visual Studio Code has been updated to 1.8. One significant new feature in Visual Studio Code 1.8 is <strong>Hot Exit<\/strong>, which remembers unsaved changes to files when you exit. And second, the PowerShell extension has been updated to 0.8.0. If you haven\u2019t updated yet, take a few minutes and update.<\/p>\n<p>In this blog post, we will examine some editing features of Visual Studio Code that will come in handy as you develop your PowerShell scripts. When you want to edit PowerShell scripts, you can open individual files. However, the real power of Visual Studio Code is unleashed when you open a folder that contains your PowerShell scripts. Features like debugging, Git version control, and search\/replace across multiple files are only available when you open a folder for editing.<\/p>\n<h2>Open a folder<\/h2>\n<p>To open a folder, select <strong>Open Folder\u2026 <\/strong>from the <strong>File<\/strong> menu, and then select a folder to open. If Explorer is not visible, you can open it by pressing Ctrl + Shift + E. In the following examples, I have opened the Plaster project\u2019s folder. If you want to follow along, you can get the <a target=\"_blank\" href=\"https:\/\/github.com\/PowerShell\/Plaster\">Plaster source from GitHub<\/a>. After the folder is open, the Explorer view shows all the files in the folder:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1-HSG-101117.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1-HSG-101117-1024x755.png\" alt=\"View of files in the Plaster project\u2019s folder\" width=\"1024\" height=\"755\" class=\"alignnone size-large wp-image-81355\" \/><\/a><\/p>\n<p>If Explorer is not visible, you can open it by pressing Ctrl + Shift + E\u00a0 (Cmd + Shift + E on Mac).<\/p>\n<h2>Quick Open and Preview tab<\/h2>\n<p>From the Explorer view, you can click a file to open it for editing. But, when there are a lot of files or a folder hierarchy, it is often easier to use <strong>Go to File\u2026<\/strong> from the <strong>Go <\/strong>menu. This feature is also referred to as <strong>Quick Open <\/strong>and is more commonly invoked by pressing Ctrl + P\u00a0 (Cmd + P on Mac).<\/p>\n<p>The Quick Open UI lets you type part of a\u00a0file name to find it quickly from anywhere under the folder you opened. Quick Open also supports fuzzy matching as shown in the following screenshot:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2-HSG-101117.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2-HSG-101117-1024x260.png\" alt=\"Type part of a file name to find a file quickly\" width=\"1024\" height=\"260\" class=\"alignnone size-large wp-image-81326\" \/><\/a><\/p>\n<p>By default, Quick Open doesn\u2019t search the .git folder or any files or folders that you might have configured in the \u201cfiles.exclude\u201d setting.<\/p>\n<p>Let\u2019s select a file and press Enter to open it. This will open the file as shown in the following screenshot:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3-HSG-101117.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3-HSG-101117-1024x165.png\" alt=\"Select the InvokePlaster.ps1 file and press Enter to view the file\" width=\"1024\" height=\"165\" class=\"alignnone size-large wp-image-81365\" \/><\/a><\/p>\n<p>Note that the file name in the editor tab is <em>italicized<\/em>. This indicates that the file has been opened in Preview mode. Preview mode lets you to browse files without opening a bunch of tabs that you will need to close later. The tab used by Preview mode is called \u201cpreview tab\u201d.<\/p>\n<p>A file is opened in the preview tab when you click it in the Explorer folder hierarchy. If you double-click the file, it will open in a new, \u201cnormal\u201d tab. Each file that\u2019s opened this way will get its own tab.<\/p>\n<p>If you edit a file in the preview tab, the tab will switch from preview mode to a normal tab. You can also double-click the editor tab to switch from a preview tab to a normal tab.<\/p>\n<h2>File and Window navigation<\/h2>\n<p>If you have multiple editor windows open, you will want a quick way to switch between them. Although you can use the mouse to select an editor window from the Explorer\u2019s <strong>OPEN EDITORS<\/strong> list, it is quicker to use Ctrl + Tab to show the open file list as shown in the following screenshot. This will display a list of open files. Keep pressing Ctrl + Tab to cycle through the list. When you release the Ctrl key, the currently selected file\u2019s editor window will get focus. You can cycle through the list backwards by pressing Ctrl +\u00a0 Shift + Tab.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4-HSG-101117.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4-HSG-101117-1024x171.png\" alt=\"Cycle through a list of files by using the Cntrl + Tab and Cntrl+ Shift + Tab keys\" width=\"1024\" height=\"171\" class=\"alignnone size-large wp-image-81375\" \/><\/a><\/p>\n<p>At other times you will want to switch focus between one of the many views and panels, such as the Explorer, Git, Problems, Debug console, Output and Terminal, instead of between editor windows. This can be done by pressing Ctrl + Q, which opens the <strong>Quick Open View<\/strong> as shown in the following screenshot. Keep pressing Ctrl + Q to cycle through the list. When you release the Ctrl key, the selected view will get focus. You can press Ctrl + Shift + Q to cycle backwards through the list.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5-HSG-101117.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5-HSG-101117-1024x757.png\" alt=\"Quick Open View\" width=\"1024\" height=\"757\" class=\"alignnone size-large wp-image-81385\" \/><\/a><\/p>\n<h2>Search and replace<\/h2>\n<p>In an open file, you can use Ctrl + F to find text or Ctrl + H to replace text in that file.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6-HSG-101117.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6-HSG-101117-1024x320.png\" alt=\"Search and replace in a file\" width=\"1024\" height=\"320\" class=\"alignnone size-large wp-image-81395\" \/><\/a><\/p>\n<p>One benefit of opening a folder is that you can also search and replace across <strong>all<\/strong> your files. To start, click the <strong>Search<\/strong> icon in the <strong>View<\/strong> bar, or press Ctrl + Shift + F (Cmd + Shift + F on Mac). The following example shows a search of mandatory parameters, as highlighted in the following screenshot, across all files in the Plaster repository:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7-HSG-1011171.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7-HSG-1011171.png\" alt=\"Search of mandatory parameters in the Plaster repository\" width=\"372\" height=\"566\" class=\"alignnone size-full wp-image-81755\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>The ellipsis \u201c\u2026\u201d above has been expanded to show all the search options that are available to let you target only the files you want.<\/p>\n<h2>Multi-cursor editing<\/h2>\n<p>A really slick feature of Visual Studio Code, and indeed of many modern editors, is the ability to edit multiple lines at once. In the following animated GIF, I position the cursor where I want to start editing on the first line, and then I use Ctrl + Alt + DownArrow to create additional cursors below that line. When I start to type, every line where I have placed a cursor will be edited:<\/p>\n<p>&nbsp;<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/multi-cursor1.gif\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/multi-cursor1.gif\" alt=\"Multi-cursor editing\" width=\"744\" height=\"208\" class=\"alignnone size-full wp-image-81695\" \/><\/a><\/p>\n<p>Multi-cursor editing doesn\u2019t apply only to text that lines up in the same column on consecutive lines. The next example shows editing the same word in multiple, disjointed places within a function. Here, I select the first instance of the word, Value, and then continue to press Ctrl + D until all instances of \u201cValue\u201d are selected. Then, I type some new text, and all selected instances get edited at the same time.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/multi-cursor2.gif\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/multi-cursor2.gif\" alt=\"Multi-cursor selection\" width=\"744\" height=\"340\" class=\"alignnone size-full wp-image-81725\" \/><\/a><\/p>\n<h2>Expand\/shrink selection<\/h2>\n<p>You can expand the text selection around the current cursor location by pressing Shift + Alt + RightArrow. As you continue to press Shift + Alt + RightArrow, the selection grows. You can shrink the selection by pressing the Shift + Alt + LeftArrow. This is shown in the following example:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/expand-shrink-selection.gif\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/expand-shrink-selection.gif\" alt=\"Expande\/shrink\" width=\"744\" height=\"340\" class=\"alignnone size-full wp-image-81715\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>In this blog post, we looked at some of the native editing features in Visual Studio Code that apply to any file. Come back tomorrow for Part 2 when we look at the language service features of the PowerShell extension.<\/p>\n<p><span>I invite you to follow the Scripting Guys on <\/span><a target=\"_blank\" href=\"http:\/\/bit.ly\/scriptingguystwitter\">Twitter<\/a><span> and <\/span><a href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a><span>. If you have any questions, send email to them at <\/span><a target=\"_blank\" href=\"mailto:scripter@microsoft.com\">scripter@microsoft.com<\/a><span>, or post your questions on the <\/span><a target=\"_blank\" href=\"http:\/\/bit.ly\/scriptingforum\">Official Scripting Guys Forum<\/a><span>.<\/span><\/p>\n<p><strong>Keith Hill\n<\/strong>Software Engineer\nPowerShell MVP<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: The editing features of Visual Studio Code come in handy as you develop your PowerShell scripts. Note: This post includes animated gifs. For best results, view the post in Internet Explorer or Microsoft Edge. In the previous blog post, we explained how to get started with PowerShell development in Visual Studio Code. If you [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[568,641],"tags":[499,153,377,701],"class_list":["post-81335","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-hey-scripting-guy","category-windows-powershell","tag-guestblogger","tag-keith-hill","tag-powershell","tag-visual-studio-code"],"acf":[],"blog_post_summary":"<p>Summary: The editing features of Visual Studio Code come in handy as you develop your PowerShell scripts. Note: This post includes animated gifs. For best results, view the post in Internet Explorer or Microsoft Edge. In the previous blog post, we explained how to get started with PowerShell development in Visual Studio Code. If you [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/81335","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=81335"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/81335\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=81335"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=81335"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=81335"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}