├── .github ├── CODE_OF_CONDUCT.md ├── FUNDING.yml ├── ISSUE_TEMPLATE │ └── feedback.md └── PULL_REQUEST_TEMPLATE.md ├── .gitignore ├── .prettierrc ├── LICENSE ├── README.md ├── docs ├── .vuepress │ ├── config.js │ ├── public │ │ ├── android-chrome-192x192.png │ │ ├── android-chrome-512x512.png │ │ ├── apple-touch-icon.png │ │ ├── banner.png │ │ ├── favicon-16x16.png │ │ ├── favicon-32x32.png │ │ ├── favicon.ico │ │ ├── hero.png │ │ └── site.webmanifest │ └── styles │ │ ├── index.styl │ │ └── palette.styl ├── 1-preparations │ ├── 1-0-intro.md │ └── 1-1-installation.md ├── 2-cli │ ├── 2-0-intro.md │ ├── 2-1-terminal.md │ ├── 2-2-shell.md │ ├── 2-3-cli-tools.md │ └── 2-4-others.md ├── 3-vscode │ ├── 3-0-intro.md │ ├── 3-1-remote-dev.md │ ├── 3-2-code-runner.md │ ├── 3-3-python.md │ ├── 3-4-c_cpp.md │ ├── 3-5-latex.md │ ├── 3-6-nodejs.md │ └── 3-7-dotnetcore.md ├── 4-advanced │ ├── 4-0-intro.md │ ├── 4-1-gui.md │ ├── 4-2-lxrunoffline.md │ ├── 4-3-wslconfig.md │ └── 4-4-usb.md ├── 5-others │ ├── 5-0-intro.md │ └── 5-1-donating.md ├── README.md ├── changelog │ └── README.md └── contributing │ └── README.md ├── package.json └── yarn.lock /.github/CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation. 6 | 7 | ## Our Standards 8 | 9 | Examples of behavior that contributes to creating a positive environment include: 10 | 11 | * Using welcoming and inclusive language 12 | * Being respectful of differing viewpoints and experiences 13 | * Gracefully accepting constructive criticism 14 | * Focusing on what is best for the community 15 | * Showing empathy towards other community members 16 | 17 | Examples of unacceptable behavior by participants include: 18 | 19 | * The use of sexualized language or imagery and unwelcome sexual attention or advances 20 | * Trolling, insulting/derogatory comments, and personal or political attacks 21 | * Public or private harassment 22 | * Publishing others' private information, such as a physical or electronic address, without explicit permission 23 | * Other conduct which could reasonably be considered inappropriate in a professional setting 24 | 25 | ## Our Responsibilities 26 | 27 | Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior. 28 | 29 | Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful. 30 | 31 | ## Scope 32 | 33 | This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers. 34 | 35 | ## Enforcement 36 | 37 | Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at spencerwoo98@gmail.com. The project team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately. 38 | 39 | Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership. 40 | 41 | ## Attribution 42 | 43 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version] 44 | 45 | [homepage]: http://contributor-covenant.org 46 | [version]: http://contributor-covenant.org/version/1/4/ 47 | -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2] 4 | patreon: # Replace with a single Patreon username 5 | open_collective: dowww 6 | custom: ['https://afdian.net/@spencerwoo'] 7 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feedback.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Feedback 3 | about: 遇到问题 / 文档改进 / 新特性建议 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | 11 | 12 | **我在这个页面的配置过程遇到了问题:** 13 | 14 | 15 | 16 | **我觉得文档这里有些问题:** 17 | 18 | 19 | 20 | **我希望文档能够包括这一内容 / 我发现新的内容可以写入文档:** 21 | 22 | 23 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | 🚀 **我这里解决了这些问题:** 2 | 3 | 1. [...] 4 | 2. [...] 5 | 6 | 💡 **我这次对这些地方做出了更改:** 7 | 8 | 1. [...] 9 | 2. [...] 10 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | Thumbs.db 2 | db.json 3 | .vscode/ 4 | docs/.vuepress/dist 5 | 6 | # Logs 7 | logs 8 | *.log 9 | npm-debug.log* 10 | yarn-debug.log* 11 | yarn-error.log* 12 | lerna-debug.log* 13 | 14 | # Diagnostic reports (https://nodejs.org/api/report.html) 15 | report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json 16 | 17 | # Runtime data 18 | pids 19 | *.pid 20 | *.seed 21 | *.pid.lock 22 | 23 | # Directory for instrumented libs generated by jscoverage/JSCover 24 | lib-cov 25 | 26 | # Coverage directory used by tools like istanbul 27 | coverage 28 | *.lcov 29 | 30 | # nyc test coverage 31 | .nyc_output 32 | 33 | # Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) 34 | .grunt 35 | 36 | # Bower dependency directory (https://bower.io/) 37 | bower_components 38 | 39 | # node-waf configuration 40 | .lock-wscript 41 | 42 | # Compiled binary addons (https://nodejs.org/api/addons.html) 43 | build/Release 44 | 45 | # Dependency directories 46 | node_modules/ 47 | jspm_packages/ 48 | 49 | # Snowpack dependency directory (https://snowpack.dev/) 50 | web_modules/ 51 | 52 | # TypeScript cache 53 | *.tsbuildinfo 54 | 55 | # Optional npm cache directory 56 | .npm 57 | 58 | # Optional eslint cache 59 | .eslintcache 60 | 61 | # Microbundle cache 62 | .rpt2_cache/ 63 | .rts2_cache_cjs/ 64 | .rts2_cache_es/ 65 | .rts2_cache_umd/ 66 | 67 | # Optional REPL history 68 | .node_repl_history 69 | 70 | # Output of 'npm pack' 71 | *.tgz 72 | 73 | # Yarn Integrity file 74 | .yarn-integrity 75 | 76 | # dotenv environment variables file 77 | .env 78 | .env.test 79 | 80 | # parcel-bundler cache (https://parceljs.org/) 81 | .cache 82 | .parcel-cache 83 | 84 | # Next.js build output 85 | .next 86 | out 87 | 88 | # Nuxt.js build / generate output 89 | .nuxt 90 | dist 91 | 92 | # Gatsby files 93 | .cache/ 94 | # Comment in the public line in if your project uses Gatsby and not Next.js 95 | # https://nextjs.org/blog/next-9-1#public-directory-support 96 | # public 97 | 98 | # Serverless directories 99 | .serverless/ 100 | 101 | # FuseBox cache 102 | .fusebox/ 103 | 104 | # DynamoDB Local files 105 | .dynamodb/ 106 | 107 | # TernJS port file 108 | .tern-port 109 | 110 | # Stores VSCode versions used for testing VSCode extensions 111 | .vscode-test 112 | 113 | # yarn v2 114 | .yarn/cache 115 | .yarn/unplugged 116 | .yarn/build-state.yml 117 | .yarn/install-state.gz 118 | .pnp.* 119 | 120 | # General 121 | .DS_Store 122 | .AppleDouble 123 | .LSOverride 124 | 125 | # Icon must end with two \r 126 | Icon 127 | 128 | 129 | # Thumbnails 130 | ._* 131 | 132 | # Files that might appear in the root of a volume 133 | .DocumentRevisions-V100 134 | .fseventsd 135 | .Spotlight-V100 136 | .TemporaryItems 137 | .Trashes 138 | .VolumeIcon.icns 139 | .com.apple.timemachine.donotpresent 140 | 141 | # Directories potentially created on remote AFP share 142 | .AppleDB 143 | .AppleDesktop 144 | Network Trash Folder 145 | Temporary Items 146 | .apdisk 147 | -------------------------------------------------------------------------------- /.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "tabWidth": 2, 3 | "useTabs": false, 4 | "trailingComma": "es5", 5 | "semi": false, 6 | "singleQuote": true 7 | } 8 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Attribution-ShareAlike 4.0 International 2 | 3 | ======================================================================= 4 | 5 | Creative Commons Corporation ("Creative Commons") is not a law firm and 6 | does not provide legal services or legal advice. Distribution of 7 | Creative Commons public licenses does not create a lawyer-client or 8 | other relationship. Creative Commons makes its licenses and related 9 | information available on an "as-is" basis. Creative Commons gives no 10 | warranties regarding its licenses, any material licensed under their 11 | terms and conditions, or any related information. Creative Commons 12 | disclaims all liability for damages resulting from their use to the 13 | fullest extent possible. 14 | 15 | Using Creative Commons Public Licenses 16 | 17 | Creative Commons public licenses provide a standard set of terms and 18 | conditions that creators and other rights holders may use to share 19 | original works of authorship and other material subject to copyright 20 | and certain other rights specified in the public license below. The 21 | following considerations are for informational purposes only, are not 22 | exhaustive, and do not form part of our licenses. 23 | 24 | Considerations for licensors: Our public licenses are 25 | intended for use by those authorized to give the public 26 | permission to use material in ways otherwise restricted by 27 | copyright and certain other rights. Our licenses are 28 | irrevocable. Licensors should read and understand the terms 29 | and conditions of the license they choose before applying it. 30 | Licensors should also secure all rights necessary before 31 | applying our licenses so that the public can reuse the 32 | material as expected. Licensors should clearly mark any 33 | material not subject to the license. This includes other CC- 34 | licensed material, or material used under an exception or 35 | limitation to copyright. More considerations for licensors: 36 | wiki.creativecommons.org/Considerations_for_licensors 37 | 38 | Considerations for the public: By using one of our public 39 | licenses, a licensor grants the public permission to use the 40 | licensed material under specified terms and conditions. If 41 | the licensor's permission is not necessary for any reason--for 42 | example, because of any applicable exception or limitation to 43 | copyright--then that use is not regulated by the license. Our 44 | licenses grant only permissions under copyright and certain 45 | other rights that a licensor has authority to grant. Use of 46 | the licensed material may still be restricted for other 47 | reasons, including because others have copyright or other 48 | rights in the material. A licensor may make special requests, 49 | such as asking that all changes be marked or described. 50 | Although not required by our licenses, you are encouraged to 51 | respect those requests where reasonable. More_considerations 52 | for the public: 53 | wiki.creativecommons.org/Considerations_for_licensees 54 | 55 | ======================================================================= 56 | 57 | Creative Commons Attribution-ShareAlike 4.0 International Public 58 | License 59 | 60 | By exercising the Licensed Rights (defined below), You accept and agree 61 | to be bound by the terms and conditions of this Creative Commons 62 | Attribution-ShareAlike 4.0 International Public License ("Public 63 | License"). To the extent this Public License may be interpreted as a 64 | contract, You are granted the Licensed Rights in consideration of Your 65 | acceptance of these terms and conditions, and the Licensor grants You 66 | such rights in consideration of benefits the Licensor receives from 67 | making the Licensed Material available under these terms and 68 | conditions. 69 | 70 | 71 | Section 1 -- Definitions. 72 | 73 | a. Adapted Material means material subject to Copyright and Similar 74 | Rights that is derived from or based upon the Licensed Material 75 | and in which the Licensed Material is translated, altered, 76 | arranged, transformed, or otherwise modified in a manner requiring 77 | permission under the Copyright and Similar Rights held by the 78 | Licensor. For purposes of this Public License, where the Licensed 79 | Material is a musical work, performance, or sound recording, 80 | Adapted Material is always produced where the Licensed Material is 81 | synched in timed relation with a moving image. 82 | 83 | b. Adapter's License means the license You apply to Your Copyright 84 | and Similar Rights in Your contributions to Adapted Material in 85 | accordance with the terms and conditions of this Public License. 86 | 87 | c. BY-SA Compatible License means a license listed at 88 | creativecommons.org/compatiblelicenses, approved by Creative 89 | Commons as essentially the equivalent of this Public License. 90 | 91 | d. Copyright and Similar Rights means copyright and/or similar rights 92 | closely related to copyright including, without limitation, 93 | performance, broadcast, sound recording, and Sui Generis Database 94 | Rights, without regard to how the rights are labeled or 95 | categorized. For purposes of this Public License, the rights 96 | specified in Section 2(b)(1)-(2) are not Copyright and Similar 97 | Rights. 98 | 99 | e. Effective Technological Measures means those measures that, in the 100 | absence of proper authority, may not be circumvented under laws 101 | fulfilling obligations under Article 11 of the WIPO Copyright 102 | Treaty adopted on December 20, 1996, and/or similar international 103 | agreements. 104 | 105 | f. Exceptions and Limitations means fair use, fair dealing, and/or 106 | any other exception or limitation to Copyright and Similar Rights 107 | that applies to Your use of the Licensed Material. 108 | 109 | g. License Elements means the license attributes listed in the name 110 | of a Creative Commons Public License. The License Elements of this 111 | Public License are Attribution and ShareAlike. 112 | 113 | h. Licensed Material means the artistic or literary work, database, 114 | or other material to which the Licensor applied this Public 115 | License. 116 | 117 | i. Licensed Rights means the rights granted to You subject to the 118 | terms and conditions of this Public License, which are limited to 119 | all Copyright and Similar Rights that apply to Your use of the 120 | Licensed Material and that the Licensor has authority to license. 121 | 122 | j. Licensor means the individual(s) or entity(ies) granting rights 123 | under this Public License. 124 | 125 | k. Share means to provide material to the public by any means or 126 | process that requires permission under the Licensed Rights, such 127 | as reproduction, public display, public performance, distribution, 128 | dissemination, communication, or importation, and to make material 129 | available to the public including in ways that members of the 130 | public may access the material from a place and at a time 131 | individually chosen by them. 132 | 133 | l. Sui Generis Database Rights means rights other than copyright 134 | resulting from Directive 96/9/EC of the European Parliament and of 135 | the Council of 11 March 1996 on the legal protection of databases, 136 | as amended and/or succeeded, as well as other essentially 137 | equivalent rights anywhere in the world. 138 | 139 | m. You means the individual or entity exercising the Licensed Rights 140 | under this Public License. Your has a corresponding meaning. 141 | 142 | 143 | Section 2 -- Scope. 144 | 145 | a. License grant. 146 | 147 | 1. Subject to the terms and conditions of this Public License, 148 | the Licensor hereby grants You a worldwide, royalty-free, 149 | non-sublicensable, non-exclusive, irrevocable license to 150 | exercise the Licensed Rights in the Licensed Material to: 151 | 152 | a. reproduce and Share the Licensed Material, in whole or 153 | in part; and 154 | 155 | b. produce, reproduce, and Share Adapted Material. 156 | 157 | 2. Exceptions and Limitations. For the avoidance of doubt, where 158 | Exceptions and Limitations apply to Your use, this Public 159 | License does not apply, and You do not need to comply with 160 | its terms and conditions. 161 | 162 | 3. Term. The term of this Public License is specified in Section 163 | 6(a). 164 | 165 | 4. Media and formats; technical modifications allowed. The 166 | Licensor authorizes You to exercise the Licensed Rights in 167 | all media and formats whether now known or hereafter created, 168 | and to make technical modifications necessary to do so. The 169 | Licensor waives and/or agrees not to assert any right or 170 | authority to forbid You from making technical modifications 171 | necessary to exercise the Licensed Rights, including 172 | technical modifications necessary to circumvent Effective 173 | Technological Measures. For purposes of this Public License, 174 | simply making modifications authorized by this Section 2(a) 175 | (4) never produces Adapted Material. 176 | 177 | 5. Downstream recipients. 178 | 179 | a. Offer from the Licensor -- Licensed Material. Every 180 | recipient of the Licensed Material automatically 181 | receives an offer from the Licensor to exercise the 182 | Licensed Rights under the terms and conditions of this 183 | Public License. 184 | 185 | b. Additional offer from the Licensor -- Adapted Material. 186 | Every recipient of Adapted Material from You 187 | automatically receives an offer from the Licensor to 188 | exercise the Licensed Rights in the Adapted Material 189 | under the conditions of the Adapter's License You apply. 190 | 191 | c. No downstream restrictions. You may not offer or impose 192 | any additional or different terms or conditions on, or 193 | apply any Effective Technological Measures to, the 194 | Licensed Material if doing so restricts exercise of the 195 | Licensed Rights by any recipient of the Licensed 196 | Material. 197 | 198 | 6. No endorsement. Nothing in this Public License constitutes or 199 | may be construed as permission to assert or imply that You 200 | are, or that Your use of the Licensed Material is, connected 201 | with, or sponsored, endorsed, or granted official status by, 202 | the Licensor or others designated to receive attribution as 203 | provided in Section 3(a)(1)(A)(i). 204 | 205 | b. Other rights. 206 | 207 | 1. Moral rights, such as the right of integrity, are not 208 | licensed under this Public License, nor are publicity, 209 | privacy, and/or other similar personality rights; however, to 210 | the extent possible, the Licensor waives and/or agrees not to 211 | assert any such rights held by the Licensor to the limited 212 | extent necessary to allow You to exercise the Licensed 213 | Rights, but not otherwise. 214 | 215 | 2. Patent and trademark rights are not licensed under this 216 | Public License. 217 | 218 | 3. To the extent possible, the Licensor waives any right to 219 | collect royalties from You for the exercise of the Licensed 220 | Rights, whether directly or through a collecting society 221 | under any voluntary or waivable statutory or compulsory 222 | licensing scheme. In all other cases the Licensor expressly 223 | reserves any right to collect such royalties. 224 | 225 | 226 | Section 3 -- License Conditions. 227 | 228 | Your exercise of the Licensed Rights is expressly made subject to the 229 | following conditions. 230 | 231 | a. Attribution. 232 | 233 | 1. If You Share the Licensed Material (including in modified 234 | form), You must: 235 | 236 | a. retain the following if it is supplied by the Licensor 237 | with the Licensed Material: 238 | 239 | i. identification of the creator(s) of the Licensed 240 | Material and any others designated to receive 241 | attribution, in any reasonable manner requested by 242 | the Licensor (including by pseudonym if 243 | designated); 244 | 245 | ii. a copyright notice; 246 | 247 | iii. a notice that refers to this Public License; 248 | 249 | iv. a notice that refers to the disclaimer of 250 | warranties; 251 | 252 | v. a URI or hyperlink to the Licensed Material to the 253 | extent reasonably practicable; 254 | 255 | b. indicate if You modified the Licensed Material and 256 | retain an indication of any previous modifications; and 257 | 258 | c. indicate the Licensed Material is licensed under this 259 | Public License, and include the text of, or the URI or 260 | hyperlink to, this Public License. 261 | 262 | 2. You may satisfy the conditions in Section 3(a)(1) in any 263 | reasonable manner based on the medium, means, and context in 264 | which You Share the Licensed Material. For example, it may be 265 | reasonable to satisfy the conditions by providing a URI or 266 | hyperlink to a resource that includes the required 267 | information. 268 | 269 | 3. If requested by the Licensor, You must remove any of the 270 | information required by Section 3(a)(1)(A) to the extent 271 | reasonably practicable. 272 | 273 | b. ShareAlike. 274 | 275 | In addition to the conditions in Section 3(a), if You Share 276 | Adapted Material You produce, the following conditions also apply. 277 | 278 | 1. The Adapter's License You apply must be a Creative Commons 279 | license with the same License Elements, this version or 280 | later, or a BY-SA Compatible License. 281 | 282 | 2. You must include the text of, or the URI or hyperlink to, the 283 | Adapter's License You apply. You may satisfy this condition 284 | in any reasonable manner based on the medium, means, and 285 | context in which You Share Adapted Material. 286 | 287 | 3. You may not offer or impose any additional or different terms 288 | or conditions on, or apply any Effective Technological 289 | Measures to, Adapted Material that restrict exercise of the 290 | rights granted under the Adapter's License You apply. 291 | 292 | 293 | Section 4 -- Sui Generis Database Rights. 294 | 295 | Where the Licensed Rights include Sui Generis Database Rights that 296 | apply to Your use of the Licensed Material: 297 | 298 | a. for the avoidance of doubt, Section 2(a)(1) grants You the right 299 | to extract, reuse, reproduce, and Share all or a substantial 300 | portion of the contents of the database; 301 | 302 | b. if You include all or a substantial portion of the database 303 | contents in a database in which You have Sui Generis Database 304 | Rights, then the database in which You have Sui Generis Database 305 | Rights (but not its individual contents) is Adapted Material, 306 | 307 | including for purposes of Section 3(b); and 308 | c. You must comply with the conditions in Section 3(a) if You Share 309 | all or a substantial portion of the contents of the database. 310 | 311 | For the avoidance of doubt, this Section 4 supplements and does not 312 | replace Your obligations under this Public License where the Licensed 313 | Rights include other Copyright and Similar Rights. 314 | 315 | 316 | Section 5 -- Disclaimer of Warranties and Limitation of Liability. 317 | 318 | a. UNLESS OTHERWISE SEPARATELY UNDERTAKEN BY THE LICENSOR, TO THE 319 | EXTENT POSSIBLE, THE LICENSOR OFFERS THE LICENSED MATERIAL AS-IS 320 | AND AS-AVAILABLE, AND MAKES NO REPRESENTATIONS OR WARRANTIES OF 321 | ANY KIND CONCERNING THE LICENSED MATERIAL, WHETHER EXPRESS, 322 | IMPLIED, STATUTORY, OR OTHER. THIS INCLUDES, WITHOUT LIMITATION, 323 | WARRANTIES OF TITLE, MERCHANTABILITY, FITNESS FOR A PARTICULAR 324 | PURPOSE, NON-INFRINGEMENT, ABSENCE OF LATENT OR OTHER DEFECTS, 325 | ACCURACY, OR THE PRESENCE OR ABSENCE OF ERRORS, WHETHER OR NOT 326 | KNOWN OR DISCOVERABLE. WHERE DISCLAIMERS OF WARRANTIES ARE NOT 327 | ALLOWED IN FULL OR IN PART, THIS DISCLAIMER MAY NOT APPLY TO YOU. 328 | 329 | b. TO THE EXTENT POSSIBLE, IN NO EVENT WILL THE LICENSOR BE LIABLE 330 | TO YOU ON ANY LEGAL THEORY (INCLUDING, WITHOUT LIMITATION, 331 | NEGLIGENCE) OR OTHERWISE FOR ANY DIRECT, SPECIAL, INDIRECT, 332 | INCIDENTAL, CONSEQUENTIAL, PUNITIVE, EXEMPLARY, OR OTHER LOSSES, 333 | COSTS, EXPENSES, OR DAMAGES ARISING OUT OF THIS PUBLIC LICENSE OR 334 | USE OF THE LICENSED MATERIAL, EVEN IF THE LICENSOR HAS BEEN 335 | ADVISED OF THE POSSIBILITY OF SUCH LOSSES, COSTS, EXPENSES, OR 336 | DAMAGES. WHERE A LIMITATION OF LIABILITY IS NOT ALLOWED IN FULL OR 337 | IN PART, THIS LIMITATION MAY NOT APPLY TO YOU. 338 | 339 | c. The disclaimer of warranties and limitation of liability provided 340 | above shall be interpreted in a manner that, to the extent 341 | possible, most closely approximates an absolute disclaimer and 342 | waiver of all liability. 343 | 344 | 345 | Section 6 -- Term and Termination. 346 | 347 | a. This Public License applies for the term of the Copyright and 348 | Similar Rights licensed here. However, if You fail to comply with 349 | this Public License, then Your rights under this Public License 350 | terminate automatically. 351 | 352 | b. Where Your right to use the Licensed Material has terminated under 353 | Section 6(a), it reinstates: 354 | 355 | 1. automatically as of the date the violation is cured, provided 356 | it is cured within 30 days of Your discovery of the 357 | violation; or 358 | 359 | 2. upon express reinstatement by the Licensor. 360 | 361 | For the avoidance of doubt, this Section 6(b) does not affect any 362 | right the Licensor may have to seek remedies for Your violations 363 | of this Public License. 364 | 365 | c. For the avoidance of doubt, the Licensor may also offer the 366 | Licensed Material under separate terms or conditions or stop 367 | distributing the Licensed Material at any time; however, doing so 368 | will not terminate this Public License. 369 | 370 | d. Sections 1, 5, 6, 7, and 8 survive termination of this Public 371 | License. 372 | 373 | 374 | Section 7 -- Other Terms and Conditions. 375 | 376 | a. The Licensor shall not be bound by any additional or different 377 | terms or conditions communicated by You unless expressly agreed. 378 | 379 | b. Any arrangements, understandings, or agreements regarding the 380 | Licensed Material not stated herein are separate from and 381 | independent of the terms and conditions of this Public License. 382 | 383 | 384 | Section 8 -- Interpretation. 385 | 386 | a. For the avoidance of doubt, this Public License does not, and 387 | shall not be interpreted to, reduce, limit, restrict, or impose 388 | conditions on any use of the Licensed Material that could lawfully 389 | be made without permission under this Public License. 390 | 391 | b. To the extent possible, if any provision of this Public License is 392 | deemed unenforceable, it shall be automatically reformed to the 393 | minimum extent necessary to make it enforceable. If the provision 394 | cannot be reformed, it shall be severed from this Public License 395 | without affecting the enforceability of the remaining terms and 396 | conditions. 397 | 398 | c. No term or condition of this Public License will be waived and no 399 | failure to comply consented to unless expressly agreed to by the 400 | Licensor. 401 | 402 | d. Nothing in this Public License constitutes or may be interpreted 403 | as a limitation upon, or waiver of, any privileges and immunities 404 | that apply to the Licensor or You, including from the legal 405 | processes of any jurisdiction or authority. 406 | 407 | 408 | ======================================================================= 409 | 410 | Creative Commons is not a party to its public 411 | licenses. Notwithstanding, Creative Commons may elect to apply one of 412 | its public licenses to material it publishes and in those instances 413 | will be considered the “Licensor.” The text of the Creative Commons 414 | public licenses is dedicated to the public domain under the CC0 Public 415 | Domain Dedication. Except for the limited purpose of indicating that 416 | material is shared under a Creative Commons public license or as 417 | otherwise permitted by the Creative Commons policies published at 418 | creativecommons.org/policies, Creative Commons does not authorize the 419 | use of the trademark "Creative Commons" or any other trademark or logo 420 | of Creative Commons without its prior written consent including, 421 | without limitation, in connection with any unauthorized modifications 422 | to any of its public licenses or any other arrangements, 423 | understandings, or agreements concerning use of licensed material. For 424 | the avoidance of doubt, this paragraph does not form part of the 425 | public licenses. 426 | 427 | Creative Commons may be contacted at creativecommons.org. 428 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |
2 | icon 3 | 4 |

Dev on Windows with WSL

5 | 6 |

在 Windows 上面用 WSL 优雅开发

7 | 8 | [![GitHub contributors](https://img.shields.io/github/contributors/spencerwooo/dowww?color=2db74d&logo=github&labelColor=212c42)](#contributing-贡献) 9 | [![Open Collective backers and sponsors](https://img.shields.io/opencollective/all/dowww?logo=open-collective&color=2db74d&logoColor=ffffff&labelColor=212c42)](#sponsoring-赞助) 10 | [![Windows](https://img.shields.io/badge/windows-%E2%9D%A4%20linux-212c42?logo=windows&labelColor=0078d6)](https://cloudblogs.microsoft.com/windowsserver/2015/05/06/microsoft-loves-linux/) 11 | [![License: CC BY-SA 4.0](https://img.shields.io/github/license/spencerwooo/dowww?color=265ca2&labelColor=212c42)](http://creativecommons.org/licenses/by-sa/4.0/) 12 | [![GitHub stars](https://img.shields.io/github/stars/spencerwooo/dowww?style=social)](https://github.com/spencerwooo/dowww) 13 |
14 | 15 |

目录

16 | 17 | - [前言](#前言) 18 | - [贡献](#贡献) 19 | - [贡献者](#贡献者) 20 | - [贡献规范](#贡献规范) 21 | - [赞助](#赞助) 22 | - [Star](#star) 23 | - [微信、支付宝](#微信支付宝) 24 | - [爱发电](#爱发电) 25 | - [Open Collective](#open-collective) 26 | - [许可](#许可) 27 | 28 | ## 前言 29 | 30 | 首先达成一个共识:Windows 给编程初学者带来了很大的困难。比如缺乏好用的包管理系统、终端环境难看难用和环境变量不易配置等等,这些都让 Windows 在开发体验上难以匹敌 Linux 甚至 macOS。 31 | 32 | WSL 的出现似乎缓解了这些烦恼。WSL —— Windows Subsystem for Linux,即适用于 Linux 的 Windows 子系统。在 2019 年的夏天,微软官方推出了 [WSL 2:基于最新虚拟化技术的 WSL 引擎](https://docs.microsoft.com/en-us/windows/wsl/wsl2-about)。强大的 WSL 2 直接将一个 Linux 内核放入 WSL 架构中,使得 Linux 子系统的 I/O 效率急速提升,也让 Linux 子系统能真正执行「全部 Linux 原生的系统调用」。无论是 WSL 还是 WSL 2,我们都可以借助之来给我们的 Windows 配置一个美观可用的学习编程的开发环境,包括: 33 | 34 | - 原汁原味 Unix 风格的终端环境和开发环境 35 | - 一行命令管理所有软件包的 APT 包管理工具(Debian's Advanced Packaging Tool) 36 | - 在 Visual Studio Code 中直接编写、开发、调试你的项目 37 | 38 | 如果你对这些内容感兴趣,那么请[直接进入文档](https://dowww.spencerwoo.com/1-preparations/1-0-intro),放飞自我,尽情折腾。ヾ(•ω•`)o 39 | 40 | ## 贡献 41 | 42 | ### 贡献者 43 | 44 | 感谢参与这个项目的所有贡献者。This project exists thanks to all the people who contribute. 45 | 46 | 47 | 48 | ### 贡献规范 49 | 50 | 欢迎任何有经验的同学前来帮我共同完善本项目,鞠躬。在你着手准备给这个项目提交一些新内容前,请务必阅读「贡献」相关内容说明与规范:[贡献指南](https://dowww.spencerwoo.com/contributing/)。 51 | 52 | ## 赞助 53 | 54 | ### Star 55 | 56 | 如果你觉得本文档干货满满,对你使用 WSL 有着巨大的帮助,请帮我[在 GitHub 上](https://github.com/spencerwooo/dowww)为此仓库点个 Star!感谢 (。・∀・)ノ゙ 57 | 58 | ### 微信、支付宝 59 | 60 | 对于中国的同学,欢迎直接通过微信和支付宝对我本人进行投喂! 61 | 62 | | 微信支付 | 支付宝支付 | 63 | | :--------------------------------------------------------------------------------------: | :------------------------------------------------------------------------------------------: | 64 | | 微信支付 | 支付宝支付 | 65 | 66 | ### 爱发电 67 | 68 | 各位也可以利用「爱发电」来对我进行支持! 69 | 70 | [![爱发电](https://img.shields.io/badge/%E7%88%B1%E5%8F%91%E7%94%B5-@SpencerWoo-946ce6?labelColor=24292e&style=for-the-badge)](https://afdian.net/@spencerwoo) 71 | 72 | ### Open Collective 73 | 74 | Thank you for your interest! Dev on Windows with WSL is a joint effort. We currently support funding via Open Collective. 75 | 76 | | Backers | Sponsors | 77 | | :-------------------------------------------------------------------------------------------------------------------------------------: | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------: | 78 | | Support this project by becoming a backer. Thank you to all our backers! 🙏 [[Become a backer](https://opencollective.com/dowww#backer)] | Support this project by becoming a sponsor. Your logo will show up here with a link to your website. 🌏 [[Become a sponsor](https://opencollective.com/dowww#sponsor)] | 79 | | | | 80 | 81 | ## 许可 82 | 83 | 本作品采用[知识共享署名-相同方式共享 4.0 国际许可协议](https://creativecommons.org/licenses/by-sa/4.0/)进行许可。 84 | 85 | 本著作係採用[創用 CC 姓名標示-相同方式分享 4.0 國際 授權條款](https://creativecommons.org/licenses/by-sa/4.0/)授權. 86 | 87 | This work is licensed under a [Creative Commons Attribution-ShareAlike 4.0 International License](http://creativecommons.org/licenses/by-sa/4.0/). 88 | 89 | 90 | --- 91 | 92 | 📟 **Dev on Windows with WSL** ©Spencer Woo. Released under the CC BY-SA 4.0 International License. 93 | 94 | Authored and maintained by Spencer Woo. 95 | 96 | [@Portfolio](https://spencerwoo.com/) · [@Blog](https://blog.spencerwoo.com/) · [@GitHub](https://github.com/spencerwooo) 97 | -------------------------------------------------------------------------------- /docs/.vuepress/config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | title: 'Dev on Windows with WSL', 3 | description: '在 Windows 上用 WSL 优雅开发', 4 | head: [ 5 | [ 6 | 'link', 7 | { 8 | rel: 'icon', 9 | type: 'image/png', 10 | sizes: '32x32', 11 | href: '/favicon-32x32.png', 12 | }, 13 | ], 14 | [ 15 | 'link', 16 | { 17 | rel: 'icon', 18 | type: 'image/png', 19 | sizes: '16x16', 20 | href: '/favicon-16x16.png', 21 | }, 22 | ], 23 | [ 24 | 'link', 25 | { 26 | rel: 'apple-touch-icon', 27 | sizes: '180x180', 28 | href: '/apple-touch-icon.png', 29 | }, 30 | ], 31 | ['link', { rel: 'manifest', href: '/site.webmanifest' }], 32 | ], 33 | theme: 'book', 34 | themeConfig: { 35 | repo: 'spencerwooo/dowww', 36 | docsDir: 'docs/', 37 | nav: [ 38 | { 39 | text: '文档首页', 40 | link: '/1-preparations/1-0-intro', 41 | }, 42 | { 43 | text: '捐赠', 44 | link: '/5-others/5-1-donating', 45 | }, 46 | { 47 | text: '贡献指南', 48 | link: '/contributing/', 49 | }, 50 | { 51 | text: '更新日志', 52 | link: '/changelog/', 53 | }, 54 | 55 | { 56 | text: '联系', 57 | items: [ 58 | { 59 | text: 'GitHub', 60 | items: [ 61 | { 62 | text: '反馈提问', 63 | link: 'https://github.com/spencerwooo/dowww/issues/new/choose', 64 | }, 65 | ], 66 | }, 67 | { 68 | text: '关于作者', 69 | items: [ 70 | { text: '个人主页', link: 'https://spencerwoo.com' }, 71 | { text: 'Telegram 频道', link: 'https://t.me/realSpencerWoo' }, 72 | ], 73 | }, 74 | ], 75 | }, 76 | ], 77 | algolia: { 78 | apiKey: '6534c856e645507d6cf52052fdfd5611', 79 | indexName: 'dowww', 80 | }, 81 | searchPlaceholder: '搜索', 82 | sidebar: [ 83 | { 84 | title: 'Preparations 准备工作', 85 | collapsable: false, 86 | children: [ 87 | '1-preparations/1-0-intro', 88 | '1-preparations/1-1-installation', 89 | ], 90 | }, 91 | { 92 | title: 'CLI 命令行环境', 93 | collapsable: false, 94 | children: [ 95 | '2-cli/2-0-intro', 96 | '2-cli/2-1-terminal', 97 | '2-cli/2-2-shell', 98 | '2-cli/2-3-cli-tools', 99 | '2-cli/2-4-others', 100 | ], 101 | }, 102 | { 103 | title: 'Visual Studio Code', 104 | collapsable: false, 105 | children: [ 106 | '3-vscode/3-0-intro', 107 | '3-vscode/3-1-remote-dev', 108 | '3-vscode/3-2-code-runner', 109 | '3-vscode/3-3-python', 110 | '3-vscode/3-4-c_cpp', 111 | '3-vscode/3-5-latex', 112 | '3-vscode/3-6-nodejs', 113 | '3-vscode/3-7-dotnetcore', 114 | ], 115 | }, 116 | { 117 | title: 'Advanced 附加内容', 118 | collapsable: false, 119 | children: [ 120 | '4-advanced/4-0-intro', 121 | '4-advanced/4-1-gui', 122 | '4-advanced/4-2-lxrunoffline', 123 | '4-advanced/4-3-wslconfig', 124 | '4-advanced/4-4-usb', 125 | ], 126 | }, 127 | { 128 | title: 'Experience 体验与参考', 129 | collapsable: false, 130 | children: ['5-others/5-0-intro', '5-others/5-1-donating'], 131 | }, 132 | ], 133 | lastUpdated: 'Last updated', 134 | logo: '/hero.png', 135 | smoothScroll: true, 136 | }, 137 | plugins: [ 138 | ['@vuepress/medium-zoom'], 139 | [ 140 | 'vuepress-plugin-container', 141 | { 142 | type: 'callout', 143 | before: info => `

${info}

`, 144 | after: '
', 145 | }, 146 | ], 147 | [ 148 | 'vuepress-plugin-container', 149 | { 150 | type: 'tree', 151 | before: `
`,
152 |         after: `
`, 153 | }, 154 | ], 155 | ['@vuepress/back-to-top'], 156 | [ 157 | '@vuepress/google-analytics', 158 | { 159 | ga: 'UA-111664763-3', 160 | }, 161 | ], 162 | ], 163 | markdown: { 164 | extendMarkdown: md => { 165 | md.use(require('markdown-it-footnote')) 166 | }, 167 | }, 168 | } 169 | -------------------------------------------------------------------------------- /docs/.vuepress/public/android-chrome-192x192.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/spencerwooo/dowww/568372210d9b91c86e2b71a2b62900403cdaff64/docs/.vuepress/public/android-chrome-192x192.png -------------------------------------------------------------------------------- /docs/.vuepress/public/android-chrome-512x512.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/spencerwooo/dowww/568372210d9b91c86e2b71a2b62900403cdaff64/docs/.vuepress/public/android-chrome-512x512.png -------------------------------------------------------------------------------- /docs/.vuepress/public/apple-touch-icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/spencerwooo/dowww/568372210d9b91c86e2b71a2b62900403cdaff64/docs/.vuepress/public/apple-touch-icon.png -------------------------------------------------------------------------------- /docs/.vuepress/public/banner.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/spencerwooo/dowww/568372210d9b91c86e2b71a2b62900403cdaff64/docs/.vuepress/public/banner.png -------------------------------------------------------------------------------- /docs/.vuepress/public/favicon-16x16.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/spencerwooo/dowww/568372210d9b91c86e2b71a2b62900403cdaff64/docs/.vuepress/public/favicon-16x16.png -------------------------------------------------------------------------------- /docs/.vuepress/public/favicon-32x32.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/spencerwooo/dowww/568372210d9b91c86e2b71a2b62900403cdaff64/docs/.vuepress/public/favicon-32x32.png -------------------------------------------------------------------------------- /docs/.vuepress/public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/spencerwooo/dowww/568372210d9b91c86e2b71a2b62900403cdaff64/docs/.vuepress/public/favicon.ico -------------------------------------------------------------------------------- /docs/.vuepress/public/hero.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/spencerwooo/dowww/568372210d9b91c86e2b71a2b62900403cdaff64/docs/.vuepress/public/hero.png -------------------------------------------------------------------------------- /docs/.vuepress/public/site.webmanifest: -------------------------------------------------------------------------------- 1 | {"name":"","short_name":"","icons":[{"src":"/android-chrome-192x192.png","sizes":"192x192","type":"image/png"},{"src":"/android-chrome-512x512.png","sizes":"512x512","type":"image/png"}],"theme_color":"#ffffff","background_color":"#ffffff","display":"standalone"} -------------------------------------------------------------------------------- /docs/.vuepress/styles/index.styl: -------------------------------------------------------------------------------- 1 | @import url('https://fonts.googleapis.com/css2?family=Lato:wght@400;700&family=Open+Sans:wght@400;700&display=swap'); 2 | 3 | // GENERAL 4 | body 5 | font-family $secondaryFont 6 | 7 | h1, h2, h3, h4, h5, h6 8 | font-family $primaryFont !important 9 | font-weight bold 10 | 11 | .callout 12 | margin 1rem 0 13 | padding 0.1rem 1.5rem 14 | border-radius 0.4rem 15 | background-color #255b9f09 16 | 17 | .title 18 | font-weight bold 19 | 20 | pre.tree 21 | border-left-width 0.5rem 22 | border-left-style solid 23 | border-color $accentColor !important 24 | border-radius 0 25 | 26 | & > code 27 | font-size 14px !important 28 | 29 | & > p 30 | margin -5px 0 -20px 0 31 | 32 | code 33 | background-color $accentColor !important 34 | color #ffffff 35 | 36 | padding 3px 5px 37 | border-radius 3px 38 | 39 | em 40 | color #808080 41 | font-weight light 42 | 43 | .hero 44 | img 45 | width 240px 46 | height auto 47 | 48 | h1 49 | border none 50 | padding-bottom 0 51 | 52 | #search-form 53 | line-height inherit 54 | 55 | .sidebar-links a.sidebar-link:hover 56 | background-color #e6ecf1 !important 57 | 58 | // SCROLLBAR 59 | @media (min-width: 719px) 60 | ::-webkit-scrollbar 61 | max-width 6px 62 | max-height 12px 63 | ::-webkit-scrollbar-thumb 64 | background-color rgba(255, 255, 255, 0.4) 65 | border-radius 6px 66 | -------------------------------------------------------------------------------- /docs/.vuepress/styles/palette.styl: -------------------------------------------------------------------------------- 1 | $accentColor = #297abd 2 | $badgeTipColor = #297abd 3 | $primaryFont = 'Open Sans', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Fira Sans', 'Droid Sans', 'Helvetica Neue', 'Noto Sans CJK SC', 'Source Han Sans SC', 'Microsoft Yahei', sans-serif 4 | $secondaryFont = 'Lato', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Fira Sans', 'Droid Sans', 'Helvetica Neue', 'Noto Sans CJK SC', 'Source Han Sans SC', 'Microsoft Yahei', sans-serif 5 | -------------------------------------------------------------------------------- /docs/1-preparations/1-0-intro.md: -------------------------------------------------------------------------------- 1 | # 序论 2 | 3 | ![](https://cdn.spencer.felinae98.cn/github/2020/09/200902_220309.png) 4 | 5 | ::: callout 🍳 本章内容 6 | 欢迎来到 **Dev on Windows with WSL —— 可能是市面上最详尽的中文 WSL 开发环境配置指南** 的文档现场,本章我们将对 WSL 本身、WSL 近期更新和 WSL 的优越特性进行简单介绍,带领你熟悉利用 WSL 在 Windows 上面开发学习的基础知识。 7 | ::: 8 | 9 | ## 什么是 WSL 10 | 11 | WSL 的全称叫做:Windows Subsystem for Linux,即「适用于 Linux 的 Windows 子系统」。WSL 的诞生让 Windows 用户(开发人员)按原样运行 GNU/Linux 环境 —— 包括大多数命令行工具、实用工具和应用程序 —— 且不会产生虚拟机开销。 12 | 13 | ## 用 WSL 可以做什么? 14 | 15 | > 什么鬼?你上面那一大堆东西说的都是啥?能讲中文吗?(╬▔ 皿 ▔)╯ 16 | 17 | 好的,在 Windows 上用 WSL 我们到底能干什么呢?[^1] 18 | 19 | - 你可以在 Windows 上「安装」你喜欢的 GNU/Linux 发行版 20 | - 你可以直接在 Windows 上运行 `grep`、`awk`、`sed` 等 Linux 原生可执行文件 21 | - 你可以在 Windows 上直接使用 Vim、Emacs 等工具,直接使用 Linux 版本的 Javascript/Node.js、Ruby、Python、C/C++、Rust、Go 等语言进行开发,直接运行 MySQL、Apache 等 Linux 原生应用和服务等 22 | 23 | 最为重要的是,利用 WSL 我们可以直接在 **不抛弃 Windows 全部优秀工具的前提下**,**在没有因为虚拟机开销而牺牲太多性能的情况下**,直接运行使用完整的 Linux 系统。 24 | 25 | 在 [README 的前言](https://github.com/spencerwooo/dowww#%E5%89%8D%E8%A8%80)中,我提到了: 26 | 27 | > Windows 给编程初学者带来了很大的困难。比如缺乏好用的包管理系统、终端环境难看难用、环境变量不易配置等等,这些都让 Windows 在开发体验上难以匹敌 Linux 甚至 macOS。 28 | 29 | WSL 的超能力,就是为我们扫清了 Windows 对开发人员不友好的障碍,让我们在 Windows 上同样享受 Linux 等 Unix 风格系统的开发特色。 30 | 31 | ## WSL 与 WSL 2 32 | 33 | ::: callout 💡 背景知识 34 | WSL 是微软 2016 年春天发布的适用于 Linux 的 Windows 子系统 1.0 版本,WSL 2 是 2019 年春天微软为了解决一系列 WSL 在 1.0 版本中出现的问题而开发的新「框架」。二者都能够满足我们在 Windows 上面进行 Linux 原生开发环境部署的需求,**但是 WSL 2 的基因更为优秀**,能够避免 WSL 在 1.0 版本中体现出的种种局限性。接下来我们简单介绍 WSL 与 WSL 2 各自的特点和优势。 35 | ::: 36 | 37 | 从技术层面来说,WSL 实质上就是让 Linux 原生的 ELF64 二进制文件能够在 Windows 上面运行。但是目前 WSL 和 WSL 2 在实现这一功能的方法上大有不同。 38 | 39 | ### WSL 诞生之初的实现手段 40 | 41 | 在 WSL 时代(也就是 WSL 1.0 版本),这一切都是通过 WSL 兼容层来实现的。WSL 在 1.0 版本中,是一系列保证 Linux ELF64 二进制文件能够在 Windows 上面运行的组件的集合[^2],这其中包含了「用户态」和「核心态」的组件,主要由以下内容构成: 42 | 43 | - 用户态会话进程维持服务(User mode session manager service),用于维护 Linux 实例的生命周期 44 | - Pico 进程驱动 —— Pico provider drivers(包括 `lxss.sys`、`lxcore.sys`),**通过直接翻译「Linux 系统调用」来模拟一个 Linux 内核** 45 | - Pico 进程本体 —— Pico processes[^3],用于托管(Host)原生 Linux 用户态进程(比如 `/bin/bash` 等) 46 | 47 | ![](https://i.loli.net/2020/01/04/b6OrutxehGVWRKI.png)^[[Windows for Linux Nerds](https://blog.jessfraz.com/post/windows-for-linux-nerds/)] 48 | 49 | 最为重要的就是 WSL 中间兼容层:Pico provider drivers,正是这一层让 WSL 能够将 Linux 进程中请求的系统调用转换为 Windows 系统调用。 50 | 51 | ### WSL 2 中采用的新措施 52 | 53 | WSL 的 1.0 版本确实为 Windows 带来了新鲜的血液,但是 WSL 自发布以来就伴随着用户对「应用性能」以及「I/O 速度」的抱怨,这些都是由于兼容层的缘故,使得 WSL 在 1.0 时代牺牲了很多性能。另外 WSL 在 1.0 时代也不支持 Docker 等虚拟化技术。通过强行「翻译」Linux 系统调用带来的局限性在 WSL 1.0 中体现的淋漓尽致。 54 | 55 | WSL 2 是 WSL 发展历史中的突破性进步,上述问题在 WSL 2 中得到了根本性的解决。 56 | 57 | ![](https://cdn.spencer.felinae98.cn/github/2020/09/200902_220309-1.png) 58 | 59 | 在 WSL 2 中,微软直接将一个完整的 Linux 内核放在 WSL 的架构之中[^4],通过 Hyper-V 的技术为 WSL 构造一个轻量级、几乎不消耗额外系统开销的虚拟环境,从而让 WSL 2 实现了: 60 | 61 | - 在 Windows 上实现完整的 Linux 原生系统调用(Full System Call Compatibility) 62 | - 用 WSL 执行曾经 1.0 版本无法运行的 Linux 原生程序,比如依赖虚拟化技术的 Docker 容器 63 | - 极大的突破了 WSL 1.0 中出现的 I/O 性能瓶颈:WSL 2 中,`git clone`、`npm install`、`apt update`、`apt upgrade` 等依赖文件系统性能的操作和命令的速度都有显著提高 64 | 65 | 另外,虽然 WSL 2 使用了虚拟化技术,但是官方宣称: 66 | 67 | > WSL 2 uses the latest and greatest in virtualization technology to run its Linux kernel inside of a lightweight utility virtual machine (VM). 68 | > 69 | > WSL 2 使用的是最新、最厉害的虚拟化技术,让 Linux 内核在一个非常轻量级的虚拟机中运行。 70 | 71 | 实际使用中,你几乎不会察觉到和 WSL 1.0 版本有特别大的区别,不过 WSL 2 还是有一些和 WSL 1 不一样的地方: 72 | 73 | - 如果你在 WSL 2 中运行 Web 服务器,你会发现映射到 Windows 中的端口并不一样。不过这一不同并无大碍 74 | - 你会发现 WSL 2 中在 Windows 侧和 Linux 侧之间相互移动文件的速度会比 WSL 的 1.0 版本慢。因此,为了尽可能保证我们 WSL 文件系统的存储速度,**我们应该将在 Linux 中使用的文件全部放在 Linux 文件系统下**[^6] 75 | - WSL 2 目前对硬件的支持有限,因此你可能无法直接通过 WSL 2 访问外设[^5]。不过,WSL 2 目前已经支持了 GPU 的连接[^7][^8],同时,我们也可以通过一些 hacky 的手段[让 WSL 2 能够访问 USB 接口](/4-advanced/4-4-usb.html) 76 | - WSL 2 对 VMware 等虚拟机软件也有一些要求,也就是:你无法在运行 WSL 2 的情况下直接运行 VMware 或旧版本的 Virtual Box。这是因为 WSL 2 中的虚拟化技术依赖于 Hyper-V,而 VMware 和 6.0 版本之前的 Virtual Box 都和 Hyper-V 不兼容[^5] 77 | 78 | 除此之外,WSL 2 就没有其他特别大的问题或局限性了。在接下来的安装中,我会继续介绍如何安装某个 Linux 发行版、如何初始化一个 Linux 发行版环境、发行版如何在 WSL 1 和 WSL 2 之间进行切换等等。同学们可以按照自己的需要,在 WSL 1 和 WSL 2 之间进行选择。**当然,我这里更推荐大家使用性能更好的 WSL 2。** 79 | 80 | [^1]: [Windows Subsystem for Linux Documentation - Microsoft Docs](https://docs.microsoft.com/en-us/windows/wsl/about) 81 | [^2]: [Windows Subsystem for Linux Overview - Microsoft Developer Blog](https://blogs.msdn.microsoft.com/wsl/2016/04/22/windows-subsystem-for-linux-overview) 82 | [^3]: [Pico Process Overview - Microsoft Developer Blog](https://blogs.msdn.microsoft.com/wsl/2016/05/23/pico-process-overview/) 83 | [^4]: [Shipping a Linux Kernel with Windows - Windows Command Line](https://devblogs.microsoft.com/commandline/shipping-a-linux-kernel-with-windows/) 84 | [^5]: [WSL 2 Post BUILD FAQ - Windows Command Line](https://devblogs.microsoft.com/commandline/wsl-2-post-build-faq/) 85 | [^6]: [User Experience Changes Between WSL 1 and WSL 2 - Microsoft Docs](https://docs.microsoft.com/en-us/windows/wsl/wsl2-ux-changes) 86 | [^7]: [Enable NVIDIA CUDA in WSL 2](https://docs.microsoft.com/en-us/windows/win32/direct3d12/gpu-cuda-in-wsl) 87 | [^8]: [Announcing CUDA on Windows Subsystem for Linux 2](https://developer.nvidia.com/blog/announcing-cuda-on-windows-subsystem-for-linux-2/) 88 | -------------------------------------------------------------------------------- /docs/1-preparations/1-1-installation.md: -------------------------------------------------------------------------------- 1 | # 安装 2 | 3 | ::: callout 🍫 本文内容 4 | 在安装 WSL 之前,有一些对 Windows 的配置工作必不可少,也有一些常识性的问题需要知道。本章主要介绍 Windows 方面的准备工作、WSL 的安装方法以及注意事项。 5 | ::: 6 | 7 | ## Windows 10 8 | 9 | ### 确认 Windows 版本 10 | 11 | 只有 Windows 10 才能安装使用 WSL。Windows 7、8 或之前的任何版本都无法使用,请及时将系统版本更新至最新。 12 | 13 | 只有 Windows 10 版本 16215 或以后的版本才能够正常运行 WSL。你可以在「Windows 设置 > 系统 > 关于」处找到你的 Windows 10 操作系统版本。 14 | 15 | **只有 Windows 10 版本 18362 或 18363 以及以后的版本,或小版本号为 1049 的版本,才能够正常运行 WSL 2**。需要明确,WSL 2 目前只能在 Windows 10 版本 1903、1909 和 2004 中使用(其中 1903 和 1909 仅支持 **x64 系统**),因此你需要将自己的 Windows 系统进行升级至合适的版本,才能使用正确的 Windows 10 版本安装 WSL 2。 16 | 17 | > 见 WSL 2 向前兼容 Windows 10 版本 1903、1909 的博客文章:[WSL 2 Support is coming to Windows 10 Versions 1903 and 1909](https://devblogs.microsoft.com/commandline/wsl-2-support-is-coming-to-windows-10-versions-1903-and-1909/). 18 | 19 | ### 开启「适用于 Linux 的 Windows 子系统」的附加功能 20 | 21 | 无论使用 WSL 1 还是 WSL 2,我们都需要开启「适用于 Linux 的 Windows 子系统」的附加功能: 22 | 23 | - 以管理员身份打开 PowerShell 终端 24 | - 运行下面的命令: 25 | 26 | ```powershell 27 | $ Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux 28 | ``` 29 | 30 | - 按照提示重启电脑 31 | 32 | ## WSL 33 | 34 | ::: callout 🍰 Linux 发行版 35 | 市面上基于 WSL 有很多 Linux 发行版,有广受大家欢迎的 Ubuntu、备受黑客喜爱的 Kali Linux、以及极客们都说好的 Arch Linux。大家可以根据自己的选择进行安装。如果没有特殊的需求,那么直接[安装 Ubuntu 最新版](https://www.microsoft.com/store/productId/9NBLGGH4MSV6)准没错。如果你像我一样,喜欢滚动更新的 Arch Linux 发行版,那么[安装 Arch WSL](https://github.com/yuk7/ArchWSL) 即可。 36 | ::: 37 | 38 | ### 官方版本 WSL 39 | 40 | 微软官方支持的 WSL Linux 发行版可以直接从微软商店下载。 41 | 42 | 官方支持的 Linux 发行版有: 43 | 44 | - Ubuntu: [最新版](https://www.microsoft.com/store/productId/9NBLGGH4MSV6),[16.04 LTS](https://www.microsoft.com/store/productId/9PJN388HP8C9),[18.04 LTS](https://www.microsoft.com/store/productId/9N9TNGVNDL3Q),和 [20.04 LTS](https://www.microsoft.com/p/ubuntu-2004-lts/9n6svws3rx71) 45 | - OpenSUSE Leap: [42](https://www.microsoft.com/store/productId/9NJVJTS82TJX), [15.0](https://www.microsoft.com/store/productId/9N1TB6FPVJ8C) 46 | - SUSE Linux Enterprise Server: [12](https://www.microsoft.com/store/productId/9P32MWBH6CNS), [15](https://www.microsoft.com/store/productId/9PMW35D7FNLX) 47 | - [Debian GNU/Linux](https://www.microsoft.com/store/productId/9MSVKQC78PK6) 48 | - [Kali Linux](https://www.microsoft.com/store/productId/9PKR34TNCV07) 49 | - [Pengwin](https://www.microsoft.com/store/productId/9NV1GV1PXZ6P) - [GitHub | WhitewaterFoundry/Pengwin](https://github.com/WhitewaterFoundry/Pengwin) 50 | - [Alpine WSL](https://www.microsoft.com/store/productId/9P804CRF0395) - [GitHub | agowa338/WSL-DistroLauncher-Alpine](https://github.com/agowa338/WSL-DistroLauncher-Alpine) 51 | - [Fedora Remix for WSL](https://www.microsoft.com/en-us/p/fedora-remix-for-wsl/9n6gdm4k2hnc) - [GitHub | WhitewaterFoundry/Fedora-Remix-for-WSL](https://github.com/WhitewaterFoundry/Fedora-Remix-for-WSL) 52 | 53 | ### 社区支持的 WSL 54 | 55 | 另外也有社区支持的 WSL Linux 发行版,社区支持的第三方 WSL 发行版: 56 | 57 | - [Arch Linux](https://github.com/yuk7/ArchWSL) 58 | - [Alpine Linux](https://github.com/yuk7/AlpineWSL) 59 | 60 | 第三方支持的发行版需要按照其官方使用说明进行安装。有需要的同学请自行查看。 61 | 62 | ## WSL 的安装 63 | 64 | ![](https://cdn.spencer.felinae98.cn/github/2020/09/200902_220319.png) 65 | 66 | 在 [开启「适用于 Linux 的 Windows 子系统」的附加功能](#开启「适用于-linux-的-windows-子系统」的附加功能) 之后,我们以 Ubuntu 最新版为例,在微软商店中点击安装即可。 67 | 68 | English badge 69 | 70 | 下载成功后,我们需要对刚刚这一发行版进行初始化。在开始菜单中,没有意外的话,你应该看到刚刚下载完成的 Ubuntu 快捷方式,我们点击打开 Ubuntu,等待初始化完成即可。(初始化过程会将下载好的 Linux 发行版的文件解压缩,并存储在电脑上供你使用,往往会需要 1 分钟甚至更多。) 71 | 72 | ![](https://cdn.spencer.felinae98.cn/github/2020/09/200902_220319-1.png) 73 | 74 | 初始化完成之后,Linux 会提示你输入一个 UNIX 用户名并为之设置一个 UNIX 密码。这一用户名和密码与你的 Windows 登录账号密码完全无关,你可以任意设置。完成之后,你的 Ubuntu 发行版就安装成功了。 75 | 76 | ## WSL 2 的安装 77 | 78 | ::: callout 🧊 注意 79 | WSL 2 需要在 WSL 已经安装完毕的基础之上才能进行安装。 80 | ::: 81 | 82 | ### 开启支持 WSL 2 的可选组件 83 | 84 | 如果你希望安装 WSL 2,你需要确保你已安装「适用于 Linux 的 Windows 子系统」和「虚拟机平台」这两个可选组件。[^1]同样: 85 | 86 | - 以管理员身份打开 PowerShell 终端 87 | - 运行下面的命令: 88 | 89 | ```powershell 90 | $ dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart 91 | $ dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart 92 | ``` 93 | 94 | - 根据提示重启电脑 95 | 96 | ### 使用命令行设置需要由 WSL 2 支持的发行版 97 | 98 | 同样,用管理员身份打开 PowerShell 终端: 99 | 100 | - 执行下面命令找到你当前安装的 Linux 发行版及其名字: 101 | 102 | ```powershell 103 | $ wsl --list 104 | ``` 105 | 106 | - 之后,执行下面的命令将你需要的发行版设置为由 WSL 2 支持,将其中的 `` 修改为 Linux 发行版名称: 107 | 108 | ```powershell 109 | $ wsl --set-version 2 110 | ``` 111 | 112 | - 另外,你也可以通过下面的命令将之后新安装的发行版均设置为默认由 WSL 2 支持: 113 | 114 | ```powershell 115 | $ wsl --set-default-version 2 116 | ``` 117 | 118 | - 执行下面的命令,如果发现最后一列的 `VERSION` 数为 2,说明你的配置是成功的: 119 | 120 | ```powershell 121 | $ wsl --list --verbose 122 | ``` 123 | 124 | ![](https://cdn.spencer.felinae98.cn/github/2020/09/200902_220319-2.png) 125 | 126 | 到这里,你应该已经成功安装完成了 WSL 的某个发行版,并成功设置了 UNIX 用户名和密码,登入 WSL 系统。接下来,我将以 Ubuntu 为例子,从终端工具、Visual Studio Code 开发环境以及更高阶的操作功能这三个角度,具体介绍如何对 WSL 进行配置,使之更加「开发环境友好」。 127 | 128 | ## 遇到问题? 129 | 130 | 如果你在安装过程中遇到了一些问题,请查阅微软官方提供的 WSL 疑难排查文档:[排查适用于 Linux 的 Windows 子系统问题 - Microsoft Docs](https://docs.microsoft.com/zh-cn/windows/wsl/troubleshooting) 131 | 132 | [^1]: [Installation Instructions for WSL 2 - Microsoft Docs](https://docs.microsoft.com/en-us/windows/wsl/wsl2-install) 133 | -------------------------------------------------------------------------------- /docs/2-cli/2-0-intro.md: -------------------------------------------------------------------------------- 1 | # 概要 2 | 3 | ![](https://cdn.spencer.felinae98.cn/github/2020/09/200902_220447.png) 4 | 5 | ::: callout 🍳 本章内容 6 | 欢迎来到 **Dev on Windows with WSL** 文档的第二部分 —— WSL 命令行 CLI 环境的配置。本章我们将对 WSL 中开发学习涉及到的工具的配置方法进行介绍,这其中包括:终端工具、终端环境、Shell 的配置以及其他工具的配置。 7 | ::: 8 | 9 | ## 背景知识 10 | 11 | 为了在命令行界面下使用 WSL,我们需要一个 Terminal —— 终端模拟器。为了在 WSL 下能够正常的与 Linux 环境进行无界面的命令行交互,我们需要一个 Shell —— 命令行解释器。为了搞清楚接下来我将介绍的内容,我先来简单讲解一下「Console、Terminal 和 Shell 这三者的区别」。在我们当前的讨论范畴内: 12 | 13 | - 我们打开 Windows 的那个「小黑框」(Command Prompt),实际上就是打开了一个终端(Terminal) 14 | - 在终端里面「输入命令,得到结果」的交互程序,就是命令行解释器(Shell) 15 | 16 | 也正因如此,Windows 最新的 Shell 叫 PowerShell。(控制台 Console 则是相对古老的概念,是大型机物理意义上面的独立终端,我们在这里先不探讨这个话题。)使用 macOS 和 Linux 的同学如果听说过 `bash` 和 `zsh` 的话,它们同样也是 Shell。 17 | 18 | ![](https://cdn.spencer.felinae98.cn/github/2020/09/200902_220447-1.png) 19 | 20 | > **推荐阅读:**[告别 Windows 终端的难看难用,从改造 PowerShell 的外观开始 - 少数派](https://sspai.com/post/52868) 21 | 22 | ## 主要内容 23 | 24 | 接下来的几篇文章中,我将具体介绍下面的工具、环境的配置方法,包括但不限于: 25 | 26 | - Terminal 工具:Windows Terminal、Fluent Terminal、Hyper、Terminus…… 27 | - Shell 环境:`bash`、`zsh`、`fish`…… 28 | - Ubuntu 中的包管理工具:`apt` - Debian's Advanced Packaging Tool 29 | - 版本控制工具:Git 30 | - 远程登陆工具:SSH - Secure Shell、Mosh - the mobile shell 31 | - Windows 和 WSL 文件互访的方法 32 | - …… 33 | 34 | 本章内容将聚焦于 WSL 在命令行界面上的使用,不会涉及到 VS Code 或其他图形界面的工具。 35 | -------------------------------------------------------------------------------- /docs/2-cli/2-1-terminal.md: -------------------------------------------------------------------------------- 1 | # 终端 Terminal 2 | 3 | ::: callout 🎮 本文内容 4 | 本文中,除了默认 WSL 终端,我们还将介绍由微软官方开发的 Windows Terminal、Windows 上面第一个 UWP 终端 Fluent Terminal、跨平台的终端 Hyper 和 Terminus 的配置。在这里面,**我更加推荐 Windows Terminal**,因为这一终端是由 WSL 项目组自己开发的原生 Windows 终端,拥有最好的性能、最美的外观和最强的定制性。 5 | ::: 6 | 7 | ## Windows Terminal 8 | 9 | 在 Microsoft Build 2019 大会上,微软发布了新一代 Windows 终端工具:Windows Terminal。其令人惊艳的 Fluent UI 设计、亚克力半透明材质的背景和对 UTF-8、Emoji 等特殊字符的支持让大家赞叹不已。^[[Introducing Windows Terminal - Windows Command Line](https://devblogs.microsoft.com/commandline/introducing-windows-terminal/)]目前,Windows Terminal 已经可以直接从 Microsoft Store 中直接下载。 10 | 11 | English badge 12 | 13 | 安装成功之后,Windows Terminal 就应该直接识别出来我们本机上面安装的全部 WSL 环境、PowerShell 环境和 Command Prompt 环境。点击下拉菜单,我们就可以看到 Windows Terminal 唤起的「环境」,可以看到,我安装的 Ubuntu 和 Arch WSL 均被识别成功。 14 | 15 | ![](https://cdn.spencer.felinae98.cn/github/2020/09/200902_220913.png) 16 | 17 | Windows Terminal 的配置文件是 `JSON` 的格式,如果你安装有 VS Code,那么你可以直接点击 Settings(或者快捷键 `Ctrl + ,`)唤起 VS Code 打开设置文件。 18 | 19 | ![](https://cdn.spencer.felinae98.cn/github/2020/09/200902_220913-1.png) 20 | 21 | 在 VS Code 中打开 Windows Terminal 配置文件进行设置的时候,是可以直接自动补全的。我们对 Windows Terminal 的字体、配色、快捷键和 Profile 等一系列设置项目均在这一 `JSON` 文件中配置。其中: 22 | 23 | - 如果希望始终保持 Windows Terminal 的主题颜色为暗色方案,而不跟随 Windows 系统亮暗主题:可以在 `globals` 项目下的 `requestedTheme` 属性中这样配置: 24 | 25 | ```json {3} 26 | { 27 | "globals": { 28 | "requestedTheme": "dark" 29 | } 30 | } 31 | ``` 32 | 33 | - 如果希望将 Ubuntu WSL 设置为 Windows Terminal 打开唤起的默认环境:可以复制 Ubuntu WSL Profile 中的 `guid`,将之粘贴至 `globals` 项目下的 `defaultProfile` 属性,类似这样: 34 | 35 | ```json {3,8} 36 | { 37 | "globals": { 38 | "defaultProfile": "{d317d852-8b6a-4936-b241-58c11be8aeb2}" 39 | // … 40 | }, 41 | "profiles": [ 42 | { 43 | "guid": "{d317d852-8b6a-4936-b241-58c11be8aeb2}", 44 | "name": "Ubuntu", 45 | "source": "Windows.Terminal.Wsl" 46 | } 47 | // … 48 | ] 49 | } 50 | ``` 51 | 52 | - 如果希望将 Dracula 的配色方案添加进入 Windows Terminal:可以到 [iTerm2-Color-Scheme - Windows Terminal](https://github.com/mbadolato/iTerm2-Color-Schemes/tree/master/windowsterminal) 仓库下找到自己想要使用的配色方案,将 `JSON` 格式的配色复制进入 Windows Terminal 配置文件即可。比如我希望添加 Dracula 配色方案,只需要访问 [Dracula.json](https://github.com/mbadolato/iTerm2-Color-Schemes/blob/master/windowsterminal/Dracula.json),将代码部分复制,并粘贴到 Windows Terminal `JSON` 配置文件中: 53 | 54 | ```json {5} 55 | { 56 | // … 57 | "schemes": [ 58 | { 59 | "name": "Dracula", 60 | "background": "#282A36", 61 | "foreground": "#F8F8F2", 62 | "black": "#21222C" 63 | // … 64 | } 65 | ] 66 | } 67 | ``` 68 | 69 | - 如果希望更改 Arch WSL Profile 的字体和配色方案为 FuraCode NF 和 Dracula(FuraCode NF 是 Fira Code 的 Nerd Font 变种),并为之开启 Fluent Design 的亚克力透明背景效果:在 `commandline` 项为 `wsl.exe -d Arch` 的 Profile 中修改 70 | - 将 `fontFace` 设置为 `FuraCode NF` 71 | - 将 `colorScheme` 设置为 `Dracula` 72 | - 将 `useAcrylic` 设置为 `true` 73 | - 将 `acrylicOpacity` 设置为 `0.95` 74 | 75 | ```json {4-5,7-8,10-11} 76 | { 77 | "profiles": [ 78 | { 79 | "acrylicOpacity": 0.95, 80 | "useAcrylic": true, 81 | "closeOnExit": true, 82 | "colorScheme": "Dracula", 83 | "commandline": "wsl.exe -d Arch", 84 | // … 85 | "fontFace": "FuraCode NF", 86 | "fontSize": 12 87 | // … 88 | } 89 | // … 90 | ] 91 | } 92 | ``` 93 | 94 | 配置项目相对较多,这里不一一列举,如果有更多配置需求的同学请参考我的 Dotfile^[[spencerwooo/dotfiles - wt_profiles.json](https://github.com/spencerwooo/dotfiles/blob/master/Windows/wt_profiles.json)]。 95 | 96 | ## WSL 默认终端 97 | 98 | 我们下载好 Ubuntu 的之后,点击开始菜单 Ubuntu 的 Logo,就可以打开 WSL 默认的终端。 99 | 100 | ### 字体 101 | 102 | 由于中文的大环境,默认的 Windows 终端字体是新宋体。相信你和我一样,对这个模糊不清的字体深恶痛绝。但是由于 Windows 默认终端是一个极为底层的应用,没有使用通用 UI 渲染层,因此它对字体有着严格的要求,支持这一要求的字体(在中文环境下)只有 [Sarasa Gothic](https://github.com/be5invis/Sarasa-Gothic)。下载安装这个字体之后,你就可以在 Windows 默认终端的设置项目下设置这个字体了。特别的,`Sarasa Mono T SC`(或者中文叫等距更纱黑体)是我们编码所需的等宽字体。 103 | 104 | ![](https://cdn.spencer.felinae98.cn/github/2020/09/200902_220913-2.png) 105 | 106 | ### 配色 107 | 108 | 微软为我们提供了一个方便更改默认终端配色的工具:[ColorTool](https://github.com/microsoft/terminal/tree/master/src/tools/ColorTool),我们可以通过这个工具方便的对我们默认终端的配色进行更改,**这不仅包括 WSL 默认终端的配色更改,还包括 PowerShell 终端的配色更改**。另外,这个工具也支持读取 iTerm 主题文件。 109 | 110 | - 首先,我们在 [ColorTool 在 GitHub 的 Release 页面](https://github.com/microsoft/terminal/releases/tag/1904.29002) 下载 ColorTool 至本地,并解压 111 | - 然后,打开 Windows 默认终端,定位至刚刚解压好有 `ColorTool.exe` 的下载文件夹 112 | - 首先我们通过这个命令来看看默认有哪些自带的主题供我们使用: 113 | 114 | ```powershell 115 | $ ColorTool.exe -s 116 | ``` 117 | 118 | ::: callout 🤗 没关系 119 | 是的,不需要怀疑自己,你可以直接在 WSL 里面执行 `exe` 程序,只是需要输全程序名称包括 `exe` 程序后缀。 120 | ::: 121 | 122 | ![](https://cdn.spencer.felinae98.cn/github/2020/09/200902_220913-3.png) 123 | 124 | - 之后,我们就可以通过 `ColorTool.exe <主题名称>` 命令来预览我们当前使用的终端主题,比如: 125 | 126 | ```powershell 127 | $ ColorTool.exe solarized_dark.itermcolors 128 | ``` 129 | 130 | - 使用下面的命令应用主题: 131 | 132 | ```powershell 133 | $ ColorTool.exe -d <主题名称> 134 | ``` 135 | 136 | ![](https://cdn.spencer.felinae98.cn/github/2020/09/200902_220913-4.png) 137 | 138 | > **推荐阅读:**[告别 Windows 终端的难看难用,从改造 PowerShell 的外观开始](https://sspai.com/post/52868) 139 | 140 | ColorTool 自带了一些常见的主题供我们直接使用,你也可以从这里下载更多的 iTerm 主题配置文件 — [iTerm2-Color-Schemes](https://github.com/mbadolato/iTerm2-Color-Schemes) — 来使用。 141 | 142 | ## Fluent Terminal 143 | 144 | Fluent Terminal 是目前相对稳定的非 Electron 构建的终端。得益于 UWP 技术,Fluent Terminal 能够同样拥有美丽的亚克力半透明背景,同时也支持 iTerm2 色彩主题。 145 | 146 | ![](https://cdn.spencer.felinae98.cn/github/2020/09/200902_220913-5.png) 147 | 148 | Fluent Terminal 开源在 [felixse/FluentTerminal](https://github.com/felixse/FluentTerminal),并已经上架于 Microsoft Store 微软商店,我们可以直接下载: 149 | 150 | Get it from Microsoft 151 | 152 | 另外,大家也可以使用 Windows 上可能是最好的「包管理工具」— [Scoop](https://scoop.sh/) — 进行安装。 153 | 154 | > **推荐阅读:**[「一行代码」搞定软件安装卸载,用 Scoop 管理你的 Windows 软件](https://sspai.com/post/52496) 155 | 156 | 安装 Scoop 之后,我们在 PowerShell 中执行下面的命令,即可安装 Fluent Terminal: 157 | 158 | ```powershell 159 | $ scoop bucket add nonportable 160 | $ scoop install fluent-terminal-np 161 | ``` 162 | 163 | 拥有图形化设置界面的 Fluent Terminal 配置非常直观简单,这里就不再赘述。 164 | 165 | ## Terminus 166 | 167 | Terminus 是基于 Electron 的 Terminal。相比下文推荐的 Hyper 终端,同样是 Electron 技术搭建的情况下,Terminus 优化的相对更好,启动快速,设置界面也是 GUI 形式而非直接修改配置文件,更用户友好。 168 | 169 | ![](https://cdn.spencer.felinae98.cn/github/2020/09/200902_220913-6.png) 170 | 171 | 你可以在这里下载 Terminus 终端: 172 | 173 | - [Terminus 官网](https://eugeny.github.io/terminus/) 174 | - [Terminus 项目主页](https://github.com/Eugeny/terminus) 175 | - [Terminus Release 下载页面](https://github.com/Eugeny/terminus/releases/latest) 176 | 177 | 安装 Terminus 之后,在设置中将默认 Shell 设置为「WSL / Default distro」即可默认每次打开 WSL 环境,如下: 178 | 179 | ![](https://cdn.spencer.felinae98.cn/github/2020/09/200902_220913-7.png) 180 | 181 | Terminus 的设置是 GUI 界面的,配置简单友好,这里就不做过多描述了,如果要达到截图中的效果: 182 | 183 | | 设置项 | 参数 / 属性 | 184 | | :-------------------------- | :--------------------------------------------------------------------------------------------- | 185 | | 字体 | [Iosevka Nerd Font](https://github.com/ryanoasis/nerd-fonts/tree/master/patched-fonts/Iosevka) | 186 | | 配色 | [ayu](https://github.com/mbadolato/iTerm2-Color-Schemes/blob/master/schemes/ayu.itermcolors) | 187 | | Terminus 主题 | Standard | 188 | | Terminus Acrylic background | ✔ | 189 | | Terminus Background Type | Fluent | 190 | 191 | ## Hyper 192 | 193 | Hyper 是基于 Electron 的 Terminal,也可能是目前 Windows 上面可定制化程度最高的终端。 194 | 195 | **推荐阅读:**[打造高颜值终端——Hyper](https://sspai.com/post/56081)。 196 | 197 | ![](https://cdn.spencer.felinae98.cn/github/2020/09/200902_220913-8.png) 198 | 199 | 下载 Hyper 终端: 200 | 201 | - [Hyper 终端官网](https://hyper.is/) 202 | - [Hyper 终端下载地址](https://hyper.is/#installation) 203 | 204 | 安装一些主题、插件: 205 | 206 | - [Awesome Hyper](https://github.com/bnb/awesome-hyper) - 有趣的与 Hyper 相关的内容、项目,可以找到合适的主题配置、字体、插件和 Hyper 的使用指南。 207 | 208 | 配置 Hyper 终端默认打开 WSL 环境: 209 | 210 | - 打开 Hyper,快捷键 `Ctrl + ,` 开启配置文件; 211 | - 将配置文件的这几项进行这样的配置: 212 | 213 | ```js 214 | config: { 215 | shell: 'C:\\Windows\\System32\\wsl.exe', 216 | shellArgs: [], 217 | env: {} 218 | } 219 | ``` 220 | 221 | - 重启 Hyper 即可 222 | 223 | 禁用令人闹心的终端提示音: 224 | 225 | - 在 Hyper 中打开位于 Linux 文件系统根目录的 `.bashrc` 并添加以下代码: 226 | 227 | ```bash 228 | # Disable the bell sound on backspace 229 | set bell-style none 230 | ``` 231 | 232 | - 加载设置:`source ~/.bashrc` 233 | 234 | 其他有关 Hyper 终端的配置,请参考我的 [Dotfiles](https://github.com/spencerwooo/dotfiles)。 235 | -------------------------------------------------------------------------------- /docs/2-cli/2-2-shell.md: -------------------------------------------------------------------------------- 1 | # Shell 环境 2 | 3 | ::: callout 🍌 本文内容 4 | 本文中我们将对 WSL 的 Shell 环境进行配置。这里面包括默认 `bash` 的配置,更用户友好的 `zsh`、`fish` 的配置以及一些常见问题的解决办法等。 5 | ::: 6 | 7 | ## 更换软件源镜像 8 | 9 | 为了加速 Ubuntu 软件包在中国大陆的下载速度,我们将包管理工具 `apt` 源更换至清华 TUNA 软件源镜像。Ubuntu 的软件源配置文件是 `/etc/apt/sources.list`。将系统自带的该文件做个备份,将该文件替换为下面内容,即可使用 TUNA 的软件源镜像。 10 | 11 | - 备份原文件: 12 | 13 | ```bash 14 | $ sudo cp /etc/apt/sources.list /etc/apt/sources.list.bac 15 | ``` 16 | 17 | - 在配置文件 `/etc/apt/sources.list` 最前面添加下面的内容(下面内容为默认 Ubuntu 20.04 LTS 版本的内容): 18 | 19 | ```bash 20 | # 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释 21 | deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse 22 | # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse 23 | deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse 24 | # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse 25 | deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse 26 | # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse 27 | deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse 28 | # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse 29 | 30 | # 预发布软件源,不建议启用 31 | # deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse 32 | # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse 33 | ``` 34 | 35 | - 更新缓存: 36 | 37 | ```bash 38 | $ sudo apt update && sudo apt upgrade 39 | ``` 40 | 41 | 更多内容请参考:[Ubuntu 镜像使用帮助 - 清华大学开源软件镜像站](https://mirror.tuna.tsinghua.edu.cn/help/ubuntu/) 42 | 43 | ## APT 44 | 45 | APT - Debian's Advanced Packaging Tool 是 Ubuntu 默认包管理工具,它是由 Debian 社区开发的。使用 Ubuntu 等基于 Debian 的 Linux 发行版时,我们往往都会使用 APT 等相似的包管理工具来安装、更新我们的软件包。命令 `apt` 和 `apt-get` 与 APT 不同,它们是用来和 APT 进行交互的高层命令执行工具。请大家清楚二者的区别。 46 | 47 | 其中,在 Ubuntu 16.04 中 Ubuntu 引入了 `apt` 命令来代替曾经老用户熟悉的 `apt-get`,提供了更用户友好的操作和命令行界面,对软件包 cache 缓存的处理也更为优雅。这里我推荐大家使用 `apt` 命令来与 APT 包管理工具交互,安装、管理和更新软件和依赖,接下来的文档中,我也都会使用 `apt` 命令进行介绍。 48 | 49 | > **推荐阅读:**[Difference Between apt and apt-get Explained - It's FOSS](https://itsfoss.com/apt-vs-apt-get-difference/) 50 | 51 | ## bash 52 | 53 | 下载安装的 Windows Subsystem for Linux 默认就是 `bash` 的 Shell 环境。`bash` 是 Unix shell 的一种,是我们开发环境的基础。不过 `bash` 本身仅提供一个非常基础的命令行交互功能,没有类似 `zsh` 或 `fish` 等 Shell 的自动补全、命令提示等高阶功能。因此,这里推荐大家继续阅读,安装 `zsh` 或 `fish` 来替代 `bash`。 54 | 55 | `zsh` 和 `fish`,都是 Unix-like 系统中不可或缺的好 Shell,它们都极大的拓展了我们命令行界面的交互体验。在命令行的世界中: 56 | 57 | - `fish` 更加注重「开箱即用」的体验,让我们安装完成即拥有一个包含了命令高亮、自动补全等强大功能的 Shell 环境 58 | - `zsh` 则更加重视拓展性,借助于社区中优秀的 `zsh` 插件系统 oh-my-zsh 以及无数优秀的插件,`zsh` 同样能有比肩 `fish` 甚至比 `fish` 更高阶的功能和体验 59 | 60 | 同学们可以根据自己实际情况,在这两个 Shell 中二选一即可。**在之后的文档中,我会用 zsh 作为默认的 Shell 进行介绍。** 61 | 62 | > **推荐阅读:**[为什么说 zsh 是 shell 中的极品? - 韦易笑的回答 - 知乎](https://www.zhihu.com/question/21418449/answer/300879747)。 63 | 64 | ## zsh 65 | 66 | ### 安装、配置 67 | 68 | 安装 `zsh` 并将之设置为默认 Shell: 69 | 70 | - 利用 apt 安装 `zsh` 71 | 72 | ```bash 73 | $ sudo apt install zsh 74 | ``` 75 | 76 | 下载安装 [oh-my-zsh](https://ohmyz.sh/),可能是市面上最好的 `zsh` 配置管理工具: 77 | 78 | - 运行命令下载安装 79 | 80 | ```bash 81 | $ sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" 82 | ``` 83 | 84 | ![](https://cdn.spencer.felinae98.cn/github/2020/09/200902_220651.png) 85 | 86 | - 将 `zsh` 作为默认的 Shell 环境(如果刚刚安装脚本没有这样做的话): 87 | 88 | ```bash 89 | $ chsh -s $(which zsh) 90 | ``` 91 | 92 | ### 插件、主题模板推荐 93 | 94 | `zsh` 拥有相当丰富的主题、插件等拓展内容,这里我仅列举一些我常用的配置插件和主题模板: 95 | 96 | #### 插件 97 | 98 | ![](https://cdn.spencer.felinae98.cn/github/2020/09/200902_220651-1.png) 99 | 100 | - [zsh-autosuggestions](https://github.com/zsh-users/zsh-autosuggestions):为 `zsh` 提供基于输入历史的自动命令提示 101 | - [autojump](https://github.com/wting/autojump):快速跳转不同的目录、路径、文件夹 102 | - [zsh-syntax-highlighting](https://github.com/zsh-users/zsh-syntax-highlighting):为 `zsh` 命令提供色彩高亮 103 | 104 | #### 主题模板 105 | 106 | ![](https://cdn.spencer.felinae98.cn/github/2020/09/200902_220651-2.png) 107 | 108 | - [starship - The cross-shell prompt for astronauts](https://starship.rs/) 109 | - [powerlevel10k - A fast reimplementation of Powerlevel9k ZSH theme](https://github.com/romkatv/powerlevel10k) 110 | 111 | 主题模板和插件的安装在它们各自的文档中都有详细的介绍,这里不再赘述。 112 | 113 | ## fish 114 | 115 | 开箱即用的 `fish` 无需安装以上 `zsh` 中繁琐的插件就能拥有几乎全部上面提到的功能。如果你觉得 `zsh` 的配置繁琐无趣,那么 `fish` 可能更符合你的口味。 116 | 117 | ### 安装 fish 118 | 119 | 同样使用 Ubuntu 中的包管理工具安装 `fish`: 120 | 121 | ```bash 122 | $ sudo apt install fish 123 | ``` 124 | 125 | 将 `fish` 作为默认 Shell: 126 | 127 | ```bash 128 | $ chsh -s $(which fish) 129 | ``` 130 | 131 | ### 配置 132 | 133 | ![](https://cdn.spencer.felinae98.cn/github/2020/09/200902_220651-3.png) 134 | 135 | 未经任何配置的 `fish` 即直接支持了诸多优秀的命令行交互特性。上图的例子中,我们可以看到 `fish` 开箱自带的几个功能: 136 | 137 | - 输入命令的实时高亮,错误命令标红 138 | - 历史命令的记忆,对输入命令的实时补全 139 | - 对 `*.png` 等通配符(Wildcard character)的支持 140 | - 相对 decent 的命令提示符(Prompt) 141 | 142 | `fish` 的配置文件位于 `~/.config/fish/config.fish`,这一文件之于 `fish` 就像 `.zshrc` 之于 `zsh`、`.bashrc` 之于 `bash` 一样。 143 | 144 | 有关 `fish` 的配置方法推荐大家查看其官方文档:[fish tutorial - fish shell](https://fishshell.com/docs/current/tutorial.html) 145 | 146 | ## 常见问题 147 | 148 | ### 解决字符渲染问题 149 | 150 | 由于 Windows 下的终端们,除了微软自己开发的 Windows Terminal 外,都尚未很好的支持全角(Double width)的 Unicode 字体渲染,因此 oh-my-zsh 默认主题 robbyrussell 在某些终端(比如 Hyper)下会出现光标位置不正确的问题(见 [Issue #5](https://github.com/spencerwooo/dowww/issues/5))。目前有这样的几种解决方法: 151 | 152 | #### 使用 Windows Terminal 153 | 154 | 最方便、理智、没有妥协的方法:使用 Windows Terminal。🤣 155 | 156 | ![](https://cdn.spencer.felinae98.cn/github/2020/09/200902_220651-4.png) 157 | 158 | 最新的 Windows Terminal 在底层修改了字符的渲染方式,采取最新的渲染引擎,拥有最好的性能和最美的 UI,能够支持几乎全部字符的渲染(甚至包括 Emoji),我认为没有不选择直接使用 Windows Terminal 的理由。 159 | 160 | 如果你一定要使用非 Windows Terminal 的终端来进入 WSL 的话,下面两个方法可能可以解决你的问题。 161 | 162 | #### 修改终端显示内容 163 | 164 | 对 oh-my-zsh 默认主题文件 robbyrussell 来说: 165 | 166 | - 在 `~/.oh-my-zsh/themes` 目录下打开 oh-my-zsh 默认主题文件:`robbyrussell.zsh-theme`,将其中的全角 Unicode 字符「➜」更改为其他字符(比如「>」或者「→」) 167 | - 加载设置:`source ~/.zshrc` 168 | 169 | 需要注意的是,很多 `oh-my-zsh` 主题(包括 [更加酷炫的 `Powerlevel10k` 主题包](https://github.com/romkatv/powerlevel10k))都用到了上文提到的 Powerline Fonts 和 Nerd Fonts,而这些字形在 Windows 任何终端下都没有很好的支持,因此目前一个很好的解决方法是: 170 | 171 | - 将酷炫的命令前部分放在一行单独显示 172 | - 将需要输入的带有光标的命令部分放在第二行显示 173 | 174 | ![](https://user-images.githubusercontent.com/32114380/50538113-00a78a00-0ba5-11e9-8a7a-db8d709e8d88.png) 175 | 176 | 比如:如果你使用了 [Powerlevel10k 主题包](https://github.com/romkatv/powerlevel10k),可以考虑将下面的内容加入 `.zshrc` 来实现命令单独在第二行的显示: 177 | 178 | ```bash 179 | # 让命令主体单独在第二行显示 180 | POWERLEVEL9K_PROMPT_ON_NEWLINE=true 181 | # 让右侧命令不显示(右侧命令同样无法正常在 Windows 下渲染) 182 | POWERLEVEL9K_DISABLE_RPROMPT=true 183 | # 上文截图中的两行命令前部字符的配置 184 | POWERLEVEL9K_MULTILINE_FIRST_PROMPT_PREFIX="╭" 185 | POWERLEVEL9K_MULTILINE_LAST_PROMPT_PREFIX="╰\u276F\u276F\u276F " 186 | ``` 187 | 188 | 这样配置之后,终端中的光标位置就不会出现错位问题了,同时也不影响终端的炫酷和易用。🍻 189 | 190 | #### 修改 Windows 终端为 UTF-8 环境 191 | 192 | Windows 终端输出错位、光标错位的根本原因在于 Windows 终端默认并非是 UTF-8 的环境(至少中文默认环境下不是)。我们可以通过下面的方法保证 Windows 终端环境为 UTF-8 的编码方式: 193 | 194 | - 首先,在「控制面板 → 区域」,选择「非 Unicode 程序语言设置」,更改为「英语」,并勾选下方「Beta:使用 Unicode UTF-8 支持全球语言」的选项 195 | 196 | ![](https://cdn.spencer.felinae98.cn/github/2020/09/200902_220651-5.png) 197 | 198 | - 之后,在 PowerShell 终端中输入 `chcp 65001`,切换为 UTF-8 代码页 199 | 200 | 理论上,你就可以成功将 Windows 全部终端环境切换为 UTF-8 的编码格式了。也就是说,就算是在 Fluent Terminal 中显示下面带有左右 Prompt 的 Powerlevel9k 配置,你的终端显示都是正确无误的,光标位置也是符合预期的。 201 | 202 | ![](https://cdn.spencer.felinae98.cn/github/2020/09/200902_220651-6.png) 203 | 204 | 但是这种方法比较玄学,因此如果你并没有成功解决问题,还请使用第一种办法。与此同时,这种方法也可能对中文环境下的一些程序造成显示乱码的问题,就我遇到的问题来说:TIM(QQ 办公简洁版客户端)的输入框显示字体从微软雅黑变成了中易宋体,另外一些个人开发者的应用程序(比如酷安 @晨钟酱 开发的多亲 AI 工具箱)完全乱码。请大家谨慎操作。 205 | 206 | ### 解决 `ls` 命令背景色问题 207 | 208 | ::: callout 🔋 贡献 209 | 感谢 [@printempw](https://github.com/printempw) 提供的从根源解决这个问题的方式。由于 DrvFs 文件权限问题导致出现有问题的背景色根本原因在于这里:[DrvFs 文件权限问题](https://printempw.github.io/wsl-guide/#6-6-DrvFs-%E6%96%87%E4%BB%B6%E6%9D%83%E9%99%90%E9%97%AE%E9%A2%98)。 210 | ::: 211 | 212 | ![](https://cdn.spencer.felinae98.cn/github/2020/09/200902_220651-7.png) 213 | 214 | 可以发现,上面我们在 Linux 自己的文件系统中时,`ls` 命令的输出没什么问题,但是当我们在 WSL 中进入 Windows 文件系统中时,`ls` 命令输出则变成了白底绿色背景。简单来说,这是由于 DrvFs 文件系统的权限问题,导致 Windows 原有 NTFS 文件系统中的文件到 WSL 下权限全部变成 0777。这样在 WSL 中执行 `ls` 和 `cd` 命令之后,显示出来的结果背景色就会出现「问题」。(不过,事实上这是有意而为之的。) 215 | 216 | #### 如果不想对文件系统的权限进行修改 217 | 218 | - 可以在 `.zshrc` 最尾部添加如下代码 219 | 220 | ```bash 221 | # Change ls colours 222 | LS_COLORS="ow=01;36;40" && export LS_COLORS 223 | 224 | # make cd use the ls colours 225 | zstyle ':completion:*' list-colors "${(@s.:.)LS_COLORS}" 226 | autoload -Uz compinit 227 | compinit 228 | ``` 229 | 230 | - 加载设置:`source ~/.zshrc` 231 | 232 | #### 如果希望从根本上解决 DrvFs 文件系统的权限问题 233 | 234 | > 以下内容来自 [@printempw](https://github.com/printempw) 提供的这篇文章 > [DrvFs 文件权限问题](https://printempw.github.io/wsl-guide/#6-6-DrvFs-%E6%96%87%E4%BB%B6%E6%9D%83%E9%99%90%E9%97%AE%E9%A2%98)。 235 | 236 | - 在 WSL 中创建 `/etc/wsl.conf`,在其中填写如下内容: 237 | 238 | ``` 239 | [automount] 240 | enabled = true 241 | root = /mnt/ 242 | options = "metadata,umask=22,fmask=111" 243 | mountFsTab = true 244 | ``` 245 | 246 | - 重启 WSL,所有的盘符就会使用上面的配置自动挂载(可以使用 `mount -l` 查看) 247 | 248 | ::: callout 🔁 重启 WSL 环境 249 | 我们在退出所有开启 WSL 的终端 session 后,可以在 Windows 侧(比如 PowerShell 中)使用命令 `wsl -t ` **来关闭 WSL 环境**。其中,WSL 发行版的名字可以用命令 `wsl -l` 列出,比如:你安装的是 Ubuntu,那么在运行 `wsl -t Ubuntu` 后再打开 WSL,就能观察到重新挂载盘符了。 250 | ::: 251 | 252 | 另外,如果你想要给不同的盘符设定不同的挂载参数(上面的方法对所有盘符都有效,如果你想在 WSL 中运行 Windows 下的应用程序,就得每次都 `chmod +x` 一下,所以我一般都会把 C: 排除掉),就需要手动修改 `/etc/fstab`。首先确保 `wsl.conf` 中的 `mountFsTab` 为 `true`,然后编辑 `/etc/fstab`,添加如下内容: 253 | 254 | ``` 255 | # 不在此列表中的盘符会使用 wsl.conf 中的参数挂载 256 | # 格式可以自己去查 fstab 的帮助文档 257 | E: /mnt/e drvfs rw,relatime,uid=1000,gid=1000,metadata,umask=22,fmask=111 0 0 258 | ``` 259 | -------------------------------------------------------------------------------- /docs/2-cli/2-3-cli-tools.md: -------------------------------------------------------------------------------- 1 | # 命令行工具 2 | 3 | ::: callout 🍊 本文内容 4 | 本文将对 WSL 上使用 Git 进行版本控制,使用 GPG 为 Git commit 签名,设置 WSL 代理以及 WSL 中访问远程服务器等内容进行介绍和说明。接下来的内容为了方便介绍,我将以 zsh 为 WSL 的默认 Shell 进行讲述。 5 | ::: 6 | 7 | ## WSL 2 中的网络访问问题 8 | 9 | :::danger 🚨 使用 WSL 2 时需注意的 IP 地址问题 10 | 由于 WSL 2 实际上是一个 Linux 内核运行在 Hyper-V 虚拟机中,因此 WSL 2 目前并不能和 Windows 共享 localhost(也就是下文中涉及到的 `127.0.0.1`)。 11 | ::: 12 | 13 | 目前,WSL 2 在和本机 Windows 互相访问的时候,有这样的两种情况: 14 | 15 | - 从 Windows 主机一侧访问 WSL 中的服务:比如在 WSL 中运行一个 HTTP 服务器并监听 3000 端口,在 Windows 侧用浏览器打开 `http://:3000` 这一 URL 来访问这一 HTTP 服务器; 16 | - 从 WSL 一侧访问 Windows 中部署的服务:比如在 Windows 中运行 HTTP / SOCKS5 代理,并让 WSL 中的网络请求也经由 Windows 中启动的代理; 17 | 18 | 其中,前者 WSL 官方已经进行了完善的支持,我们可以直接在 Windows 中通过通用的 `127.0.0.1` 来访问 WSL 中启动的服务。但是对于后者来说,有时候需要我们手动在 WSL 中获取 Windows 主机的 IP 地址才能正确进行代理配置、网络访问等。为了方便下文表述,我们在文档中使用 `` 来表示你在 WSL 中访问 Windows 主机的 IP 地址: 19 | 20 | - 如果你正在使用 WSL 2 环境进行开发工作,**那么下文提到的所有 `` 都是需要我们用下面介绍的方法手动获取的 Windows 主机 IP 地址。** 21 | - 如果你仍使用 WSL 1,那么下面的配置中的 `` 即为 `127.0.0.1`。 22 | 23 | 你可以使用下面的命令来在 WSL 中获取 Windows 主机的 IP 地址: 24 | 25 | > 来自:[WSL2 的一些网络访问问题 - 获取主机的 IP](https://lengthmin.me/posts/wsl2-network-tricks/#%E8%8E%B7%E5%8F%96%E4%B8%BB%E6%9C%BA%E7%9A%84-ip)。 26 | 27 | ```bash 28 | # 一种方法 29 | $ ip route | grep default | awk '{print $3}' 30 | # 或者另一种方法 31 | $ cat /etc/resolv.conf | grep nameserver | awk '{print $2}' 32 | ``` 33 | 34 | ![](https://cdn.spencer.felinae98.cn/github/2020/09/200916_094051.png) 35 | 36 | 另外,[@implic](https://github.com/implic) 发现 `<主机名>.mshome.net` 这一特殊的 URL 可以用来在 WSL 2 中访问 Windows 主机(其中的 `<主机名>` 即为「Windows 设置 » 系统 » 关于」中所设置的设备名称的小写)。 37 | 38 | ![](https://cdn.spencer.felinae98.cn/github/2020/09/200916_094137.png) 39 | 40 | ## 版本控制 41 | 42 | Git 是目前版本控制工具的典范、代表,如果你使用 GitHub,那么我相信你已经非常了解 Git 及其使用原理和方法了。 43 | 44 | ### 安装 Git 45 | 46 | 我们可以使用 Ubuntu 包管理工具 APT 安装 Git: 47 | 48 | ```bash 49 | $ sudo apt install git 50 | ``` 51 | 52 | ### 配置 Git 使用代理 53 | 54 | :::callout 🍐 剧情预告 55 | 这里介绍 Git 使用代理,以及下面介绍 ssh 通过代理登录 GitHub,重点为了让各位熟悉命令。在「[#自动化执行代理配置](#自动化执行代理配置)」部分我们将介绍通过脚本自动化执行配置过程的方法,避免每次需要手动获取 WSL 的 IP 地址并为每个工具设定代理。 56 | ::: 57 | 58 | 配置 Git 访问 GitHub 时使用代理: 59 | 60 | ```bash 61 | $ git config --global http.https://github.com.proxy 'http://:<代理端口>' 62 | ``` 63 | 64 | 取消 Git 代理: 65 | 66 | ```bash 67 | $ git config --global --unset http.https://github.com.proxy 68 | ``` 69 | 70 | ### 使用 ssh 与 Git 登录管理 GitHub 仓库 71 | 72 | 我们可以使用 ssh 在不输入 GitHub 账户密码的情况下将 Git 仓库内容推送至 GitHub 远程仓库。ssh 是我们与远程服务器认证沟通的工具,详见下一节:[远程登录 - ssh](#远程登录)。 73 | 74 | 接下来,我们配置与 GitHub 连接的 SSH 密钥:^[[Generating a new SSH key and adding it to the ssh-agent - GitHub Help](https://help.github.com/cn/github/authenticating-to-github/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent)] 75 | 76 | - 在 WSL 下生成 SSH 公钥 — 私钥对(将邮箱替换为你的邮箱),此时生成的 SSH 密钥默认位于 `~/.ssh` 路径下,公钥为 `id_rsa.pub`,私钥为 `id_rsa`: 77 | 78 | ```bash 79 | $ ssh-keygen -t rsa -b 4096 -C "your_email@example.com" 80 | ``` 81 | 82 | - 打开 ssh-agent 使之在后台运行: 83 | 84 | ```bash 85 | $ eval "$(ssh-agent -s)" 86 | ``` 87 | 88 | - 将私钥添加到 ssh-agent 之中: 89 | 90 | ```bash 91 | $ ssh-add ~/.ssh/id_rsa 92 | ``` 93 | 94 | - 查看公钥并将之复制到剪贴板: 95 | 96 | ```bash 97 | # 查看公钥内容 98 | $ cat ~/.ssh/id_rsa.pub 99 | 100 | # 将公钥复制到剪贴板 101 | $ cat ~/.ssh/id_rsa.pub | clip.exe 102 | ``` 103 | 104 | - 将复制好的公钥添加到 GitHub 账户密钥里面:[Adding a new SSH key to your GitHub account - GitHub Help](https://help.github.com/en/github/authenticating-to-github/adding-a-new-ssh-key-to-your-github-account). 105 | 106 | ### 让 ssh 登录 GitHub 也经由代理 107 | 108 | 上面配置了 Git 使用代理,但并没有配置 ssh 通过代理的登录过程,因此我们可能遇到 ssh 认证过程无法连接的情况。为了解决这一问题,我们需要继续对 ssh 连接登录 GitHub 的认证过程进行配置。 109 | 110 | ssh 默认的配置文件位于 `~/.ssh/config`,在此文件中,我们可以配置 ssh 访问某个主机时经由代理访问,因此我们可以配置当访问 `github.com` 主机时通过代理,来加速 ssh 登录过程。 111 | 112 | 在 WSL 大部分 Linux 发行版中都有 `netcat` 这一工具,`netcat` 即 `nc`,是一个 Linux 上用于执行和 TCP、UDP 或其他网络协议相关操作的工具,我们可以使用 `nc` 来让 ssh 登录过程通过代理登录。 113 | 114 | > The nc (or netcat) utility is used for just about anything under the sun involving TCP, UDP, or UNIX-domain sockets. It can open TCP connections, send UDP packets, listen on arbitrary TCP and UDP ports, do port scanning, and deal with both IPv4 and IPv6.^[[nc — arbitrary TCP and UDP connections and listens](http://manpages.ubuntu.com/manpages/bionic/man1/nc_openbsd.1.html)] 115 | 116 | ssh 登录 GitHub 时: 117 | 118 | - 我们的主机名称 Host 为 `github.com`; 119 | - 我们的 ssh 用户 User 为 `git`; 120 | - 我们需要用这一命令让 ssh 登录过程经由代理:`nc -X 5 -x :<代理端口> %h %p`; 121 | 122 | 我们将以上配置用如下的语法写入 `~/.ssh/config`: 123 | 124 | ``` 125 | Host github.com 126 | User git 127 | ProxyCommand nc -X 5 -x :<代理端口> %h %p 128 | ``` 129 | 130 | 之后我们即可通过代理登录 GitHub 了。我们可以通过命令 `ssh -T git@github.com` 来测试我们是否能够登录成功。 131 | 132 | ![](https://cdn.spencer.felinae98.cn/github/2020/09/200903_152333.png) 133 | 134 | ## 使用 GPG 为 Git commit 签名 135 | 136 | :::callout 🎫 Why GPG? 137 | 明明前面的配置内容已经完全足够我们使用 Git 来管理发布 GitHub 仓库了,为什么我们还需要使用 GPG 签名?**因为伪造一个 Git commit 的作者和邮箱轻而易举,任何人都可能用你的身份伪造一份 Git commit,来栽赃于你。** 138 | 139 | GPG 全称为 GNU Privacy Guard,GPG 通过非对称加密来帮助我们从密码学的角度在互联网上证明「我是我」,也从而证明「这不一定真的是我」。为了防止出现我这篇文章中讲述的事情:[震惊!竟然有人在 GitHub 上冒充我的身份!](https://blog.spencerwoo.com/2020/08/wait-this-is-not-my-commit/),推荐所有人使用 GPG 为自己的 commit 签名。 140 | ::: 141 | 142 | ### 生成 GPG 密钥对 143 | 144 | 首先,大部分 Linux 发行版已经安装有 GPG 工具了,在 WSL 中执行 `gpg --version` 即可查看当前 GPG 工具的安装情况: 145 | 146 | ```bash 147 | $ gpg --version 148 | 149 | gpg (GnuPG) 2.2.19 150 | libgcrypt 1.8.5 151 | Copyright (C) 2019 Free Software Foundation, Inc. 152 | License GPLv3+: GNU GPL version 3 or later 153 | This is free software: you are free to change and redistribute it. 154 | There is NO WARRANTY, to the extent permitted by law. 155 | 156 | Home: /home/spencer/.gnupg 157 | Supported algorithms: 158 | Pubkey: RSA, ELG, DSA, ECDH, ECDSA, EDDSA 159 | Cipher: IDEA, 3DES, CAST5, BLOWFISH, AES, AES192, AES256, TWOFISH, 160 | CAMELLIA128, CAMELLIA192, CAMELLIA256 161 | Hash: SHA1, RIPEMD160, SHA256, SHA384, SHA512, SHA224 162 | Compression: Uncompressed, ZIP, ZLIB, BZIP2 163 | ``` 164 | 165 | 同时,请注意并记住 GPG 存储根目录:即输出内容中的 Home 目录。此例子中的目录为:`/home/spencer/.gnupg`。 166 | 167 | 之后,我们就可以用下面的命令来为自己生成一个 GPG 公钥和私钥: 168 | 169 | ```bash 170 | $ gpg --full-generate-key 171 | ``` 172 | 173 | - 在密钥种类处:选择默认 RSA and DSA 即可; 174 | - 在密钥长度选项处:按照 GitHub 的要求选择 4096 bits; 175 | - 在密钥过期时间处:按照自己的需要选择,默认为永不过期; 176 | - 在我们的用户 ID 和 GPG key 签名邮箱处:填写我们的常用用户名,并填入 GitHub 上面认证过的邮箱; 177 | - 最后,为密钥设置一个安全的密码,并一定记住这一密码。 178 | 179 | 这样,我们就生成了我们的第一对 GPG 密钥!我们可以用这样的命令查看当前我们拥有的所有 GPG key: 180 | 181 | ```bash 182 | $ gpg --list-secret-keys --keyid-format LONG 183 | 184 | /home/spencer/.gnupg/pubring.kbx 185 | -------------------------------- 186 | sec rsa4096/24CD550268849CA0 2020-08-29 [SC] 187 | 9433E1B6807DE7C15E20DC3B24CD550268849CA0 188 | uid [ultimate] Spencer Woo (My GPG key) 189 | ssb rsa4096/EB754D2B2409E9FE 2020-08-29 [E] 190 | ``` 191 | 192 | 其中,sec 一行的 `rsa4096/24CD550268849CA0` 就是我们的 GPG 私钥,其中的 `24CD550268849CA0` 即为我们的 GPG 私钥 ID。 193 | 194 | ### 配置 Git 默认使用 GPG 对 commit 签名 195 | 196 | 生成了 GPG 密钥,并拿到了我们的 GPG 私钥 ID 后,我们即可让 Git 用这一 GPG key 为我们的 commit 进行签名: 197 | 198 | ```bash 199 | $ git config --global user.signingkey 24CD550268849CA0 200 | $ git config --global commit.gpgsign true 201 | ``` 202 | 203 | 这样设置后,如果没有问题,之后的 commit 中 Git 就会自动为我们用这一 GPG 私钥进行签名。我们可以用这一命令确认签名的存在: 204 | 205 | ```bash 206 | $ git log --show-signature 207 | 208 | commit c407d4efc980cbee981da50d714a751999b19ddf (HEAD -> master) 209 | gpg: Signature made Sun Aug 30 17:16:18 2020 CST 210 | gpg: using RSA key 9433E1B6807DE7C15E20DC3B24CD550268849CA0 211 | gpg: Good signature from "Spencer Woo (My GPG key) " [ultimate] 212 | Author: spencerwooo 213 | Date: Sun Aug 30 17:16:18 2020 +0800 214 | 215 | Signed by GPG 216 | ``` 217 | 218 | 另外,此时我们再次用之前查看 commit 详细信息的命令查看本次 commit,我们会发现 GPG 签名已经直接保存于这一 commit 之中了: 219 | 220 | ```bash 221 | $ git cat-file -p c407d4e 222 | ``` 223 | 224 | ![](https://cdn.spencer.felinae98.cn/github/2020/09/200903_194423.png) 225 | 226 | 最后,我们需要告诉 GitHub 我们使用的 GPG 公钥。对于刚刚我们拿到的私钥 ID:24CD550268849CA0,我们使用下面的命令即可导出我们的 GPG 公钥: 227 | 228 | ```bash 229 | $ gpg --armor --export 24CD550268849CA0 230 | ``` 231 | 232 | 将输出粘贴进入 GitHub 的 [Settings » SSH and GPG keys » New GPG key](https://github.com/settings/keys),并保存。之后,我们就可以开始在 GitHub 上享受 Verified 被钦定的感觉! 233 | 234 | ### 错误排查 235 | 236 | 首先,由于 WSL 没有图形界面,因此在 WSL 中使用 GPG 在全新的配置环境下很可能出现问题。在出现问题时,我们可以使用 `--debug-all` 来让 GPG 输出更多错误日志,从而找到问题所在。我们可以用下面的命令来在不必每次都需要建立一个 commit 的情况下复现 GPG 签名中出现的问题: 237 | 238 | ```bash 239 | $ echo "test" | gpg --clear-sign --debug-all 240 | ``` 241 | 242 | 首先,你可能发现了我们每次使用 GPG 密钥签发 commit 时,都需要输入一次 GPG 密码。此时,如果我们在 Windows 上,那么会弹出 Pinentry 这个图形化的密码输入窗口,但是 WSL 并没有图形界面,因此如果我们不设置 Pinentry 的具体程序,那么每次使用 GPG 签名都会失败。这里我们有两种解决办法: 243 | 244 | - 在 WSL 中设置 `GPG_TTY` 环境变量:在 `~/.zshrc` 中加入 `export GPG_TTY="$(tty)"`,从而让 GPG 知道当前的输出终端,从而弹出命令行界面的 `pinentry-curses` 来输入 GPG 密码。但是,我个人发现这种办法偶尔在第一次启动 WSL 的时候无法设置 `GPG_TTY`(即使我将其放在 `.zshrc` 里面),也就是当我执行 `echo $GPG_TTY` 时出现 not a tty 的错误信息,此时我们就需要第二种方法; 245 | - 让 WSL 中的 GPG 使用 Windows 的 Pinentry 窗口程序:如果我们在 Windows 中也安装了 GPG 程序(可以通过 scoop 安装),那么我们应该在 GPG 的安装目录下找到叫做 `pinentry-basic.exe` 的程序。是的,**WSL 中安装的 GPG 同样可以使用 Windows 中的 Pinentry 来输入密码**!我们在 WSL 里面,在前面提到的 GPG 的 Home 目录(默认 `~/.gnupg`)中创建名为 `gpg-agent.conf` 的文件,并在其中写入: 246 | 247 | ``` 248 | pinentry-program /mnt/c//pinentry-basic.exe 249 | ``` 250 | 251 | 其中 `PATH_TO_GPG_INSTALLATION_DIRECTORY` 就是 GPG 工具在 Windows 上的安装目录,这样设置后,WSL 中的 GPG 每次进行签名,都会调用 Windows 的 Pinentry GUI,从而避免了我们 WSL 没有图形界面的尴尬局面,这一解决方法也能够避免 VS Code 在 Remote-WSL 环境下直接进行 Git commit 时由于无法开启命令行界面导致无法启动 Pinentry 的问题。 252 | 253 | 另外,在使用 Git 进行 commit 的时候,如果出现类似 Error: “signing failed: No secret key” 的报错信息,可能是 Git 使用的 GPG 命令行工具跟我们生成密钥使用的不一致。我们可以首先用 `which gpg` 来找到我们所使用的 GPG 工具的具体地址,比如 `/usr/bin/gpg`,之后告诉 Git 使用这一 GPG binary 即可: 254 | 255 | ```bash 256 | $ git config --global gpg.program /usr/bin/gpg 257 | ``` 258 | 259 | ## 代理配置 260 | 261 | 上面介绍的方法仅能对 Git 开启代理访问功能,如果我们希望 WSL 中的工具均能经由代理,那么需要通过环境变量 `http_proxy` 和 `https_proxy` 进行配置。 262 | 263 | ### 基础配置 264 | 265 | 使用下面的命令将当前 session(会话)的代理进行配置: 266 | 267 | ```bash 268 | $ export http_proxy=http://:<代理端口> 269 | $ export https_proxy=http://:<代理端口> 270 | ``` 271 | 272 | 使用下面的命令取消代理: 273 | 274 | ```bash 275 | $ unset http_proxy https_proxy 276 | ``` 277 | 278 | 我们可以使用下面的命令检测自己的对外端口: 279 | 280 | ```bash 281 | # 一个接口 282 | $ curl ipinfo.io 283 | 284 | # 另一个接口 285 | $ curl cip.cc 286 | ``` 287 | 288 | ### 自动化执行代理配置 289 | 290 | 不难发现,上面的配置非常繁琐,对于不同的工具需要不同的命令手段,使用 WSL 2 还需要手动获取 IP,为了自动化整个过程,实现一行命令设置代理,我们可以在 `~/.zshrc` 或你使用的 Shell 的配置文件中添加这样的内容,来自动化配置代理: 291 | 292 | ```bash 293 | # Fetch Windows ip address inside WSL environment 294 | WINDOWS_IP=$(ip route | grep default | awk '{print $3}') 295 | PROXY_HTTP="http://${WINDOWS_IP}:<代理端口>" 296 | PROXY_SOCKS5="${WINDOWS_IP}:<代理端口>" 297 | 298 | # Git & SSH for Git proxy 299 | proxy_git () { 300 | git config --global http.https://github.com.proxy ${PROXY_HTTP} 301 | if ! grep -qF "Host github.com" ~/.ssh/config ; then 302 | echo "Host github.com" >> ~/.ssh/config 303 | echo " User git" >> ~/.ssh/config 304 | echo " ProxyCommand nc -X 5 -x ${PROXY_SOCKS5} %h %p" >> ~/.ssh/config 305 | else 306 | lino=$(($(awk '/Host github.com/{print NR}' ~/.ssh/config)+2)) 307 | sed -i "${lino}c\ ProxyCommand nc -X 5 -x ${PROXY_SOCKS5} %h %p" ~/.ssh/config 308 | fi 309 | } 310 | 311 | # Set proxy 312 | set_proxy () { 313 | export http_proxy="${PROXY_HTTP}" 314 | export https_proxy="${PROXY_HTTP}" 315 | proxy_git 316 | } 317 | 318 | # Unset proxy 319 | unset_proxy () { 320 | unset http_proxy 321 | unset https_proxy 322 | git config --global --unset http.https://github.com.proxy 323 | } 324 | 325 | # Set alias 326 | alias proxy=set_proxy 327 | alias deproxy=unset_proxy 328 | ``` 329 | 330 | 其中: 331 | 332 | - 第一行 `WINDOWS_IP=$(ip route | grep default | awk '{print $3}')` 让我们使用 WSL 2 时可以自动获取最新的 WSL IP 地址,WSL 1 可以直接将 `WINDOWS_IP` 设置为 `127.0.0.1`; 333 | - 之后的 `PROXY_HTTP` 和 `PROXY_SOCKS5` 分别是我们代理的 HTTP 协议地址和 SOCKS5 地址; 334 | - 函数 `proxy_git()` 让我们直接设置 Git 自己的代理和 ssh 登录 GitHub 的代理; 335 | - 后续的 `set_proxy()` 和 `unset_proxy()` 就分别是设定 Git 代理和环境变量,以及取消 Git 代理、删除环境变量; 336 | 337 | 最后,我们使用命令 `proxy` 和 `deproxy` 即可开启、关闭 WSL 中工具使用代理的功能。 338 | 339 | ## 远程登录 340 | 341 | ### ssh 342 | 343 | ssh — Secure Shell 工具是与远程服务器沟通的渠道。我们不仅可以使用 ssh 登录远程服务器,还可以利用 ssh 在不输入 GitHub 账户密码的情况下将 Git 仓库内容推送至 GitHub 远程仓库。上面我们已经配置好了 ssh 登录 GitHub 的功能,接下来我们配置在 WSL 中登录远程服务器的功能。 344 | 345 | 一般远程服务器都会为你提供上传 SSH 密钥的功能,或者提供生成 SSH 密钥对供你下载使用的功能。这里以阿里云服务器为例子,阿里云为我提供了一个 `.pem` 的密钥,这一密钥就是我们的「私钥」,在执行 ssh 登录的时候,我们将以命令行参数的形式用「密钥」进行身份认证: 346 | 347 | - 一般情况下,我们会将 SSH 密钥(公钥和私钥)存储在 `~/.ssh` 目录下。为了安全且符合 ssh 工具标准,我们需要为密钥先赋予正确的权限: 348 | 349 | ```bash 350 | # 赋予只读权限 351 | $ sudo chmod 400 ~/.ssh/{SSH_KEY_FILENAME}.pem 352 | ``` 353 | 354 | - 之后,登录服务器就只需要执行类似下面的命令: 355 | 356 | ```bash 357 | # 以 {USERNAME} 的身份登录地址(或 IP)位于 {HOST_IP_OR_URL} 的远程服务器 358 | $ ssh -i ~/.ssh/{SSH_KEY_FILENAME}.pem {USERNAME}@{HOST_IP_OR_URL} 359 | ``` 360 | 361 | ![](https://cdn.spencer.felinae98.cn/github/2020/09/200902_220808-1.png) 362 | 363 | 另外,我们可以配置将 Windows 侧和 WSL 2 的 SSH 密钥共享使用。详见:[Sharing SSH keys between Windows and WSL 2 - Windows Command Line](https://devblogs.microsoft.com/commandline/sharing-ssh-keys-between-windows-and-wsl-2/) 364 | 365 | ### Mosh 366 | 367 | [Mosh](https://mosh.org/) 是新一代远程登录工具。Mosh 全新的「服务器 —— 客户端」沟通方式让我们客户端不再需要因为「延迟」或「切换网络」而需要重新登录服务器、重建 session,而丢失工作进度。Mosh 让远程服务器的连接能「持久化」,从而让我们能长时间、多网络保持同一个 session 不变。 368 | 369 | ::: callout 🍧 注意 370 | Windows 原生环境下没有 Mosh 的可安装、可执行版本,因此如果我们想使用最先进的远程登录技术 —— Mosh,或者我们需要安装 Chrome 版本的 Mosh,或者我们就需要使用 WSL。 371 | ::: 372 | 373 | 在本机 WSL 环境下安装 Mosh: 374 | 375 | ```bash 376 | $ sudo apt install mosh 377 | ``` 378 | 379 | ssh 登录远程服务器之后,在服务器上面同时安装 Mosh。我的服务器是 CentOS 7 系统,以 CentOS 为例: 380 | 381 | ```bash 382 | $ sudo yum install mosh 383 | ``` 384 | 385 | 由于 Mosh 在建立 SSH 连接之后,会使用 60000 - 61000 之间的某个端口,因此我们需要为服务器防火墙打开 60000 - 61000 端口的 UDP 转发服务: 386 | 387 | ```bash 388 | $ sudo firewall-cmd --zone=public --permanent --add-port=60000-61000/udp 389 | ``` 390 | 391 | 之后,我们在本机通过 Mosh 就可以直接登录远程服务器: 392 | 393 | ```bash 394 | $ mosh {USERNAME}@{HOST_IP_OR_URL} --ssh="ssh -i ~/.ssh/{SSH_KEY_FILENAME}.pem" 395 | ``` 396 | -------------------------------------------------------------------------------- /docs/2-cli/2-4-others.md: -------------------------------------------------------------------------------- 1 | # 杂项 2 | 3 | ## Windows 和 WSL 之间互相访问 4 | 5 | Windows 和 WSL 相互配合的一大利好就是能够直接在 Linux 中执行 Windows 可执行文件(`exe` 文件),也可以反过来在 Windows 中执行 Linux 可执行文件。同时,WSL 2 的出现让我们能够直接在 Windows 的文件资源管理器中访问 Linux 文件系统,而不会像 WSL 1.0 一样对 Linux 中的文件造成无法逆转的影响。 6 | 7 | ### 文件系统 8 | 9 | ::: callout ❗ 注意 10 | [WSL 2 架构](/dev/1-Preparations/1-0-Intro.html#wsl-2-中采用的新措施)允许我们通过 [Plan 9 文件系统协议(9P protocol server)]()来从 Windows 侧访问 Linux 文件,与访问网络资源类似。这不意味着你可以直接通过 AppData 文件目录去访问 Linux 文件,如果你这样做,依旧会对 WSL 造成不可逆的影响。 11 | ::: 12 | 13 | 在 WSL 环境中: 14 | 15 | - Windows 目录往往位于 `/mnt/c/Users/{WINDOWS_USERNAME}` 下 16 | - Linux 目录往往位于 `/home/{WSL_USERNAME}` 下 17 | 18 | 我们在 WSL 中可以通过下面的命令在文件资源管理器中打开 Linux 文件系统中的某个目录: 19 | 20 | ```bash 21 | # 进入目标目录 22 | $ cd /home/spencer 23 | 24 | # 用 Windows 文件资源管理器打开目录 25 | $ explorer.exe . 26 | ``` 27 | 28 | ![](https://cdn.spencer.felinae98.cn/github/2020/09/200902_220808-2.png) 29 | 30 | 日后,为了方便我们直接访问 WSL 文件系统中的用户根目录,我们甚至可以直接将这一路径固定在「快速访问」中,完全没有任何问题。WSL 环境中的文件可以被 Windows 直接无障碍访问,用正常 Windows 应用程序打开,没有问题。 31 | 32 | ![](https://cdn.spencer.felinae98.cn/github/2020/09/200902_220808-3.png) 33 | 34 | 事实上,Windows 的 `explorer.exe` 命令能够将任意文件按照默认打开方式打开。也就是说,我们也可以直接在 WSL 中用 `explorer.exe` 打开图片、Markdown 文件、音频、视频等。比如,我们在 WSL 环境下进入 Linux 文件系统中的某个目录,希望用 Windows 的「照片」应用打开其中的一张 PNG 图片,那么我们可以直接: 35 | 36 | ```bash 37 | $ explorer.exe {IMAGE_PATH}/{IMAGE_NAME}.png 38 | ``` 39 | 40 | ![](https://cdn.spencer.felinae98.cn/github/2020/09/200902_220808-4.png) 41 | 42 | ### 命令执行 43 | 44 | 在 WSL 环境下执行 Windows 侧的命令非常直接易懂,就是在 Windows 命令后面加上可执行文件后缀 `exe`。比如: 45 | 46 | - 执行 `explorer.exe` 打开文件资源管理器,和上面的介绍类似 47 | - 工具 `clip.exe` 是 Windows 侧的剪贴板,我们可以将 WSL 侧的命令输出利用 `clip.exe` 导入 Windows 剪贴板。比如: 48 | 49 | ```bash 50 | $ uname -r | clip.exe 51 | ``` 52 | 53 | ![](https://cdn.spencer.felinae98.cn/github/2020/09/200902_220808-5.png) 54 | 55 | 在 Windows 侧(PowerShell 中)执行 WSL 的命令也同样相似,我们只需要在命令之前加上 `wsl` 即可。比如: 56 | 57 | ```powershell 58 | # 查看 WSL 内核版本 59 | $ wsl uname -a 60 | 61 | # 查看 WSL 发行版信息 62 | $ wsl cat /etc/os-release 63 | ``` 64 | 65 | ![](https://cdn.spencer.felinae98.cn/github/2020/09/200902_220808-6.png) 66 | 67 | ### 优化 WSL 2 虚拟磁盘占用空间 68 | 69 | WSL 2 使用 [VHDX](https://docs.microsoft.com/en-us/windows-hardware/manufacture/desktop/boot-to-vhd--native-boot--add-a-virtual-hard-disk-to-the-boot-menu) 格式的虚拟磁盘储存文件,磁盘大小会在需要时自动扩容,长时间使用可能会占用较大的磁盘空间。我们可以通过 [`Optimize-VHD`](https://docs.microsoft.com/en-us/powershell/module/hyper-v/optimize-vhd?view=win10-ps) 命令来优化其占用空间。 70 | 71 | 该命令是 Hyper-V 功能的一部分,我们需要在 [Windows 功能](https://jingyan.baidu.com/article/a378c960cb5b39b328283092.html)中启用 **Hyper-V » Hyper-V 管理工具 » Windows PowerShell 的 Hyper-V 模块** 以及 **Hyper-V » Hyper-V 平台 » Hyper-V 服务**: 72 | 73 | ![](https://cdn.spencer.felinae98.cn/github/2020/09/200902_220808-7.png) 74 | 75 | 也可以参照[开启「适用于 Linux 的 Windows 子系统」的附加功能](../1-Preparations/1-1-Installation.md#开启「适用于-linux-的-windows-子系统」的附加功能)一节,通过运行命令开启: 76 | 77 | ```powershell 78 | $ Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V-Management-PowerShell 79 | $ Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V-Services 80 | ``` 81 | 82 | 然后以管理员权限执行 `Optimize-VHD -Path -Mode Full` 即可。如果不清楚存储路径,可以打开[注册表编辑器](https://support.microsoft.com/help/4027573/windows-10-open-registry-editor),在 `HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Lxss\{随机 GUID}\BasePath` 下找到。 83 | 84 | WSL 命令行方面的配置、工具、操作和问题异常处理等内容基本介绍完毕。接下来的一章,我们将对利用 Visual Studio Code 和 WSL 配合进行工作开发的内容进行介绍。 85 | -------------------------------------------------------------------------------- /docs/3-vscode/3-0-intro.md: -------------------------------------------------------------------------------- 1 | # Visual Studio Code 2 | 3 | ![](https://cdn.spencer.felinae98.cn/github/2020/09/200902_221343.png) 4 | 5 | :::callout 👓 本章内容 6 | Visual Studio Code 是一款介乎于 Text Editor(文本编辑器)和 IDE(集成开发环境)之间的一款强大的代码编辑器。(后文以 VS Code 称呼。)借助于 VS Code 和 WSL 环境,我们可以在 Windows 环境下配置一个 Linux 风格的、用 Linux 工具的、符合 Linux 规范的开发环境。本章将对 VS Code 的安装以及让 VS Code 在 WSL 环境下运行进行介绍。 7 | ::: 8 | 9 | VS Code 是一款强大的代码编辑器,也可能是市面上最受欢迎的代码编辑器。2019 年 6 月的今天,我不仅因为 [这里陈述的理由](https://sspai.com/post/47719) 推荐 VS Code,更因为 VS Code 已经通过 Remote 远程开发组件包直接支持了 WSL 下的开发调试。**一句话:如果你希望使用 WSL 作为你的主要开发环境,请直接考虑 VS Code。** 10 | 11 | ## 下载安装 12 | 13 | 你可以在 VS Code 官网下载安装 Windows 版本的 VS Code:[Visual Studio Code | Code editing. Redefined.](https://code.visualstudio.com/) 14 | 15 | ::: danger DANGER 16 | 在 Windows 文件夹与文件中出现的名字不能有(包括 Emoji 在内的)特殊符号,否则会出现无法打开终端、无法直接进入相应文件夹的问题。 17 | ::: 18 | 19 | 请注意,安装 Windows 版本 VS Code 的时候,需要: 20 | 21 | 1. 安装至 C 盘(否则会有路径与访问权限的问题) 22 | 2. 在「选择其他任务」界面勾选「其他」下的全部四个选项(为了后面在 WSL 中直接调用 `code` 命令来打开 Visual Studio Code 打下基础。) 23 | 24 | 如果没有出现问题,那么你可以通过开始菜单点击打开 VS Code,也可以在 WSL 环境或 Windows PowerShell 环境的终端下,使用下面的命令用 VS Code 打开某个目录(某个文件夹): 25 | 26 | ```bash 27 | $ code {DIRECTORY_PATH} 28 | ``` 29 | 30 | 另外,你也可以用下面的命令用 VS Code 打开当前文件夹: 31 | 32 | ```bash 33 | $ code . 34 | ``` 35 | 36 | ## 功能简介 37 | 38 | ![](https://cdn.spencer.felinae98.cn/github/2020/09/200902_221343-1.png) 39 | 40 | VS Code 的界面清晰易懂,主界面由: 41 | 42 | - Activity Bar:功能栏 43 | - Sidebar:侧边栏 44 | - Editor:编辑器 45 | - Terminal:集成终端 46 | - Status Bar:状态栏 47 | 48 | 五个区域构成。默认环境下 VS Code 拥有:文件管理、搜索管理、版本管理(Git)、调试窗口以及插件管理这五个功能区域。在功能栏可以分别唤起这几个功能。使用快捷键 `` Ctrl + ` `` 可以唤起 VS Code 的集成终端。 49 | 50 | 更多关于 VS Code 的使用方法,请参考:[Visual Studio Code - Docs](https://code.visualstudio.com/docs)。在接下来的文档里,我们将对利用 WSL 作为主要开发环境并让 VS Code 直接连接至 WSL 环境的配置工作进行介绍。 51 | -------------------------------------------------------------------------------- /docs/3-vscode/3-1-remote-dev.md: -------------------------------------------------------------------------------- 1 | # Remote 开发 2 | 3 | VS Code 远程开发组件包(VS Code Remote Development)允许 VS Code 将一个远程服务器上的环境、一个虚拟化容器中的环境或我们的 WSL 环境作为一个「全功能的开发环境」来使用。这里,我们将重点关注利用 VS Code 远程开发组件包让 VS Code 和 WSL 连接并直接开发的配置方法。 4 | 5 | ![](https://cdn.spencer.felinae98.cn/github/2020/09/200902_221434.png) 6 | 7 | ## 安装 Remote 开发插件 8 | 9 | ![](https://cdn.spencer.felinae98.cn/github/2020/09/200902_221434-1.png) 10 | 11 | VS Code 远程开发组件包由: 12 | 13 | - [Remote - WSL](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-wsl):将 VS Code 连接至 WSL 环境中进行开发 14 | - [Remote - SSH](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-ssh):利用 SSH 将 VS Code 连接至远程服务器进行开发 15 | - [Remote - Containers](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-containers):将 VS Code 连接至虚拟容器环境进行开发 16 | 17 | 三个插件组成。我们可以直接搜索 Remote Development 一起安装,或者如果我们只安装 Remote - WSL 也可以。安装之后,我们就会看到 VS Code 左下角出现了 >< - Open a remote window 的按钮。 18 | 19 | ![](https://cdn.spencer.felinae98.cn/github/2020/09/200902_221434-2.png) 20 | 21 | 如果你在左下角选择 Remote 环境中选择了「Remote-WSL: New Window」,VS Code 会默认开启一个 Remote 环境的新窗口,之后会在其中下载必要的 VS Code Server,用来沟通 Remote 环境和本机环境。 22 | 23 | ![](https://cdn.spencer.felinae98.cn/github/2020/09/200902_221434-3.png) 24 | 25 | 上面就是 VS Code Server 在 WSL 环境中的下载过程,之后我们 VS Code 的新窗口就处于 WSL 环境中了。 26 | 27 | ## 在 WSL 环境中安装插件 28 | 29 | 需要了解的重要的一点是:**VS Code 在 Remote 插件下的使用和在本机下的使用实际上是两个环境**,大部分插件并不共享。因此,我们在 VS Code 中进入 WSL 的 Remote 环境下之后,所下载的插件也都是在这个环境下才能使用的。 30 | 31 | ![](https://cdn.spencer.felinae98.cn/github/2020/09/200902_221434-4.png) 32 | 33 | 可以看到,Beautify 这一插件我在 Windows 的 Local 环境下已经安装,但是并未在 WSL 环境下安装,因此插件上出现了 Install on WSL: Arch 的选项。 34 | 35 | 同时,在 VS Code 插件侧边栏中:VS Code 为我们区分了本机 Local 环境中安装的插件,和 WSL Remote 环境下安装的插件。不过,值得注意的是,本地安装的配色、图标主题等插件我们也是可以直接使用的。 36 | 37 | ## 访问 WSL 环境中的项目 38 | 39 | 连接到 WSL 环境下的 VS Code 可以直接打开 Linux 文件系统中的文件夹、目录、文件等。进入 WSL 环境中之后,我们可以点击 VS Code 左上角 File > Open File 或 Open Folder,之后在弹出的下拉菜单中选择 Linux 文件系统里面的目录、文件……即可。 40 | 41 | ![](https://cdn.spencer.felinae98.cn/github/2020/09/200902_221434-5.png) 42 | 43 | 最新的 Remote 开发组件包还包括了 Remote Explorer,在 VS Code 的 Activity Bar - 功能栏中,我们可以点击 Remote Explorer 直接访问远程资源。(也就是 WSL 文件资源。) 44 | 45 | ![](https://cdn.spencer.felinae98.cn/github/2020/09/200902_221434-6.png) 46 | 47 | VS Code 同样支持在 WSL 环境下的终端中直接用命令 `code` 打开的文件、文件夹,这种方法会直接唤起连接至相应 WSL 环境的 VS Code 实例。 48 | 49 | ![](https://cdn.spencer.felinae98.cn/github/2020/09/200902_221434-7.png) 50 | 51 | ## 环境集成 52 | 53 | ![](https://cdn.spencer.felinae98.cn/github/2020/09/200902_221434-8.png) 54 | 55 | ### 集成终端 56 | 57 | 在 WSL 环境下打开的 VS Code 集成终端唤起的是 WSL 默认 Shell,无需配置即可直接使用。^[[Dev on Windows with WSL - Shell 环境](/dev/2-CLI/2-2-Shell.html)] 58 | 59 | ### 版本控制 60 | 61 | 同样,WSL 环境中打开的 VS Code 使用的版本控制 Git 也是 WSL 中的 Git,无需配置即可直接使用。^[[Dev on Windows with WSL - 版本控制](/dev/2-CLI/2-3-Others.html#版本控制)] 62 | -------------------------------------------------------------------------------- /docs/3-vscode/3-2-code-runner.md: -------------------------------------------------------------------------------- 1 | # Code Runner 2 | 3 | Code Runner 是一个能够一键执行编译运行的 VS Code 插件,支持多达 44 种语言。支持在代码工作区右键选择 `Run Code`、在 Command Palette (`Ctrl + Shift + P`) 输入 Run Code、或者直接快捷键 `Ctrl + Alt + N` 快速编译执行代码。无论哪种语言的开发环境,我都推荐安装。 4 | 5 | ## 下载安装 6 | 7 | 下载安装 Code Runner 插件:[Code Runner](https://marketplace.visualstudio.com/items?itemName=formulahendry.code-runner). 8 | 9 | ![](https://cdn.spencer.felinae98.cn/github/2020/09/200902_221512.png) 10 | 11 | 并在 VS Code 设置中将 Code Runner 设置为在终端中运行。 12 | 13 | ![](https://cdn.spencer.felinae98.cn/github/2020/09/200902_221512-1.png) 14 | 15 | ## 使用 Code Runner 运行文件 16 | 17 | ![](https://cdn.spencer.felinae98.cn/github/2020/09/200902_221512-2.png) 18 | 19 | 点击右上角 `▶`(或快捷键 `Ctrl + Alt + N`)即可运行当前文件。Code Runner 可以非常方便的运行单文件,也可以运行多个文件的小项目。 20 | -------------------------------------------------------------------------------- /docs/3-vscode/3-3-python.md: -------------------------------------------------------------------------------- 1 | # Python 2 | 3 | ## 安装 VS Code 插件 4 | 5 | ![](https://cdn.spencer.felinae98.cn/github/2020/09/200902_221536.png) 6 | 7 | 安装 Visual Studio Code [官方 Python 插件](https://marketplace.visualstudio.com/items?itemName=ms-python.python)。 8 | 9 | ## 安装 Python 10 | 11 | 在 WSL 环境中安装 Python: 12 | 13 | - 使用 APT 安装最新版本的 Python 3: 14 | 15 | ```bash 16 | # 安装最新版本的 Python 3 17 | $ sudo apt install python-is-python3 18 | ``` 19 | 20 | - 安装 Python 包管理 `pip` 工具: 21 | 22 | ```bash 23 | $ sudo apt install python3-pip 24 | ``` 25 | 26 | - 更新 `pip` 包管理源为清华大学 TUNA 镜像源:^[[pypi 镜像使用帮助 - TUNA](https://mirror.tuna.tsinghua.edu.cn/help/pypi/)] 27 | 28 | ```bash 29 | # 使用 TUNA 镜像源更新 pip 30 | $ pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pip -U 31 | 32 | # 将 pip 源设置为 TUNA 33 | $ pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple 34 | ``` 35 | 36 | ## 安装代码优化工具 37 | 38 | ::: callout 🥝 注意 39 | 在上面安装完成 VS Code 的 Python 插件之后,如果顺利,那么 VS Code 应该提示你缺少 Python 代码检查工具(Python linter),如果你尝试格式化文档,那么 VS Code 还会提示你缺少 Python 代码格式化工具(Python code formatter)。我们需要手动安装这些组件。 40 | ::: 41 | 42 | - 安装 Python 代码检查工具,VS Code 支持 Pylint(默认)、Flake8、mypy 等多个 Python linter,我更推荐大家使用 [Flake8](https://flake8.pycqa.org/en/latest/): 43 | 44 | ```bash 45 | $ pip install flake8 46 | ``` 47 | 48 | - 安装 Python 格式化代码工具,VS Code 支持 autopep8(默认)、yapf 和 black,我更推荐大家使用 [black](https://black.readthedocs.io/en/stable/) 来格式化代码: 49 | 50 | ```bash 51 | $ pip install black 52 | ``` 53 | 54 | ## 用 Poetry 管理 Python 项目 55 | 56 | > Poetry -- Python packaging and dependency management made easy.^[[Poetry: Official website.](https://python-poetry.org/)] 57 | 58 | ::: callout 🌽 注意 59 | 熟悉 Python 项目开发的同学可能知道,Python 需要利用虚拟环境工具 `virtualenv` 来创建虚拟环境运行 Python 项目,也需要 `pip` 包管理工具来安装 Python 依赖。使用两个单独的工具管理一个项目可能会出现诸多问题,同时 `requirements.txt` 的管理也相当不优雅。因此我们用 Poetry 作为统一管理 Python 环境和依赖的工具。Poetry 之于 Python 就如 yarn / npm 之于 Node.js、cargo 之于 Rust、composer 之于 PHP…… 60 | ::: 61 | 62 | ### 安装 Poetry 63 | 64 | 在 WSL 中安装 Poetry: 65 | 66 | ```bash 67 | $ curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python 68 | ``` 69 | 70 | 将 Tab 补全加入我们所使用的 Shell 环境,比如 zsh 中: 71 | 72 | ```bash 73 | $ poetry completions zsh > ~/.zfunc/_poetry 74 | ``` 75 | 76 | ### 基本使用 77 | 78 | 我们可以用 Poetry 直接新建一个模板 Python 项目,比如叫做 `poetry-demo`: 79 | 80 | ```bash 81 | $ poetry new poetry-demo 82 | ``` 83 | 84 | 这一命令会在 `poetry-demo` 目录下生成如下的文件内容: 85 | 86 | ::: tree 87 | `poetry-demo` 88 | ├── pyproject.toml 89 | ├── README.rst 90 | ├── `poetry_demo` 91 | │ └── \_\_init\_\_.py 92 | └── `tests` 93 | ├── \_\_init\_\_.py 94 | └── test_poetry_demo.py 95 | ::: 96 | 97 | 其中的 `pyproject.toml` 文件就是定义我们当前 Python 项目所使用依赖库的文件。 98 | 99 | 我们可以用下面的命令安装一个新的 Python 依赖: 100 | 101 | ```bash 102 | # 安装一个依赖 103 | $ poetry add requests 104 | 105 | # 安装一个仅在开发中使用的依赖(比如格式化工具 black) 106 | $ poetry add black --dev # 或 -D 107 | ``` 108 | 109 | 对于一个已定义 `pyproject.toml` 的项目,我们可以用下面的命令安装所有依赖: 110 | 111 | ```bash 112 | # 安装所有依赖 113 | $ poetry install 114 | 115 | # 安装除了仅限开发使用的依赖外的所有依赖库 116 | $ poetry install --no-dev 117 | ``` 118 | 119 | 使用 remove 关键词来移除某个依赖: 120 | 121 | ```bash 122 | $ poetry remove requests 123 | ``` 124 | 125 | > 更多 Poetry 命令行工具的使用,推荐大家参考 Poetry 官方文档:[Poetry - Commands](https://python-poetry.org/docs/cli/). 126 | 127 | ## 更多内容 128 | 129 | 有关利用 VS Code 开发 Python 项目的配置,请大家参考:[Getting Started with Python in VS Code - Visual Studio Code Docs](https://code.visualstudio.com/docs/python/python-tutorial) 130 | -------------------------------------------------------------------------------- /docs/3-vscode/3-4-c_cpp.md: -------------------------------------------------------------------------------- 1 | # C/C++ 2 | 3 | ::: callout 🥐 本文内容 4 | 本文我们会对利用 VS Code 在 WSL 环境中编写 C、C++ 程序的方法进行介绍。本文主要面向学习 C、C++ 语言的同学,包括使用 C、C++ 实现各项算法的 ACM 竞赛同学,以及学习数据结构等知识的同学。值得注意的是,本文的配置项目可能仅仅适合单个 C、C++ 代码文件的编译运行,涉及到大型 C、C++ 项目我更加推荐使用专业 IDE(比如 Visual Studio)进行开发工作。 5 | ::: 6 | 7 | 在开始之前,我们先对编译运行 C、C++ 文件的基础知识进行简单的介绍。我们可以: 8 | 9 | - 使用 `gcc` 工具来编译运行 C 语言编写的代码文件 10 | - 使用 `g++` 工具来编译运行 C++ 语言编写的代码文件 11 | - 使用 `gdb` 工具来调试 C 或 C++ 语言编写的代码文件 12 | 13 | 在命令行环境中,基本的编译运行命令大致如下: 14 | 15 | ```bash 16 | # 使用 gcc 编译一个名叫 hello.c 的 C 文件,生成名为 hello 的可执行文件,或…… 17 | $ gcc hello.c -o hello 18 | # 使用 g++ 编译一个名叫 hello.cpp 的 C++ 文件,生成名为 hello 的可执行文件 19 | $ g++ hello.cpp -o hello 20 | 21 | # 执行可执行文件 hello 22 | $ ./hello 23 | ``` 24 | 25 | ![](https://cdn.spencer.felinae98.cn/github/2020/09/200902_222006.png) 26 | 27 | 接下来,我们来看如何利用 VS Code 编译、调试 C/C++ 项目 28 | 29 | ## 安装 C/C++ 插件 30 | 31 | ![](https://cdn.spencer.felinae98.cn/github/2020/09/200902_222006-1.png) 32 | 33 | 下载安装 VS Code 官方 C/C++ 插件:[C/C++ for Visual Studio Code](https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools). 34 | 35 | ## 安装 C/C++ 编译调试工具 36 | 37 | - 下载安装包含大部分开发所需组件的 `build-essential` 组件包: 38 | 39 | ```bash 40 | $ sudo apt install build-essential 41 | ``` 42 | 43 | - 为了确保安装了编译 C/C++ 的组件,我们尝试安装编译工具 `gcc`、`g++` 和调试工具 `gdb`: 44 | 45 | ```bash 46 | $ sudo apt install gcc g++ gdb 47 | ``` 48 | 49 | - 再次确认安装状态: 50 | 51 | - 运行:`gcc --version` 与 `g++ --version`,如果有类似下面的输出,说明我们的 C/C++ 编译环境就安装成功了: 52 | 53 | ![](https://cdn.spencer.felinae98.cn/github/2020/09/200902_222006-2.png) 54 | 55 | - 运行:`gdb --version`,如果有类似下面的输出,说明我们调试 C/C++ 程序的环境就安装成功了: 56 | 57 | ![](https://cdn.spencer.felinae98.cn/github/2020/09/200902_222006-3.png) 58 | 59 | ## VS Code 智能代码提示 60 | 61 | 默认情况下,VS Code 会根据你当前所在的环境自动为你生成代码智能提示(也就是 Intellisense)的配置文件,这个文件往往叫做 `c_cpp_properties.json`,位于当前项目的 `.vscode` 文件夹下。在打开 C、C++ 文件的情况下,VS Code 右下角会出现当前使用的配置文件,比如这里就是 Linux,表示我们在 Linux 环境下,使用默认的 Linux 配置文件。 62 | 63 | ![](https://cdn.spencer.felinae98.cn/github/2020/09/200902_222006-4.png) 64 | 65 | 点击 VS Code 右下角配置文件的按钮,我们可以打开配置文件进行修改。VS Code 提供了直接修改 JSON 文件以及通过 UI 的方法修改配置的两种手段,UI 的方法有更多的说明事项,大家可以按照自己的实际情况进行配置。 66 | 67 | ![](https://cdn.spencer.felinae98.cn/github/2020/09/200902_222006-5.png) 68 | 69 | 事实上,这里需要配置的项目并不多,如果你没有涉及到一些专有的编译选项的话,这里直接使用默认配置即可。 70 | 71 | ## 文件结构 72 | 73 | 一个我个人比较推荐的 C/C++ 的文件结构是这样的: 74 | 75 | ::: tree 76 | . 77 | ├── `.vscode` 78 | │ ├── c_cpp_properties.json 79 | │ ├── launch.json 80 | │ └── tasks.json 81 | ├── hello.cpp (or hello.c) 82 | ├── in.txt 83 | └── out.txt 84 | ::: 85 | 86 | 其中: 87 | 88 | > 🔴 必要、🔵 可选、🔷 自动生成。 89 | 90 | | 属性 | 文件用途 | 91 | | ---- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | 92 | | 🔴 | `hello.cpp` 是主 C++ 文件(相应的,`hello.c` 是主 C 文件) | 93 | | 🔷 | `.vscode/` 文件夹下面安置 VS Code 编译调试所需的配置文件 | 94 | | 🔵 | `in.txt` 和 `out.txt` 是 C/C++ 程序的输入输出文件,我相信经历过 ACM 培训的同学会理解为什么要单独配置这两个文件,主要原因在于有时候输入内容太庞大复制粘贴有时候会出现格式问题,配置输入输出文件更加有利开发 | 95 | | 🔷 | `a.out` 是编译之后 C/C++ 程序的可执行文件,由编译器自动生成 | 96 | 97 | 当然一个文件夹下可以放置多个 C/C++ 文件,不会影响正常调试与开发。 98 | 99 | ## 一键编译运行 100 | 101 | ::: callout 🍔 注意 102 | C/C++ 文件的一键编译运行依赖于 Code Runner 插件,基础配置详见:[Dev on Windows with WSL - Code Runner](/dev/3-VSCode/3-2-Code-Runner.md) 103 | ::: 104 | 105 | 大多数情况下我们可以直接利用之前安装的 Code Runner 插件进行编译运行 C/C++ 程序。默认情况下 Code Runner 编译 C、C++ 文件的配置如下: 106 | 107 | ```json 108 | "code-runner.executorMap": { 109 | "c": "cd $dir && gcc $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt", 110 | "cpp": "cd $dir && g++ $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt", 111 | } 112 | ``` 113 | 114 | 可以看到: 115 | 116 | - 具体编译的部分命令是 `gcc $fileName -o $fileNameWithoutExt` 117 | - 编译出来的可执行文件为 `$dir$fileNameWithoutExt` 118 | - 运行可执行文件的命令为 `./$dir$fileNameWithoutExt` 119 | 120 | 通过 Code Runner 插件我们实际上就是自动化了这一过程,一键编译。经过这样的配置,在代码工作区右键选择 `Run Code`、在 Command Palette (`Ctrl + Shift + P`) 输入 Run Code、或者直接快捷键 `Ctrl + Alt + N`,就可以编译执行代码了。**输入等其他与程序进行交互的部分位于终端区域。** 121 | 122 | 大部分情况下,这里都不需要更改,不过如果你希望自定义编译参数,可以在 Code Runner 的配置项目 `code-runner.executorMap` 中进行相应的增减。 123 | 124 | ## 调试 C++ 项目 125 | 126 | ::: callout 🧇 注意 127 | 接下来我以 C++ 的调试为例子进行介绍,C 语言程序的调试几乎一致,大家请举一反三。 128 | ::: 129 | 130 | VS Code 有着其他编辑器无法比拟的调试功能。强大到令人发指。默认情况下,我们在已经打开一个 C++ 文件的情况下,点击 VS Code 菜单栏的 Debug > Start debugging,或者快捷键 `F5`,即可唤起 VS Code 的调试功能。在没有任何配置的情况下,首先 VS Code 会提示我们选择编译器,这里我们选择 `C++ (GDB/LLDB)` 的环境: 131 | 132 | ![](https://cdn.spencer.felinae98.cn/github/2020/09/200902_222006-6.png) 133 | 134 | 之后,选择一个已经配置好的配置,这里我们选择 `g++ build and debug active file`,也就是利用 `g++` 工具编译当前文件并对其进行调试: 135 | 136 | ![](https://cdn.spencer.felinae98.cn/github/2020/09/200902_222006-7.png) 137 | 138 | 如果一切顺利,那么我们应该直接进入调试界面。可以看到,上面的选项条就是单步执行、跳至下一部分等调试命令。在代码左侧行号旁边,我们可以为程序添加断点等等。侧边栏上面会实时显示程序中涉及到的某个变量的值,比如这里我的 `input` 变量在接收到输入之后就变成了我刚刚输入的 `Hello!`。 139 | 140 | ![](https://cdn.spencer.felinae98.cn/github/2020/09/200902_222006-8.png) 141 | 142 | 与此同时,我们可以发现,在当前项目的 `.vscode` 文件夹下出现了两个文件,它们分别是 `tasks.json` 和 `launch.json`: 143 | 144 | - `tasks.json`:是编译当前文件的指令 145 | - `launch.json`:是执行当前文件并让 `gdb` 调试工具连接到可执行文件的指令 146 | 147 | ![](https://cdn.spencer.felinae98.cn/github/2020/09/200902_222006-9.png) 148 | 149 | 大多数情况下,我们直接用上面的步骤即可对一个 C++ 代码项目进行调试。 150 | 151 | ## ACM 比赛的建议 152 | 153 | 为了更方便的处理庞大的格式化输入,在 C/C++ 中我们可以通过这样的代码片段来读取输入 `in.txt` 并将程序输出、程序运行时间写入文件 `out.txt`。这段代码是这样的: 154 | 155 | ```c 156 | #ifdef SUBMIT 157 | freopen("in.txt", "r", stdin); 158 | freopen("out.txt", "w", stdout); 159 | long _begin_time = clock(); 160 | #endif 161 | 162 | /* your code here */ 163 | 164 | #ifdef SUBMIT 165 | long _end_time = clock(); 166 | printf("time = %ld ms", _end_time - _begin_time); 167 | #endif 168 | ``` 169 | 170 | 同时要修改 `tasks.json` 中 `args` 字段,增加参数 `-DSUBMIT` 让编译器定义 `SUBMIT` 宏,未定义该宏的环境中上面的代码段则不会被编译: 171 | 172 | ```json 173 | "args": [ 174 | "-DSUBMIT", 175 | "-g", 176 | "${file}", 177 | "-o", 178 | "${fileDirname}/${fileBasenameNoExtension}" 179 | ], 180 | ``` 181 | 182 | VS Code 支持用户自定义代码片段,访问这里直接获取为 VS Code 生成的代码片段设置 > [Snippet Generator](https://url.cn/5wqOpNm): 183 | 184 | ![](https://cdn.spencer.felinae98.cn/github/2020/09/200902_222006-10.png) 185 | 186 | 然后在左下角设置 > User Snippets 中添加这个代码片段,之后只需输入 `inputoutput` 就可以自动补全了。 187 | 188 | 更多有关利用 VS Code 撰写、编译、调试 C/C++ 代码项目的方法,请参考:[C/C++ for Visual Studio Code (Preview) - Visual Studio Code Docs](https://code.visualstudio.com/docs/languages/cpp) 189 | -------------------------------------------------------------------------------- /docs/3-vscode/3-5-latex.md: -------------------------------------------------------------------------------- 1 | # LaTeX 2 | 3 | 用 WSL 下的 TeXLive 和 Visual Studio Code 的 LaTeX Workshop 可以完全配置好一个可用的 Windows LaTeX 环境,摆脱 Windows 下蛋疼的 TexLive 安装包。 4 | 5 | ## 安装 TeXLive 6 | 7 | 在 WSL 下使用包管理器安装 `texlive-full`: 8 | 9 | ```bash 10 | $ sudo apt install -y texlive-full 11 | ``` 12 | 13 | TeXLive 完整安装下来大概有 3GB 左右,这个安装过程会十分漫长,但是我十分强烈建议使用这个完整包。 14 | 15 | ## 安装 LaTeX Workshop 16 | 17 | 在 VS Code 的插件中心搜索 `LaTeX Workshop` 并安装: 18 | 19 | ![](https://cdn.spencer.felinae98.cn/github/2020/09/200902_221903.png) 20 | 21 | 当我们的 VS Code 使用 Remote 远程开发组件包连接 WSL 环境,并在 WSL 环境下直接编写 LaTeX 文档的时候,我们无需过多配置,使用 LaTeX Workshop 自带的几个编译命令(Recipe)即可。 22 | 23 | ::: callout 🍑 注意 24 | 如果在 LaTeX 文档中使用中文等 CJK 字符,那么推荐使用 `xelatex` 的命令进行编译。 25 | ::: 26 | 27 | 这样我们就基本配置成功了,以下是效果: 28 | 29 | ![](https://cdn.spencer.felinae98.cn/github/2020/09/200902_221903-1.jpg) 30 | 31 | 更多有关 LaTeX Workshop 插件的配置,请参考:[James-Yu/LaTeX-Workshop - Wiki](https://github.com/James-Yu/LaTeX-Workshop/wiki) 32 | -------------------------------------------------------------------------------- /docs/3-vscode/3-6-nodejs.md: -------------------------------------------------------------------------------- 1 | # Node.js 2 | 3 | :::callout 🥂 版本更新 4 | 由于 VS Code 在支持 Remote-WSL 之后,在 WSL 中开发 Node.js 项目和正常无异,因此我([@SpencerWoo](https://github.com/spencerwooo))重新撰写了这部分内容,和 @suyanhanx 原先贡献的内容有较大变动。 5 | ::: 6 | 7 | 在开始之前,我先来介绍几个 Node.js 相关的概念: 8 | 9 | - Node.js - A JavaScript runtime built on Chrome's V8 JavaScript engine 是一个不依赖浏览器的 JavaScript 运行环境,大部分前端项目比如 Vue、React 和后端项目比如 Express、Koa 均依赖于 Node.js 生态系统; 10 | - `n` - Interactively Manage Your Node.js Versions:是一个 Node.js 版本管理工具,我们可以使用 `n` 来安装不同版本的 Node.js 环境; 11 | - npm 和 yarn,分别是 Node.js 的包管理工具,其中我更推荐大家使用后者(yarn)来管理安装 Node.js 依赖; 12 | 13 | 接下来,我们将在 WSL 中使用 `n` 来安装 Node.js 环境,并配置包管理工具 yarn 来管理 Node.js 环境中的依赖。 14 | 15 | ## 安装 Node.js 16 | 17 | 建议大家使用 [`n`](https://github.com/tj/n) 来管理与安装 Node.js,便于切换版本和快捷安装。首先,我们使用 [n-install](https://github.com/mklement0/n-install) 安装 `n`: 18 | 19 | ```bash 20 | $ curl -L https://git.io/n-install | bash 21 | ``` 22 | 23 | n-install(也就是上面的命令)默认会自动帮我们将 `n` 的重要环境变量 `PREFIX` 和 `N_PREFIX` 设置到 `$HOME/n`,并将 `n` 安装到 `$HOME/n/bin`。同时,n-install 会帮助我们更新当前我们所使用的 Shell(比如 zsh),在相应的配置文件中将 `$HOME/n/bin` 添加到 `PATH` 中。最后,n-install 会帮我们安装最新的 LTS 版本的 Node.js 环境。 24 | 25 | > 更多关于 n-install 的使用,请参考 [n-install 的 README 文档](https://github.com/mklement0/n-install)。 26 | 27 | 使用 n-install 安装好 `n` 以及 Node.js 之后,我们可以通过下面的命令进行验证: 28 | 29 | ```bash 30 | # 验证 n 的安装情况 31 | $ n --version 32 | 6.7.0 33 | 34 | # 验证 Node.js 的安装情况 35 | $ node --version 36 | v12.18.3 37 | 38 | # 验证 npm 的安装情况 39 | $ npm --version 40 | 6.14.6 41 | ``` 42 | 43 | 另外,我们也可以用 `n` 下载其他版本的 Node.js: 44 | 45 | ```bash 46 | # 下载最新的 LTS 版本 Node.js 47 | $ n lts 48 | 49 | # 下载特定版本的 Node.js 50 | $ n 10.16.0 51 | ``` 52 | 53 | 直接运行命令 `n` 也可以进入 interactive 界面,让我们选择使用某个版本的 Node.js。 54 | 55 | > 有关使用 `n` 下载安装与使用 Node.js 的命令和方法,请参考 [n 的官方 README 文档](https://github.com/tj/n)。 56 | 57 | ## 配置 Node.js 包管理工具 58 | 59 | 接下来,我们安装 [yarn](https://yarnpkg.com/),推荐大家安装使用 yarn:这个更加现代、科学的 Node.js 包管理工具。刚刚下载的 Node.js 中包含有 npm,因此我们可以直接用 npm 来安装 yarn: 60 | 61 | ```bash 62 | $ npm install -g yarn 63 | ``` 64 | 65 | 此时我们安装的 yarn 实际上是 1.22.0: 66 | 67 | ```bash 68 | $ yarn --version 69 | 70 | 1.22.4 71 | ``` 72 | 73 | 此时我们安装的是 yarn 1.0 版本,也就是比较传统的管理安装 Node.js 依赖的工具,跟 npm 比较类似。yarn 在 2.0+ 版本中经历了重大的变化,因此如果我们需要对某个项目开启 yarn 的 2.0 版本,需要使用下面的命令手动开启: 74 | 75 | ```bash 76 | $ yarn set version berry 77 | ``` 78 | 79 | 其中,yarn 2.0 所支持的 Plug and play - Plug'n'Play 功能是 yarn 2.0 的重磅功能,也是我们使用 yarn 2.0 的主要原因,Plug'n'Play 重点解决了 Node.js 的 `node_modules` 存在于每个项目中的问题,更多内容请见:[yarn - Plug'n'Play](https://yarnpkg.com/features/pnp)。需要注意的是,并非不是所有的 Node.js 库均支持 yarn 2.0,因此推荐预先查看你所使用的库是否支持 yarn 2.0:[Compatibility Table](https://yarnpkg.com/features/pnp#compatibility-table)。 80 | 81 | > 如果你之前使用 yarn 1.0 版本,推荐查看 yarn 官方的 1.0 至 2.0 版本迁移指南:[Migrating from Yarn 1](https://yarnpkg.com/advanced/migration)。 82 | 83 | 84 | 有关在 VS Code 中开发 Node.js 项目的方法,更多请参考:[Node.js tutorial in Visual Studio Code - Visual Studio Code Docs](https://code.visualstudio.com/docs/nodejs/working-with-javascript) 85 | -------------------------------------------------------------------------------- /docs/3-vscode/3-7-dotnetcore.md: -------------------------------------------------------------------------------- 1 | # .NET Core 2 | 3 | :::callout 🥦 前导知识 4 | [.NET Core](https://docs.microsoft.com/zh-cn/dotnet/core/about) 是开放源代码通用开发平台,由 Microsoft 和 .NET 社区在 [GitHub](https://github.com/dotnet/core) 上共同维护。它跨平台(支持 Windows、macOS 和 Linux),并且可用于生成设备、云和 IoT 应用程序。 5 | ::: 6 | 7 | ## 安装插件 8 | 9 | 下载 Visual Studio Code 官方 C# 插件:[C# for Visual Studio Code (powered by OmniSharp).](https://marketplace.visualstudio.com/items?itemName=ms-vscode.csharp). 10 | 11 | ## 在 WSL 侧安装 .NET Core 12 | 13 | 以下内容以 Ubuntu 20.04 为例,其他发行版请参考:[官方文档](https://dotnet.microsoft.com/download/linux-package-manager/rhel/sdk-current)。 14 | 15 | ```bash 16 | # 下载 .NET 安装包 17 | $ wget https://packages.microsoft.com/config/ubuntu/20.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb 18 | $ sudo dpkg -i packages-microsoft-prod.deb 19 | 20 | # 使用 APT 下载安装合适的 .NET SDK 21 | $ sudo add-apt-repository universe 22 | $ sudo apt install apt-transport-https 23 | $ sudo apt update 24 | $ sudo apt install dotnet-sdk-2.2 25 | ``` 26 | 27 | ## 在 WSL 侧安装 Debugger 28 | 29 | ```bash 30 | $ sudo apt install unzip 31 | $ curl -sSL https://aka.ms/getvsdbgsh | bash /dev/stdin -v latest -l ~/vsdbg 32 | ``` 33 | 34 | ## 调试 .NET Core 程序 35 | 36 | - 配置 `.vscode/launch.json`: 37 | 38 | ```json 39 | { 40 | "version": "0.2.0", 41 | "configurations": [ 42 | { 43 | "name": ".NET Core Launch (console)", 44 | "type": "coreclr", 45 | "request": "launch", 46 | "preLaunchTask": "build", 47 | "program": "/mnt/c/your-project-path/.dll", 48 | "cwd": "/mnt/c/your-project-path", 49 | "console": "internalConsole", 50 | "pipeTransport": { 51 | "pipeCwd": "${workspaceRoot}", 52 | "pipeProgram": "bash.exe", 53 | "pipeArgs": ["-c"], 54 | "debuggerPath": "~/vsdbg/vsdbg" 55 | }, 56 | "sourceFileMap": { 57 | "/mnt/c/your-project-path": "C:\\your-project-path" 58 | } 59 | } 60 | ] 61 | } 62 | ``` 63 | 64 | - 配置 `.vscode/task.json`: 65 | 66 | ```json 67 | { 68 | "version": "2.0.0", 69 | "tasks": [ 70 | { 71 | "label": "build", 72 | "command": "dotnet", 73 | "type": "shell", 74 | "args": ["build", "/mnt/c/your-project-path/projectname.csproj"], 75 | "options": { 76 | "shell": { 77 | "executable": "bash.exe", 78 | "args": ["-c"] 79 | } 80 | } 81 | } 82 | ] 83 | } 84 | ``` 85 | 86 | 按 F5 进入调试: 87 | 88 | ![](https://cdn.spencer.felinae98.cn/github/2020/09/200902_221819.png) 89 | 90 | 更多配置信息请阅读:[Configuring debugging](https://github.com/OmniSharp/omnisharp-vscode/wiki/Windows-Subsystem-for-Linux#configuring-debugging) 91 | -------------------------------------------------------------------------------- /docs/4-advanced/4-0-intro.md: -------------------------------------------------------------------------------- 1 | # 概要 2 | 3 | ![](https://cdn.spencer.felinae98.cn/github/2020/09/200902_221200.png) 4 | 5 | ::: callout 🍄 本章内容 6 | 本章将对 WSL 中的一些附加功能进行介绍。包括:为没有窗口界面的 WSL 安装 GUI 程序,以及 LxRunOffline 的功能和使用。 7 | ::: 8 | 9 | WSL 的基本功能和配置在前面我们基本介绍完毕,但是还有一些特殊的操作或者工具需要我们了解。它们可能: 10 | 11 | - 是在使用 WSL 过程中可能会遇到的比较棘手问题的解决方法; 12 | - 是更好的,但也是更加麻烦配置的使用方法; 13 | - 是需要一定知识技巧才可以配置完美的操作; 14 | 15 | 这些内容虽然可能比较高阶,但是并不是每个使用 WSL 的用户都会遇到,也并不是所有人都需要配置。 16 | -------------------------------------------------------------------------------- /docs/4-advanced/4-1-gui.md: -------------------------------------------------------------------------------- 1 | # GUI 图形化窗口 2 | 3 | :::callout 🍋 本文内容 4 | WSL 是一个完全的「终端」命令行环境,默认情况下是没有窗口程序的。但并不是所有在 WSL 上运行的应用程序都能在「无窗口」环境下运行,也不是所有的开发工具都能像 VS Code 一样利用 Remote 远程开发插件连接,从而提供一个近似原生的开发体验。这时候,**我们就需要为 WSL 提供一个 GUI 环境**,来运行需要窗口的应用程序。 5 | ::: 6 | 7 | ## 安装 XServer for windows 8 | 9 | - 推荐安装 [VcXsrv Windows X Server](https://sourceforge.net/projects/vcxsrv/),并以这样的配置打开: 10 | 11 | ![](https://cdn.spencer.felinae98.cn/github/2020/09/200902_221232.jpg) 12 | 13 | - 在 WSL 中安装必要组件: 14 | 15 | ```bash 16 | $ sudo apt install libgtk2.0-0 libxss1 libasound2 17 | ``` 18 | 19 | - 配置 WSL 参数: 20 | 21 | ```bash 22 | # 如果你使用的是 WSL 1 23 | $ echo 'export DISPLAY=:0.0' >> .profile 24 | 25 | # 如果你使用的是 WSL 2:填入 {YOUR_WINDOWS_IP},你的本机 IP 地址 26 | $ echo 'export DISPLAY={YOUR_WINDOWS_IP}:0.0' >> .profile 27 | ``` 28 | 29 | - 安装一个小眼睛,看看图形窗口安装成功没有: 30 | 31 | ```bash 32 | $ sudo apt install x11-apps -y && xeyes 33 | ``` 34 | 35 | ![](https://cdn.spencer.felinae98.cn/github/2020/09/200902_221232-1.png) 36 | 37 | 如果出现了这样的小眼睛盯着你的鼠标看,那么说明我们的 XServer 安装成功了。 38 | 39 | ## 在 WSL 环境中安装 VS Code 40 | 41 | - 添加 Visual Studio Code 库: 42 | 43 | ```bash 44 | $ curl https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > microsoft.gpg 45 | $ sudo mv microsoft.gpg /etc/apt/trusted.gpg.d/microsoft.gpg 46 | $ sudo sh -c 'echo "deb [arch=amd64] https://packages.microsoft.com/repos/vscode stable main" > /etc/apt/sources.list.d/vscode.list' 47 | ``` 48 | 49 | - 安装 Visual Studio Code: 50 | 51 | ```bash 52 | $ sudo apt update && sudo apt upgrade 53 | $ sudo apt install code 54 | ``` 55 | 56 | - 打开 XServer 窗口,在 WSL 终端执行 `code`,应该就可以看到 WSL 中的 VS Code 窗口启动了 57 | - 如果出现运行 `code` 命令报错,提示缺少 `libx11-xcb1` 库,那么按照下面的方法安装即可: 58 | 59 | ```bash 60 | $ sudo apt install libx11-xcb1 61 | ``` 62 | 63 | ![](https://cdn.spencer.felinae98.cn/github/2020/09/200902_221232-2.png) 64 | 65 | ## 其他应用 66 | 67 | 上面安装 VS Code 仅仅是一个 Proof of concept,来证明我们能够真的从 WSL 环境下启动「窗口程序」。接下来,我们可以借助于 XServer 来让 WSL 中安装的任意其他工具拥有 GUI 窗口。比如:Intellij IDEA、Android Studio 等程序。 68 | -------------------------------------------------------------------------------- /docs/4-advanced/4-2-lxrunoffline.md: -------------------------------------------------------------------------------- 1 | # 未受支持的 Linux 发行版 2 | 3 | ::: callout 🥞 本文内容 4 | 本文重点关注如何在 WSL 中从头部署一个完整的 Linux 发行版。如果你希望使用的 Linux 发行版并未被官方支持,当前也没有社区的支持,那么你可能需要按照下面的方法进行配置。在少数派上有更为详细的中文介绍:[想安装更多 Linux 发行版?LxRunOffline 让 WSL 更好用](https://sspai.com/post/61634). 5 | ::: 6 | 7 | [LxRunOffline](https://github.com/DDoSolitary/LxRunOffline) is a **Windows command line tool** that can dispatch whatever Linux distributions, however you would like. Following the WSL tradition, you are free to pick from whatever Linux distributions. For a complete list of all possibilities, [visit this link](https://github.com/DDoSolitary/LxRunOffline/wiki). 8 | 9 | ## Quick start 10 | 11 | ### Get LxRunOffline ready 12 | 13 | The best way to install is to use `choco`. As suggested in the project's home page, pick either Command Prompt or PowerShell to execud the following installation command: 14 | 15 | ```powershell 16 | $ choco install lxrunoffline 17 | ``` 18 | 19 | Note, you will need to press `y` key, for once, to confirm something. 20 | 21 | ### Download and install the Linux image 22 | 23 | [This table](https://github.com/DDoSolitary/LxRunOffline/wiki) details all possibilties, but can be a bit overwhelming. In particular, most of the entries in the table are meant for building dockers, which I know very little about. Here goes a layman approach to getting a _fully_ functional Linux. 24 | 25 | 1. Download the image from: 26 | 27 | 2. Store it somewhere, and run the following `LxRunOffline` command through, preferrably, a Command Prompt window with Admin privilege. (You can press `Win + X`, and then `A`, to get it.) 28 | 29 | ```powershell 30 | $ LxRunOffline i -n UF -d c:\WSL\Full -f -s 31 | ``` 32 | 33 | Upon filling up the precise absolute path to the `16.04.2-server-cloudimg-amd64-root.tar.gz` file, the command shall create a distribution named as UF, in directory `c:\WSL\UFull`. 34 | 35 | :::callout 🍼 Comments about those "docker images" 36 | Docker is meant to be as light-weight as possbile, and a functional Linux distribution may come in size of less than 20MB. Yet, with these skinny super-light-weight Linux distro, at times, you won't even have the `apt-get` command. 37 | ::: 38 | 39 | ### Accessing the newly installed image 40 | 41 | From any console interface, be it PowerShell or Command Prompt, if you have followed the naming convention above, the following set of command will load the newly minted Linux distro: 42 | 43 | ```powershell 44 | $ LxRunOffline run -n UF 45 | ``` 46 | 47 | Other frequently commands include: 48 | 49 | - `LxRunOffline l`: for listing all installed distributions; 50 | - `LxRunOffline`: for admiring the list of possible other commands. 51 | 52 | ### Caveats 53 | 54 | 1. `LxRunOffline i` can only install a Linux distro with `root`. 55 | 2. `LxRunOffline r -n Name_of_Distro` will get you to the root access. 56 | 3. You will need to create your user account, if you prefer to have one. 57 | 4. For now, you cannot deploy the linux distribution you installed in PC-1 to PC-2, should they don't share the same Windows build number. (I tested to copy the folder generated on Build 1809 to a Build 1803 machine, and get a `0x80070040` error.) 58 | 59 | ## Advanced usage 60 | 61 | Here goes a wish list of pending items to be added, including: 62 | 63 | 1. Getting X11 forwarding to work: 64 | 1. Use the default settings for Xming, which open port `:0` for display 65 | 2. Add the following setting into `~/.bashrc` (or the setup file for your Fish/Zsh shell): 66 | ```bash 67 | $ export DISPLAY=:0 68 | ``` 69 | 2. Firing `LxRunOffline`-deployed WSL from a console emulator (ConEmu, Hyper.is, 70 | or Cmder). Currently, one will need to access the Comand Prompt or PowerShell first, then to the root user, then to the desired user. 71 | - It should be doable to put up some `*.sh` script to set the non-`root` user as the default user? Pending instruction on this. 72 | -------------------------------------------------------------------------------- /docs/4-advanced/4-3-wslconfig.md: -------------------------------------------------------------------------------- 1 | # 配置 WSL 2 全局设置 2 | 3 | ::: callout 📟 版本限制 4 | 请注意,这些配置可在 Windows Build 19041 及以后版本中使用,且仅限 WSL 2。 5 | ::: 6 | 7 | 你可以通过将 `.wslconfig` 文件放置在用户文件夹的根目录下(即 `C:\Users\\.wslconfig`)来配置全局 WSL 选项。此文件中许多配置与 WSL 2 有关,你需要运行 `wsl --shutdown` 来关闭 WSL 2 虚拟机,重启 WSL 2 ,以确保这些配置生效。 8 | 9 | 一个 `.wslconfig` 文件的示例: 10 | 11 | ```ini 12 | [wsl2] 13 | # 自定义 Linux 内核的绝对路径 14 | kernel= 15 | # 给 WSL 2 虚拟机分配的内存大小 16 | memory= 17 | # 为 WSL 2 虚拟机分配的处理器核心数量 18 | processors= 19 | # 为 WSL 2 虚拟机分配的交换空间,0 表示没有交换空间 20 | swap= 21 | # 自定义交换虚拟磁盘 vhd 的绝对路径 22 | swapFile= 23 | # 是否允许将 WSL 2 的端口转发到主机(默认为 true) 24 | localhostForwarding= 25 | 26 | # `` 必须是带反斜杠的绝对路径,例如 `C:\\Users\\kernel` 27 | # `` 必须在后面加上单位,例如 8 GB 或 512 MB 28 | ``` 29 | 30 | 可以自行写入需要的配置,不需要的配置即为默认值,以下是该文件所包含的配置选项,这些设置会影响所有的 WSL 2 发行版。 31 | 32 | | 键 | 值类型 | 默认值 | 备注 | 33 | | :------------------ | :------ | :------------------------------------------- | :----------------------------------------------------------------------------------------------------------------- | 34 | | kernel | string | 微软默认提供的 WSL 内核 | 连接到自定义 Linux 内核的绝对路径 | 35 | | memory | size | Windows 总内存的 80% | 给 WSL 2 虚拟机分配的内存大小 | 36 | | processors | number | 在 Windows 上的处理器核心数量相同 | 为 WSL 2 虚拟机分配的处理器核心数量 | 37 | | localhostForwarding | boolean | `true` | 是否通过 localhost:port 将绑定到 WSL 2 虚拟机中的通配符或 localhost 的端口连接到主机 (允许 WSL 2 的端口转发到主机) | 38 | | kernelCommandLine | string | 无参数 | 额外的内核命令行参数 | 39 | | swap | size | Windows 上内存大小的 25% | 为 WSL 2 虚拟机分配的交换空间,0 表示没有交换空间 | 40 | | swapFile | string | `%USERPROFILE%\AppData\Local\Temp\swap.vhdx` | 交换虚拟磁盘 vhd 的绝对路径 | 41 | 42 | 更多有关 `.wslconfig` 的配置,请参考官方文档:[Configure global options with .wslconfig](https://docs.microsoft.com/en-us/windows/wsl/wsl-config#configure-global-options-with-wslconfig). 43 | -------------------------------------------------------------------------------- /docs/4-advanced/4-4-usb.md: -------------------------------------------------------------------------------- 1 | # 让 WSL 2 支持 USB 2 | 3 | ::: callout 📟 版本限制 4 | 请注意,本文方法需要 Windows Build 19041 及以后版本,且仅限 WSL 2 使用。本部分参考了这一 GitHub 仓库中的介绍:[Adding USB support to WSL 2](https://github.com/rpasek/usbip-wsl2-instructions). 5 | ::: 6 | 7 | 本文介绍的办法能够通过 Windows 侧安装的 USBIP 来将 USB 数据包转发至 Linux 侧的 USBIP,从而让 WSL 2 支持 USB 设备。默认情况下,WSL 2 的 Linux 内核并不支持 USB,本文将介绍如何向 WSL 2 的 Linux 内核中添加 USB 功能,以及如何用 USBIP 将 USB 设备连接至 Linux 上。 8 | 9 | ## USBIP-Win 10 | 11 | :::warning 🔧 手动编译 12 | 如果你需要最新版本的 USBIP-Win,那么你需要手动编译这一程序。 13 | ::: 14 | 15 | ### 手动编译(最新版) 16 | 17 | 将 USBIP-Win 仓库源代码通过 Git 克隆至本地: 18 | 19 | ```shell 20 | $ git clone https://github.com/cezuni/usbip-win.git 21 | ``` 22 | 23 | 根据 USBIP-Win 仓库 README 编译步骤来编译 USBIP-Win,我们需要安装 Visual Studio、Windows SDK 以及 Windows Driver Kit,**一定按照 README 中给出的顺序依次安装,不要同时安装多个组件,否则编译会出现问题。** 24 | 25 | ### 下载安装预编译二进制文件 ![GitHub release (latest by date including pre-releases)](https://img.shields.io/github/v/release/cezanne/usbip-win?include_prereleases) 26 | 27 | 当然,我们也可也从 GitHub Release 中下载可能不是最新版本的 USBIP-Win 预编译文件: 28 | 29 | > [cezuni/usbip-win/releases](https://github.com/cezuni/usbip-win/releases). 30 | 31 | ## 向 Linux 侧添加 USB 支持 32 | 33 | :::warning 🔧 手动编译 34 | 这里我们将手动编译 Linux 内核,请注意可能出现的任何问题。 35 | ::: 36 | 37 | 在 WSL 中,下载安装编译 Linux 内核所需要的库和工具: 38 | 39 | ```shell 40 | $ sudo apt install build-essential flex bison libssl-dev libelf-dev libncurses-dev autoconf libudev-dev libtool 41 | ``` 42 | 43 | 找到我们当前所使用的 Linux 内核名称: 44 | 45 | ```shell 46 | $ uname -r 47 | 48 | 4.19.43-microsoft-standard 49 | ``` 50 | 51 | :::callout 🌏 名称提示 52 | 这里我们得到的 `4.19.43-microsoft-standard` 即为 WSL 的 Linux 内核名称。下文命令中将以此作为命令中所使用的内核名称,因此请将下文中涉及到这一内容的部分替换为你自己的内核版本名称。 53 | ::: 54 | 55 | 将当前的 WSL 内核克隆至本地,Linux 内核源码一般放置在 `/usr/src/<内核名称>` 路径下。克隆完成后进入这一目录: 56 | 57 | ```shell 58 | $ sudo git clone https://github.com/microsoft/WSL2-Linux-Kernel.git /usr/src/4.19.43-microsoft-standard 59 | $ cd /usr/src/4.19.43-microsoft-standard 60 | ``` 61 | 62 | 将 Git 分支 checkout 至我们的内核版本分支(这里是 `v4.19.43`): 63 | 64 | ```shell 65 | $ sudo git checkout v4.19.43 66 | ``` 67 | 68 | 将我们当前的内核设置复制进去: 69 | 70 | ```shell 71 | $ sudo cp /proc/config.gz config.gz 72 | $ sudo gunzip config.gz 73 | $ sudo mv config .config 74 | ``` 75 | 76 | 运行 menuconfig 来选择我们想要添加的内核模块: 77 | 78 | ```shell 79 | $ sudo make menuconfig 80 | ``` 81 | 82 | 在 menuconfig 中选定我们想要添加的 USB 内核模块,以下这些模块就满足了我自己的个人需要,你可以根据你自己的需要选定不同的模块: 83 | 84 | ``` 85 | Device Drivers->USB support[*] 86 | Device Drivers->USB support->Support for Host-side USB[M] 87 | Device Drivers->USB support->Enable USB persist by default[*] 88 | Device Drivers->USB support->USB Modem (CDC ACM) support[M] 89 | Device Drivers->USB support->USB Mass Storage support[M] 90 | Device Drivers->USB support->USB/IP support[M] 91 | Device Drivers->USB support->VHCI hcd[M] 92 | Device Drivers->USB support->VHCI hcd->Number of ports per USB/IP virtual host controller(8) 93 | Device Drivers->USB support->Number of USB/IP virtual host controllers(1) 94 | Device Drivers->USB support->USB Serial Converter support[M] 95 | Device Drivers->USB support->USB Serial Converter support->USB FTDI Single Port Serial Driver[M] 96 | Device Drivers->USB support->USB Physical Layer drivers->NOP USB Transceiver Driver[M] 97 | Device Drivers->Network device support->USB Network Adapters[M] 98 | Device Drivers->Network device support->USB Network Adapters->[Deselect everything you don't care about] 99 | Device Drivers->Network device support->USB Network Adapters->Multi-purpose USB Networking Framework[M] 100 | Device Drivers->Network device support->USB Network Adapters->CDC Ethernet support (smart devices such as cable modems)[M] 101 | Device Drivers->Network device support->USB Network Adapters->Multi-purpose USB Networking Framework->Host for RNDIS and ActiveSync devices[M] 102 | ``` 103 | 104 | 开始编译内核!如果你的电脑拥有多个 CPU 核心,可以通过命令行设置 `-j <核心数量>` 来执行多核编译,大大加快编译速度。这一步骤将会持续较长时间,下面命令中我们使用的核心数量为 12: 105 | 106 | ```shell 107 | sudo make -j 12 && sudo make modules_install -j 12 && sudo make install -j 12 108 | ``` 109 | 110 | 编译完成后我们将会看到我们都安装了哪些内核模块,我自己的模块列表如下: 111 | 112 | ``` 113 | INSTALL drivers/hid/hid-generic.ko 114 | INSTALL drivers/hid/hid.ko 115 | INSTALL drivers/hid/usbhid/usbhid.ko 116 | INSTALL drivers/net/mii.ko 117 | INSTALL drivers/net/usb/cdc_ether.ko 118 | INSTALL drivers/net/usb/rndis_host.ko 119 | INSTALL drivers/net/usb/usbnet.ko 120 | INSTALL drivers/usb/class/cdc-acm.ko 121 | INSTALL drivers/usb/common/usb-common.ko 122 | INSTALL drivers/usb/core/usbcore.ko 123 | INSTALL drivers/usb/serial/ftdi_sio.ko 124 | INSTALL drivers/usb/phy/phy-generic.ko 125 | INSTALL drivers/usb/serial/usbserial.ko 126 | INSTALL drivers/usb/storage/usb-storage.ko 127 | INSTALL drivers/usb/usbip/usbip-core.ko 128 | INSTALL drivers/usb/usbip/vhci-hcd.ko 129 | DEPMOD 4.19.43-microsoft-standard 130 | ``` 131 | 132 | 编译 USBIP 工具: 133 | 134 | ```shell 135 | $ cd tools/usb/usbip 136 | $ sudo ./autogen.sh 137 | $ sudo ./configure 138 | $ sudo make install -j 12 139 | ``` 140 | 141 | 将 USBIP 工具库复制到 USBIP 可以访问到的位置: 142 | 143 | ```shell 144 | $ sudo cp libsrc/.libs/libusbip.so.0 /lib/libusbip.so.0 145 | ``` 146 | 147 | 在我们的个人文件夹 `$HOME` 下新建一个脚本叫做 `startusb.sh` 用来 modprobe 全部驱动,**确保先 modprobe `usbcore` 和 `usb-common` 这两个模块:** 148 | 149 | ```bash 150 | #!/bin/bash 151 | sudo modprobe usbcore 152 | sudo modprobe usb-common 153 | sudo modprobe hid-generic 154 | sudo modprobe hid 155 | sudo modprobe usbnet 156 | sudo modprobe cdc_ether 157 | sudo modprobe rndis_host 158 | sudo modprobe usbserial 159 | sudo modprobe usb-storage 160 | sudo modprobe cdc-acm 161 | sudo modprobe ftdi_sio 162 | sudo modprobe usbip-core 163 | sudo modprobe vhci-hcd 164 | echo $(cat /etc/resolv.conf | grep nameserver | awk '{print $2}') 165 | ``` 166 | 167 | 其中,最后一行告诉我们 Windows 主机的 IP 地址,这一 IP 地址在我们向 Windows 设备中插入 USB 设备时会起到作用。另外,如果你遇到类似 `/bin/bash^M: bad interpreter: No such file or directory` 的报错信息,可能是因为你的脚本文件的换行符是 CRLF 而非 LF。 168 | 169 | 把我们新建的脚本文件赋予可执行权限: 170 | 171 | ```shell 172 | $ sudo chmod +x startusb.sh 173 | ``` 174 | 175 | 在 Windows PowerShell 中重启 WSL: 176 | 177 | ```powershell 178 | $ wsl --shutdown 179 | ``` 180 | 181 | 再次进入 WSL,并执行刚刚的脚本: 182 | 183 | ```shell 184 | $ ./startusb.sh 185 | ``` 186 | 187 | 在 `dmesg` 中检测确保我们所有的 USB 驱动都载入成功: 188 | 189 | ```shell 190 | $ sudo dmesg 191 | ``` 192 | 193 | 好了,太棒了我的老弟,你成功给 WSL 添加了 USB 支持。 194 | 195 | ## 使用 USBIP-Win 和 Linux 侧的 USBIP 196 | 197 | 首先在 Windows 侧,在 PowerShell 中执行下面的命令将列出 Windows 设备上插入的 USB 设备列表: 198 | 199 | ```powershell 200 | $ usbip list -l 201 | ``` 202 | 203 | 我想要绑定的 USB 设备的 `busid` 是 1-220,我们可以用下面的命令将 1-220 设备绑定给 USBIP: 204 | 205 | ```powershell 206 | $ usbip bind --busid=1-220 207 | ``` 208 | 209 | 之后我们启动 USBIP 守护程序(USBIP daemon): 210 | 211 | ```powershell 212 | # 开启 debug 模式与否都可以,用 --debug 开启 debug 模式可以查看更多日志信息 213 | $ usbipd --debug 214 | ``` 215 | 216 | 接下来,在 WSL 中,使用下面的命令来查看可供使用的 USB 设备列表,其中的 `172.30.64.1` 即为前面我们拿到的 Windows 主机的 IP 地址: 217 | 218 | ```shell 219 | $ sudo usbip list --remote=172.30.64.1 220 | ``` 221 | 222 | 我们想要连接的 USB 设备的 `busid` 依旧是 1-220,用以下命令连接: 223 | 224 | ```shell 225 | $ sudo usbip attach --remote=172.30.64.1 --busid=1-220 226 | ``` 227 | 228 | 经过上面的操作,我们就应该在 WSL 侧成功连接 Windows 上插入的 USB 设备了。用 `dmesg` 检测一切正常工作: 229 | 230 | ```shell 231 | $ sudo dmesg 232 | ``` 233 | 234 | ## FAQ 235 | 236 | ### 本文的方法有什么限制? 237 | 238 | 以上介绍的方法: 239 | 240 | - 是无法直接绑定组合设备的子设备的,我们如果连接组合设备的话,需要直接绑定主设备; 241 | - 是无法连接 USB Hub 的,各种 USB 集线器都无法使用; 242 | 243 | ### 我如何重新在 Windows 上访问我的 USB 设备? 244 | 245 | 如果我们要断开 WSL 侧的 USB 设备绑定(来重新在 Windows 侧访问 USB 设备),则需要: 246 | 247 | - 在 Windows 中进入「设备管理器」; 248 | - 找到系统设备下的 USB/IP STUB 设备; 249 | - 右键点击,选择「更新驱动」; 250 | - 点击「浏览我的电脑以查找驱动程序」; 251 | - 选择「让我从计算机上的可用驱动程序列表中选取」; 252 | - 最后选择我们之前在 Windows 上连接 USB 设备用的驱动; 253 | 254 | ### USBIP-Win 无法连接我的 USB 设备怎么办? 255 | 256 | 有的时候 USBIP 在 Windows 上面无法连接一个 USB 设备,这时候我们可以尝试将这一设备更换一个插口,重新用 USBIP 连接绑定。绑定之后我们可以将 USB 设备插回原先的插口,因为「USBIP 的绑定关系」在连接、断开的生命周期中是持久化的。 257 | 258 | ### USBIP-Win 不显示我的 USB 组合设备怎么办? 259 | 260 | 我之前出现过这样的情况:在 Windows 侧无法在 USBIP 的列表中显示组合设备,此时我们可以用下面的方法解决: 261 | 262 | - 下载安装 [Zadig](https://zadig.akeo.ie/); 263 | - 进入设置并选择「显示全部设备 List All Devices」,并取消选择「忽略 Hub 或组合设备主设备 Ignore Hubs or Composite Parents」; 264 | - 选择我们连接的 USB 组合设备; 265 | - 安装 libusbK 驱动; 266 | - 之后我们就可以在 USBIP 列表中看到我们的设备了; 267 | 268 | 我也不知道这个情况具体是什么导致的,可能是因为 Windows 默认的 USB 驱动拿到的 USB 组合设备无法让 USBIP-Win 访问,但是安装 libusbK 可以解决这个问题。USBIP-Win 本质上会用 USBIP-Win 的驱动覆盖掉 libusbK 驱动,所以我们可以选择任何 USB 驱动(而非 libusbK 单独一个驱动)。不论是什么原因,这个问题都是 USBIP-Win 需要解决的 bug。 269 | -------------------------------------------------------------------------------- /docs/5-others/5-0-intro.md: -------------------------------------------------------------------------------- 1 | # 后记 2 | 3 | ![](https://cdn.spencer.felinae98.cn/github/2020/09/200902_221105.png) 4 | 5 | ::: callout 🍓 结束语 6 | Dev on Windows with WSL 到这里基本介绍完成,我们将使用 WSL 中可能涉及到的一些问题、配置方法都进行了详细的介绍,对 WSL 的工作原理也进行了大致的讲解。本章主要介绍一些使用 WSL 进行开发的体验,文档涉及到的参考资料等内容。 7 | ::: 8 | 9 | ## 体验 10 | 11 | 事实上,从一个学生的角度来说,WSL 完完全全是可以胜任我们日常学习开发工作的。无论是在 ACM 中使用 C/C++ 编写算法,还是在学校课程中在 Linux 环境下进行软件开发,WSL 都 more than enough。特别是在 VS Code 能够借助于 Remote 远程开发组件包直接连接到 WSL 环境上之后,在 Windows 上用 VS Code 在原生 Windows 环境下开发和连接到 WSL 环境下开发的体验是几乎完全一致的。 12 | 13 | WSL 为我们在 Windows 系统中提供了一个近乎原生的 Linux 环境,让我们能在不折腾、尽量少折腾的情况下,运行完整的 Linux 发行版。这不论是对 Linux 开发者还是 Windows 开发者来说,都是一大利好。 14 | 15 | ## 参与贡献 16 | 17 | 2018 年的时候,Dev on Windows with WSL 仅仅是我一个个人的项目,而且仅仅是一篇文章。到今天,我和几位 Contributors 已经撰写了超过 5 个部分的 20 章内容。现在,希望有经验的同学前来与我共同完善本项目,不论是增加一门语言在 WSL 下的配置方法,还是增加一些高级的 WSL 使用操作,任何内容都是欢迎的。 18 | 19 | 在你着手准备给这个项目提交一些新内容前,请务必阅读「贡献」相关内容说明与规范:[贡献指南](/contributing/)。 20 | 21 | ## 参考资料 22 | 23 | 感谢前辈总结的经验,没有以下这些文章我不可能总结出这些东西: 24 | 25 | - [lloydstubber/my-wsl-setup](https://github.com/lloydstubber/my-wsl-setup) 26 | - [Voronoff/wsl_setup.md](https://gist.github.com/Voronoff/059c50f9fd354386c305c55af1f3a61f#install-and-set-up-python-to-work-with-vscode-and-wsl) 27 | - [Running VS Code for Linux in WSL #2760](https://github.com/Microsoft/WSL/issues/2760) 28 | - [sirredbeard/Awesome-WSL](https://github.com/sirredbeard/Awesome-WSL) 29 | - [ethanhs/WSL-Programs](https://github.com/ethanhs/WSL-Programs) 30 | - [如何在 Windows Subsystem for Linux (WSL) 上运行 Linux GUI 软件]() 31 | - [在 WSL 下启动 VS Code](https://zhuanlan.zhihu.com/p/33226830) 32 | - [Setting proxy in git](https://gist.github.com/laispace/666dd7b27e9116faece6) 33 | -------------------------------------------------------------------------------- /docs/5-others/5-1-donating.md: -------------------------------------------------------------------------------- 1 | # 捐赠 2 | 3 | :::callout 🧧 投喂指南 4 | 请给我打钱!~~(不是)~~ 5 | ::: 6 | 7 | ## Star 8 | 9 | 如果你觉得本文档干货满满,对你使用 WSL 有着巨大的帮助,请帮我[在 GitHub 上](https://github.com/spencerwooo/dowww)为此仓库点个 Star!感谢 (。・∀・)ノ゙ 10 | 11 | ## 微信、支付宝 12 | 13 | 对于中国的同学,欢迎直接通过微信和支付宝对我本人进行投喂! 14 | 15 | | 微信支付 | 支付宝支付 | 16 | | :--------------------------------------------------------------------------------------: | :------------------------------------------------------------------------------------------: | 17 | | 微信支付 | 支付宝支付 | 18 | 19 | ## 爱发电 20 | 21 | 各位也可以利用「爱发电」来对我进行支持! 22 | 23 | [![爱发电](https://img.shields.io/badge/%E7%88%B1%E5%8F%91%E7%94%B5-@SpencerWoo-946ce6?labelColor=24292e&style=for-the-badge)](https://afdian.net/@spencerwoo) 24 | 25 | ## Open Collective 26 | 27 | Thank you for your interest! Dev on Windows with WSL is a joint effort. We currently support funding via Open Collective. 28 | 29 | | Backers | Sponsors | 30 | | :-------------------------------------------------------------------------------------------------------------------------------------: | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------: | 31 | | Support this project by becoming a backer. Thank you to all our backers! 🙏 [[Become a backer](https://opencollective.com/dowww#backer)] | Support this project by becoming a sponsor. Your logo will show up here with a link to your website. 🌏 [[Become a sponsor](https://opencollective.com/dowww#sponsor)] | 32 | | | | 33 | 34 | ## 其他 35 | 36 | 谈钱伤感情,如果你觉得你也可以为 Dev on Windows with WSL 添砖加瓦,欢迎来 PR,请阅读:[贡献指南](/contributing/)。 37 | -------------------------------------------------------------------------------- /docs/README.md: -------------------------------------------------------------------------------- 1 | --- 2 | home: true 3 | heroText: Dev on Windows with WSL 4 | heroImage: /hero.png 5 | actionText: 入坑指南 → 6 | actionLink: /1-preparations/1-0-intro 7 | features: 8 | - title: 🍳 9 | details: 在 Windows 下,解决那令人烦恼的非 Unix 环境。 10 | - title: 💡 11 | details: 原汁原味的 Unix 开发环境,又有着 Windows 强大的生产力。 12 | - title: 🎉 13 | details: 与 Visual Studio Code 联合,打造最为健壮的 Windows 开发环境。 14 | footer: 2018 - 2020 ©Spencer Woo. Released under the CC BY-NC-SA 4.0 International License. 15 | --- 16 | -------------------------------------------------------------------------------- /docs/changelog/README.md: -------------------------------------------------------------------------------- 1 | --- 2 | sidebar: auto 3 | --- 4 | 5 | # 更新日志 6 | 7 | ## [1.3.1] - 2020-9-3 8 | 9 | ### Features 10 | 11 | 小版本更新,将所有 outdated 信息进行了更新: 12 | 13 | - 更新了 WSL 2 的 Windows 10 系统版本要求。 14 | - 更新了 Fluent Terminal 的安装方式,新增了 Fluent Terminal 在 Microsoft Store 微软商店的下载链接。 15 | - 新增了 WSL 2 设置 Git 代理、ssh 登录 GitHub 代理以及自动化配置脚本的方法。 16 | - 新增了 WSL 使用 GPG 对 Git commit 进行签名的方法和问题排查方法。 17 | - 拆分第二章部分内容,将 CLI 命令行工具单独拆为一章。 18 | - 将 Python 环境管理工具由 `pipenv` 改为更加优秀的 Poetry。 19 | - 将 Node.js 版本管理工具由 `nvm` 改为更为先进的 `n`,重构了 Node.js 部分的文档。 20 | 21 | 为了提升体验: 22 | 23 | - 将图片存储更改为阿里云香港 OSS。 24 | - 将图片 CDN 更改为阿里云全球 CDN。 25 | 26 | ## [1.3.0] - 2020-9-2 27 | 28 | ### Features 29 | 30 | - 移除了全部无用的 CSS 代码,使用 `vuepress-theme-book` 构建文档。 31 | - 抛弃了以版本号更新文档的策略,使用滚动更新策略更新文档。 32 | - 添加自定义滚动条。 33 | - 新增了 WSL 2 中连接 USB 设备的方法。 34 | 35 | ### Bug fixes 36 | 37 | - 更新了全部依赖。 38 | - 移除了脚注构建插件,使用 `markdown-it-footnote` 插件构建脚注。 39 | 40 | ## [1.2.0] - 2020-7-25 41 | 42 | 将从 1.1 版本开始陆续新增的 PR 和改动进行了整合,达到 1.2 版本。 43 | 44 | ### Features 45 | 46 | - 新增了关于 WSL 2 配置文件 `.wslconfig` 的说明:[配置 WSL 2 全局选项](https://dowww.spencerwoo.com/4-advanced/4-3-wslconfig.html)。 47 | - 新增了关于重启 WSL 的提示和方法(为了让 WSL 重新挂载 `/mnt` 下的盘符):[🔁 重启 WSL 环境](https://dowww.spencerwoo.com/2-cli/2-2-shell.html#%E8%A7%A3%E5%86%B3-ls-%E5%91%BD%E4%BB%A4%E8%83%8C%E6%99%AF%E8%89%B2%E9%97%AE%E9%A2%98)。 48 | - 新增了全新的 README 头图 Banner。 49 | - 重新设计了全新的 Dev on Win with WSL 图标。 50 | 51 | ### Bug fixes 52 | 53 | - 修改了 APT 包管理工具的称谓(`Ubuntu` -> `Debian`)。 54 | - 修改了有关 Windows Hyper-V 虚拟机的具体称谓(`已安装的虚拟机平台` -> `虚拟机平台`)。 55 | - 修复了在 Netlify 平台部署时路径大小写不匹配导致无法通过侧边栏导航的问题,错误复盘:[Netlify or VuePress:大型悬疑推理篇之——报错到底是谁的锅?](https://blog.spencerwoo.com/2020/05/debugging-netlify-static-site/)。 56 | - 修改若干错别字([#59](https://github.com/spencerwooo/dowww/pull/59))。 57 | 58 | ## [1.1.0] - 2020-4-15 59 | 60 | 将从 1.0 版本开始陆续新增的 PR 和改动进行了整合,达到 1.1 版本。 61 | 62 | ### Features 63 | 64 | - 新增了 WSL 2 IP 地址需要手动修改的注意事项:[WSL 2 中的网络访问问题](https://dowww.spencerwoo.com/2-CLI/2-3-Others.html#wsl-2-%E4%B8%AD%E7%9A%84%E7%BD%91%E7%BB%9C%E8%AE%BF%E9%97%AE%E9%97%AE%E9%A2%98)(Issue #49)。 65 | - 新增了 WSL 2 虚拟磁盘空间优化的建议:[优化 WSL 2 虚拟磁盘占用空间](https://dowww.spencerwoo.com/2-CLI/2-3-Others.html#%E4%BC%98%E5%8C%96-wsl-2-%E8%99%9A%E6%8B%9F%E7%A3%81%E7%9B%98%E5%8D%A0%E7%94%A8%E7%A9%BA%E9%97%B4)(PR #45)。 66 | - 新增了 GitHub Sponsors 和爱发电链接,~~请给我打钱!~~(不是)。 67 | 68 | ### Bugs and fixes 69 | 70 | - 修复了 GitHub 主页 README 支付宝二维码失效的问题。 71 | - 修复了 Hyper 终端介绍链接失效的问题:[终端 Terminal - Hyper](https://dowww.spencerwoo.com/2-CLI/2-1-Terminal.html#hyper)。 72 | 73 | ## [1.0.0] - 2020-1-7 🎉🎉🎉 74 | 75 | 我们终于迎来了 v1.0.0 的里程碑!文档重构完成,重大更新。历经一周有余,文档几乎全新开始。感谢大家的期待 (✿◡‿◡) 76 | 77 | ### Features 78 | 79 | - 将 VuePress 更新到 1.0 版本,增加了自定义容器和 Medium 风格图片显示。 80 | - 全新的文档色彩方案和设计,使用 [Pantone 2020 Color of the Year](https://time.com/5744039/pantone-color-of-the-year-2020/) 作为强调色。 81 | - 使用更为清晰的侧边栏导航,抛弃使用 navbar 导航栏进行文档导航。 82 | - 在 VuePress 默认主题的基础之上,实现了版本切换、版本归档(`yarn bump-version `)。 83 | - 全新设计了每个部分的「题图」banner,统一设计风格。 84 | - 新增了 WSL 序论、安装部分: 85 | - 新增了 [关于 WSL、WSL 2 架构设计的介绍](https://dowww.spencerwoo.com/1-Preparations/1-0-Intro.html)。 86 | - 新增了 [关于 WSL、WSL 2 安装的方法介绍](https://dowww.spencerwoo.com/1-Preparations/1-1-Installation.html)。 87 | - 新增了 CLI 命令行环境中其他工具的配置: 88 | - 新增了远程登录的配置:[SSH、Mosh 工具的使用](https://dowww.spencerwoo.com/2-CLI/2-3-Others.html#%E8%BF%9C%E7%A8%8B%E7%99%BB%E5%BD%95)。 89 | - 新增了 [Windows 和 WSL 之间互相文件访问、互相执行命令的方法](https://dowww.spencerwoo.com/2-CLI/2-3-Others.html#windows-%E5%92%8C-wsl-%E4%B9%8B%E9%97%B4%E4%BA%92%E7%9B%B8%E8%AE%BF%E9%97%AE)。 90 | 91 | ### Changes 92 | 93 | - 文档部署到 Netlify,加速中国用户访问。 94 | - 修改了「工具链」部分,将用词切换为「命令行环境 CLI」,更为恰当。 95 | - 修改了 [终端工具 Terminal 环境的配置](https://dowww.spencerwoo.com/2-CLI/2-1-Terminal.html)。 96 | - 修改了 [命令行工具 Shell 环境的配置](https://dowww.spencerwoo.com/2-CLI/2-2-Shell.html)。 97 | - 更新了 VS Code 和 WSL 配合开发的方法: 98 | - 更新了 [VS Code 远程开发组件包的配置手段](https://dowww.spencerwoo.com/3-VSCode/3-1-Remote-Dev.html)。 99 | - 更新了 [VS Code 使用 Code Runner 一键执行代码文件的配置](https://dowww.spencerwoo.com/3-VSCode/3-2-Code-Runner.html)。 100 | - 更新了 [VS Code 连接 WSL 环境开发 Python 项目的配置方法](https://dowww.spencerwoo.com/3-VSCode/3-3-Python.html)。 101 | - 更新了 [VS Code 连接 WSL 环境开发 C、C++ 项目的配置方法](https://dowww.spencerwoo.com/3-VSCode/3-4-C_Cpp.html)。 102 | - 更新了 [VS Code 连接 WSL 环境开发 Node.js 项目的配置方法](https://dowww.spencerwoo.com/3-VSCode/3-6-NodeJS.html)。 103 | - 更新了 [VS Code 连接 WSL 环境撰写 LaTeX 文档的配置方法](https://dowww.spencerwoo.com/3-VSCode/3-5-LaTeX.html)。 104 | - 修改了 [体验、参考、后记](https://dowww.spencerwoo.com/5-Experience/5-0-Intro.html)。 105 | 106 | ### Removed / Fixed 107 | 108 | 删除的部分大多数为 Remote 开发组件出现之前,将 VS Code 通过「魔改」的方法与 WSL 环境连接的一些奇淫巧计。不一一赘述。 109 | 110 | --- 111 | 112 |
113 | Dev on Windows with WSL v0.9 旧版本更新日志 114 | 115 | ## [0.2.0] - 2019-12-30 116 | 117 | - 开始重构。 118 | 119 | ## [0.1.1] - 2019-5-13 120 | 121 | - 新增了 [Remote-WSL](https://dowww.spencerwoo.com/3-VSCode/#remote-wsl-%E6%8F%92%E4%BB%B6) 更加详细的介绍。 122 | 123 | ## [0.1.0] - 2019-5-12 124 | 125 | - 新增了对 [Windows Terminal](https://dowww.spencerwoo.com/2-Toolchain/2-1-TerminalEnv.html#windows-terminal) 和 [Fluent Terminal](https://dowww.spencerwoo.com/2-Toolchain/2-1-TerminalEnv.html#fluent-terminal) 的介绍。 126 | - 新增了 VS Code [Remote-WSL 支持的介绍](https://dowww.spencerwoo.com/3-VSCode/3-0-Terminal.html#remote-wsl-%E6%8F%92%E4%BB%B6)。 127 | - 新增了 Arch Linux 上架微软官方商店的介绍。 128 | 129 | ## [0.0.7] - 2019-3-3 130 | 131 | ### Add 132 | 133 | - 新增了 [.NET Core 的配置](https://dowww.spencerwoo.com/3-VSCode/3-7-DotNetCore.html),[PR #21](https://github.com/spencerwooo/dowww/pull/21)。 134 | 135 | ## [0.0.6] - 2019-1-24 136 | 137 | ### Add 138 | 139 | - 新增了进阶操作 - [LxRunOffline 的使用配置](https://dowww.spencerwoo.com/4-Advanced/4-2-LxRunOffline.html),[PR #18](https://github.com/spencerwooo/dowww/pull/18)。 140 | - 新增了 [对 Windows 默认终端模拟器的配置](https://dowww.spencerwoo.com/2-Toolchain/2-1-TerminalEnv.html#%E9%BB%98%E8%AE%A4%E7%9A%84-wsl-%E7%BB%88%E7%AB%AF%E6%A8%A1%E6%8B%9F%E5%99%A8)。 141 | 142 | ## [0.0.5] - 2019-1-3 143 | 144 | ### Changed 145 | 146 | - 更新了 [VSCode 参考配置内容](https://dowww.spencerwoo.com/3-VSCode/#%E5%8F%82%E8%80%83%E9%85%8D%E7%BD%AE)。 147 | - 完善了 C/Cpp 部分的环境配置指南。 148 | - 更新了 C/Cpp 部分的配图。 149 | 150 | ## [0.0.4] - 2018-12-30 151 | 152 | ### Add 153 | 154 | - 新增了 [ESLint 的配置](https://dowww.spencerwoo.com/3-VSCode/3-6-NodeJS.html#配置-eslint)。 155 | 156 | ### Fixed 157 | - 完善了 [Node.js 调试的配置](https://dowww.spencerwoo.com/3-VSCode/3-6-NodeJS.html#调试-node-js-程序)。 158 | - 完善了各部分作者的信息。 159 | 160 | ## [0.0.3] - 2018-12-26 161 | 162 | ### Add 163 | 164 | - 新增了 [`wslgit` 的加速方法指南](https://dowww.spencerwoo.com/3-VSCode/3-1-Git.html#提升-git-on-wsl-的性能)。 165 | - 新增了 [ArchWSL 入教指南](https://dowww.spencerwoo.com/3-VSCode/3-1-Git.html#提升-git-on-wsl-的性能)。 166 | 167 | ## [0.0.2] - 2018-12-22 168 | 169 | ### Add 170 | 171 | - 增加了 VSCode 中 Node.js 环境的配置,[PR #11](https://github.com/spencerwooo/dowww/pull/11)。 172 | 173 | ## [0.0.1] - 2018-12-13 174 | 175 | ### Add 176 | 177 | - 新增了 Terminus 终端的推荐。 178 | - 增加了 VSCode 中 LaTeX 环境的配置,[PR #10](https://github.com/spencerwooo/dowww/pull/10)。 179 | 180 | 181 |
182 | -------------------------------------------------------------------------------- /docs/contributing/README.md: -------------------------------------------------------------------------------- 1 | --- 2 | sidebar: auto 3 | --- 4 | 5 | # 贡献指南 6 | 7 | > 👍🎉💖 Hi,感谢你对这个项目感兴趣。💖 🎉 👍 8 | 9 | 下面是一些内容、规范等等,来帮助你更好的对这个项目进行贡献。包括但不限于:如何增加一篇新文章、如何对文章内容进行更新更正、文章的行文规范等等。当然,值得注意的是:下面这些内容仅仅是规范而非规则。欢迎来一起完善这个项目! 10 | 11 | ## 在贡献之前,我应该了解些什么? 12 | 13 | ### Code of Conduct 14 | 15 | 请先看看我们的行为准则 - [Code of Conduct](https://github.com/spencerwooo/dowww/blob/master/.github/CODE_OF_CONDUCT.md),并保证贡献内容符合这些规范。如果发现项目中出现了不符合这些规则的内容,欢迎反馈内容。 16 | 17 | ### 开发路线 18 | 19 | 目前 WSL 在不断更新迭代,那么本项目会尽量的跟随 WSL 的开发进行更新。包括但不限于:WSL 发行版下载方式的更新、终端的推荐更新、VSCode 本身的更新、VSCode 插件的更新等等。希望有一天,WSL 不再需要更多配置就可以无缝完美使用。 20 | 21 | ## 我应该从哪里下手? 22 | 23 | ### 反馈问题 24 | 25 | 最简单的方法就是,当你在配置过程中,出现了任何的问题,请直接去 [Issue 区](https://github.com/spencerwooo/dowww/issues/new/choose) 进行反馈。我会尽力帮助你解决问题。 26 | 27 | ### 新增内容 28 | 29 | 目前最大的障碍是针对各大语言在 WSL 下的适配工作。因此希望如果你在 WSL 中使用一个具体语言十分舒爽(比如 Ruby、Typescript、Golang、Rust、Scala 等等),可以在这里分享你的这门语言的开发环境配置过程。 30 | 31 | 如果你确定希望对本项目增加「具体语言相关的开发环境配置」教程,那么请按照下面的步骤进行贡献: 32 | 33 | ## 熟悉项目 34 | 35 | 由于项目是由 VuePress 构建,部署并托管在 Vercel 上的静态网页,因此希望你在对项目做任何改变前,先熟悉项目的结构和构建方式: 36 | 37 | ### 构建方式 38 | 39 | 1. 在 GitHub 上 fork 一份本项目到你自己的 GitHub 账号下 40 | 2. 将项目 `clone` 到你的本机: 41 | ```bash 42 | $ git clone https://github.com/{YOUR_USER_NAME}/dowww.git 43 | ``` 44 | 3. 安装 `Node.js` 和其包管理 `yarn`: 45 | - 如果你已经开始(或很想)使用 WSL 进行开发,请直接用你所选择的 Linux 发行版的包管理安装上面所述的内容。更多内容请参考 - [Dev on Windows with WSL | Node.js](https://dowww.spencerwoo.com/3-VSCode/3-6-NodeJS.html) 46 | - 如果你依旧在使用 Windows 侧的开发环境,推荐使用 Windows 的「包管理」工具 - [`scoop`](https://github.com/lukesampson/scoop): 47 | - 安装 `scoop`,PowerShell 中运行: 48 | ```powershell 49 | $ iex (new-object net.webclient).downloadstring('https://get.scoop.sh') 50 | ``` 51 | - 安装 `nvm`: 52 | ```powershell 53 | $ scoop install nvm 54 | ``` 55 | - 安装最新稳定版本的 `Node.js`: 56 | ```powershell 57 | $ nvm install latest 58 | ``` 59 | - 安装 `yarn`: 60 | ```powershell 61 | $ scoop install yarn 62 | ``` 63 | - 如果你并没有在用 Windows ... 那你怎么看到这个项目了 ∑( 口 || 64 | 4. 安装相关依赖: 65 | ```bash 66 | $ yarn install 67 | ``` 68 | 5. 本地预览: 69 | ```bash 70 | $ yarn docs:dev 71 | ``` 72 | 73 | ### 结构 74 | 75 | - `/docs` 文件夹下存放文档 Markdown 文件。文件夹名、文件名等即表示相应的文档内容; 76 | - `/docs/.vuepress` 文件夹下为 VuePress 的配置文件,请不要非必要情况下任意修改; 77 | 78 | ## 明确行文准则 79 | 80 | > ❗ 请先明确,为了整个文档统一的格式,新增内容必须严格符合下文的行文规范。不符合的内容将会被要求修改。请谅解。 81 | 82 | 文章行文规范严格遵守 [中文文案排版指北](https://github.com/sparanoid/chinese-copywriting-guidelines) 中的规范准则。 83 | 84 | 详细地: 85 | 86 | - 空格 87 | - 行文时,请在中文与英文、中文与数字、英文与数字之间增加空格; 88 | - 一段文字中有超链接的部分,在超链接的前后使用空格; 89 | - 英文前后接全角标点符号或者表示单位的角标符号时,不需要加空格; 90 | - 对于有特殊用法的专有名词,如 WSL、Windows 10、Ubuntu 20.04 等,英文和数字之间是否空格以官方标准为准; 91 | - 每段文字的开头不需要空两格; 92 | - 标点符号 93 | - **不要重复使用标点符号,尤其是在表达强烈情感的时候**; 94 | - 引号请使用直角引号「」,而不是弯引号 “”; 95 | - 一般情况下,一个中文句子中出现了英文部分,仍然使用中文标点,即全角符号; 96 | - 如果引用一段完整的英文句子,或是出现在专有名词中的标点,则不需要更改标点符号; 97 | 98 | 我本人会对新增内容进行审核,请务必遵守上面的行文准则,当然,也要保证文章内容清晰规范。 99 | 100 | ## 增加一门语言的环境配置指南 101 | 102 | 请在 `/docs/3-VSCode` 下新建一个文件,命名为 `3-*-***.md`。其中 `*` 表示第几篇文章,`***` 表示语言的名称,比如:`3-8-AmazingLanguage.md`。请在这个文件下进行文章的撰写。 103 | 104 | 撰写完成之后,请不要忘记在 `docs/.vuepress/config.js` 中的相应位置增加你撰写的文档路径,比如: 105 | 106 | ```javascript 107 | module.exports = [ 108 | // …… 109 | { 110 | title: 'Visual Studio Code', 111 | collapsable: false, 112 | children: [ 113 | // …… 114 | '3-VSCode/3-7-DotNetCore', 115 | // 你应该下面添加 3-8-AmazingLanguage 的路径 116 | '3-VSCode/3-8-AmazingLanguage' 117 | ] 118 | } 119 | // …… 120 | ] 121 | ``` 122 | 123 | 之后,在本地构建预览: 124 | 125 | ```bash 126 | $ yarn docs:dev 127 | ``` 128 | 129 | 这样就可以在本地预览你的文章了。 130 | 131 | 一般情况下,到这里就可以了,在本地写好文章之后将新增内容 push 到 GitHub 并提交 PR,我就会将你的新增内容更新至文档中。 132 | 133 | ## 其他 134 | 135 | 其他内容的增加和更改,请直接修改你想要修改的地方,我会根据更改选择是否 merge 你的内容。 136 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "dev-on-win-with-wsl", 3 | "version": "1.1.0", 4 | "description": "Dev on Windows with WSL", 5 | "scripts": { 6 | "docs:dev": "vuepress dev docs", 7 | "docs:build": "vuepress build docs" 8 | }, 9 | "repository": "https://github.com/spencerwooo/dowww", 10 | "author": "spencerwooo", 11 | "license": "CC-BY-NC-SA-4.0", 12 | "dependencies": { 13 | "@vuepress/plugin-back-to-top": "^1.2.0", 14 | "@vuepress/plugin-google-analytics": "^1.2.0", 15 | "@vuepress/plugin-medium-zoom": "^1.2.0", 16 | "markdown-it-footnote": "^3.0.2", 17 | "vuepress": "^1.5.4", 18 | "vuepress-plugin-container": "^2.1.2", 19 | "vuepress-theme-book": "^0.0.4" 20 | } 21 | } 22 | --------------------------------------------------------------------------------