Mẹo vặt và các phím tắt thường sử dụng trong Visual Studio

1. Các hotkey cơ bản
Sử dụng hotkey nhiều sẽ khiến người ngoài thấy bạn code có vẻ prohơn. Bạn cũng đỡ tốn công di chuyển chuột hơn
F5 – Ctrl F5 – Debug
Capture
Sau khi code xong, thay vì phải di chuyển chuột lên, bấm nút Start này để chạy chương trình, hãy bấm F5 (Cái này cơ bản quá rồi, bỏ vào cho những ai chưa biết thôi).
Trường hợp ta đặt breakpoint để bug, hãy bấm Ctrl + F5 để chạy chế độ no debug, bỏ qua breakpoint nhé.
F12 – See implementation
7e283a9a8eb79982555108a368bd55c1
Khi đọc code, ta muốn xem 1 class hay method viết như thế nào, hãy sử dụng phím F12. Nếu bạn cài Reshaper hoặc VS Productivity Tool, bạn có thể giữ Ctrl và click vào class để có hiệu quả tương tự
Ctrl K + Ctrl D – Format Code
dc4142a79110417ebf8e261dad97900a
Code xong, nếu thấy code xiêu vẹo thì hãy dùng Ctrl K + Ctrl D để tự động format lại. Nếu code không tự động format thì code thể là bạn quên đóng () hoặc {} nhé.
Ctrl K + Ctrl C – Comment Code / Ctrl K + Ctril U – Uncomment Code
Để comment code, ta thường comment bằng tay dùng 2 nút này trên toolbar
Capture
Ta có thể áp dụng hotkey để comment/uncomment nhanh nhé
2579938a9039a675c6e87060f8601ec3
F9 – Set breakpoint
Capture
Để đặt breakpoint ta thường phải click vào thanh dọc bên trái. Sao phải làm vậy khi ta có thể bấm F9 là xong, nhanh gọn lẹ.
F10/F11 – Step over/Step into
Capture2
Khi debug, thay vì bấm 2 nút trên toolbar này, dùng hotkey F10 và F11 sẽ tiết kiệm thời gian hơn.
2. Sử dụng snippet
Trong quá trình code, có những đoạn code ta phải lặp lại nhiều lần. Visual Studio đã tích hợp sẵn những đoạn code này, gọi là snippet, tiết kiệm khá nhiều thời gian (Netbeans hay Eclipse cũng có).
Cách sử dụng snippet: Gõ ra rồi Tab là xong
ctor – Tạo contrucstor cho class
4ba583d625ea477f4e2a6e6d487688e3
prop – Tạo properties – Gõ xong nhớ Tab Tab
7ff4683654699f984f00a56826df2b50
Hồi mới chuyển từ java qua C#, mình thắc mắc sao Visual Studio xịn vậy mà ko có cái tạo getter, setter cho field. Sau này mới biết getter, setter được hiện thực bằng property nhé.
cw – Console.WriteLine() – Cho mấy bạn hay code Console
60575fb39be324ef353e21227d2f0cee
for/forr – Tự tạo hàm for
89d840b75c4ddc6cde266edeb4046d48
Các bạn có thể thử với một số snippet khác như if/else, do/while, switch, try nhé.

3. Một số mẹo nhỏ khác
Giữ Ctrl + Nhấp chuột – Box Selection
Bạn từng gặp cảnh phải sửa nhiều dòng code 1 lúc chưa? Sử dụng Box Selection sẽ giúp việc này trở nên vô cùng dễ dàng
b17623192b9f9f6779f115aac3c325ed
Ctrl + Shift + V – Nghịch clipboard
Trong đời code, chắc ai cũng đã từng phải copy paste. Đã bao giờ bạn từng copy A, sau đó lỡ bấm nhầm copy B, C, sau đó phải tìm A để copy lại chưa? Visual Studio lưu lại tận 20 lần copy gần nhất trong clipboard, ta có thể dùng Ctrl + Shift + V để tìm đoạn clipboard cần paste.
fe4e0a1e8e09f813b9a448dff49198d4
Ctrl + , – Navigate File/Ctrl + ; : Search
987c49fecb17dd097570d1193da5878f
Thay vì Ctrl + F, hãy sử dụng 2 chức năng này, hữu hiệu hơn nhiều đấy
Paste JSON/XML as class
Mình không biết chức năng này được Microsoft thêm vào Visual Studio từ khi nào, nhưng mà nó khá hữu dụng. Hiện nay, khi nhà nhà dùng Rest API, người người dùng Rest API, JSON và XML trở thành hotboy hotgirl. Trong C#, ta không thể dùng thẳng mà phải convert JSON sang object. Chức năng này cho phép ta tạo class để convert json sang.
VD mình có chuỗi JSON sau (Lấy từ facebook graph api):
1
2
3
4
5
6
7
8
9
10
11
12
{
  "id": "1000017928484843",
  "first_name": "Mạnh Chức",
  "gender": "male",
  "last_name": "Lê",
  "locale": "vi_VN",
  "name": "Lê Mạnh Chức",
  "timezone": 7,
  "updated_time": "2017-02-06T12:56:23+0000",
  "verified": true
}
Chỉ cần dùng paste JSON as class, dán đoạn JSON này vào, sau đó chọn uppercase các kí tự đầu tiên của các field là xong. VS khá thông minh, có thể xác định một số trường là datetime hoặc boolean, còn mặc định là string.
Wn6mLGm6Hh
Bài viết load hơi nặng vì có nhiều ảnh động, các bạn thông cảm. Chúc các bạn ngày càng thành thục với IDE của mình. Mấy trò hotkey snippet v…v này rất phù hợp để “chứng tỏ trình độ” hoặc “hù mem mới” nhé.
1. Basic hotkeys
Using hotkeys a lot makes your coding look moreproto others. You also move the mouse a lot less
F5 - Ctrl F5- Debug
Capture
When you finish coding, instead of moving the mouse up and clicking the Start button to run the program, just press F5 (very basic, added for anyone who does not know it yet).
If you set breakpoints to debug, press Ctrl + F5 to run without debugging and skip the breakpoints.
F12 - See implementation
7e283a9a8eb79982555108a368bd55c1
When reading code and you want to see how a class or method is written, press F12. If you have ReSharper or VS Productivity Tool installed, you can also hold Ctrl and click the class for the same result
Ctrl K + Ctrl D - Format Code
dc4142a79110417ebf8e261dad97900a
When the code looks messy, use Ctrl K + Ctrl D to format it automatically. If it does not format, you probably forgot to close a () or {}.
Ctrl K + Ctrl C - Comment Code /Ctrl K + Ctrl U - Uncomment Code
To comment code, we usually do it by hand with these 2 buttons on the toolbar
Capture
Use the hotkeys to comment and uncomment quickly
2579938a9039a675c6e87060f8601ec3
F9 - Set breakpoint
Capture
To set a breakpoint you usually click the vertical bar on the left. Why do that when pressing F9 is quicker?
F10/F11 - Step over/Step into
Capture2
When debugging, using F10 and F11 instead of these 2 toolbar buttons saves time.
2. Use snippets
While coding, some pieces of code have to be written again and again. Visual Studio has them built in as snippets, which saves a lot of time (NetBeans and Eclipse have them too).
How to use a snippet: Type it, then pressTab and you are done
ctor - Create a constructor for the class
4ba583d625ea477f4e2a6e6d487688e3
prop - Create a property. Press Tab Tab after typing
7ff4683654699f984f00a56826df2b50
When I moved from Java to C#, I wondered why such a good IDE as Visual Studio had no generator for the getter and setter of a field. Later I learned that in C# getters and setters are written as aproperty instead.
cw - Console.WriteLine(), for those who write console apps a lot
60575fb39be324ef353e21227d2f0cee
for/forr - Create a for loop
89d840b75c4ddc6cde266edeb4046d48
You can also try other snippets likeif/else, do/while, switch, try too.

3. Other small tips
Hold Ctrl + click- Box Selection
Ever had to edit many lines at once? Box Selection makes this very easy
b17623192b9f9f6779f115aac3c325ed
Ctrl + Shift + V - Clipboard ring
Everyone who codes copies and pastes. Have you ever copied A, then by mistake copied B and C, and had to find A to copy it again? Visual Studio keeps the last 20 copies in its clipboard, and Ctrl + Shift + V lets you pick the one you want to paste.
fe4e0a1e8e09f813b9a448dff49198d4
Ctrl + , - Navigate File/Ctrl + ; : Search
987c49fecb17dd097570d1193da5878f
Instead of Ctrl + F, use these 2 features, they work much better
Paste JSON/XML as class
I do not know when Microsoft added this feature to Visual Studio, but it is quite useful. Now that every project uses REST APIs, JSON and XML are everywhere. In C# you cannot use JSON directly, you have to convert it to an object. This feature creates the class to convert the JSON into.
For example, I have this JSON string (from the Facebook Graph API):
1
2
3
4
5
6
7
8
9
10
11
12
{
  "id": "1000017928484843",
  "first_name": "Mạnh Chức",
  "gender": "male",
  "last_name": "Lê",
  "locale": "vi_VN",
  "name": "Lê Mạnh Chức",
  "timezone": 7,
  "updated_time": "2017-02-06T12:56:23+0000",
  "verified": true
}
Just use Paste JSON as class, paste the JSON in, then make the first letter of each field uppercase and you are done. VS is quite smart: it detects some fields as datetime or boolean, and everything else defaults to string.
Wn6mLGm6Hh
This post loads a bit slowly because of the many animated images, sorry about that. I hope you get better and better with your IDE. These hotkey and snippet tricks are great for “showing off your skills” or “impressing new teammates”.
1. Grundlegende Tastenkürzel
Wer viele Tastenkürzel nutzt, wirkt beim Programmierenprofessionellerals andere. Außerdem bewegst du die Maus viel seltener
F5 - Ctrl F5- Debug
Capture
Wenn du fertig bist, drück einfach F5, statt mit der Maus nach oben zu fahren und auf Start zu klicken (sehr einfach, nur für alle, die es noch nicht kennen).
Wenn du Breakpoints zum Debuggen gesetzt hast, starte mit Ctrl + F5 ohne Debugger und überspringe die Breakpoints.
F12 - Implementierung ansehen
7e283a9a8eb79982555108a368bd55c1
Wenn du beim Lesen sehen willst, wie eine Klasse oder Methode geschrieben ist, drück F12. Mit ReSharper oder VS Productivity Tool kannst du auch Ctrl gedrückt halten und auf die Klasse klicken
Ctrl K + Ctrl D - Code formatieren
dc4142a79110417ebf8e261dad97900a
Sieht der Code unordentlich aus, formatiert Ctrl K + Ctrl D ihn automatisch. Klappt das nicht, fehlt wahrscheinlich eine schließende () oder {}.
Ctrl K + Ctrl C - Code auskommentieren /Ctrl K + Ctrl U - Kommentar entfernen
Zum Auskommentieren klickt man meist von Hand auf diese 2 Schaltflächen in der Symbolleiste
Capture
Mit den Tastenkürzeln geht das Ein- und Auskommentieren viel schneller
2579938a9039a675c6e87060f8601ec3
F9 - Breakpoint setzen
Capture
Für einen Breakpoint klickt man meist auf die senkrechte Leiste links. Mit F9 geht es schneller.
F10/F11 - Step over/Step into
Capture2
Beim Debuggen sparen F10 und F11 Zeit gegenüber diesen 2 Schaltflächen in der Symbolleiste.
2. Snippets nutzen
Manche Codestücke schreibt man immer wieder. Visual Studio bringt sie als Snippets mit und spart damit viel Zeit (NetBeans und Eclipse haben das auch).
So nutzt du ein Snippet: Eintippen, dannTab drücken, fertig
ctor - Konstruktor für die Klasse anlegen
4ba583d625ea477f4e2a6e6d487688e3
prop - Property anlegen, danach Tab Tab drücken
7ff4683654699f984f00a56826df2b50
Als ich von Java zu C# wechselte, wunderte ich mich, warum ein so gutes Visual Studio keine Getter und Setter für Felder erzeugt. Später lernte ich, dass Getter und Setter in C# alsproperty geschrieben werden.
cw - Console.WriteLine(), für alle, die viel mit der Konsole arbeiten
60575fb39be324ef353e21227d2f0cee
for/forr - for-Schleife anlegen
89d840b75c4ddc6cde266edeb4046d48
Probier auch andere Snippets aus, zum Beispielif/else, do/while, switch, try und mehr.

3. Weitere kleine Tipps
Ctrl gedrückt halten + klicken- Box Selection
Musstest du schon einmal viele Zeilen auf einmal ändern? Mit Box Selection geht das ganz leicht
b17623192b9f9f6779f115aac3c325ed
Ctrl + Shift + V - Zwischenablage-Ring
Jeder, der programmiert, kopiert und fügt ein. Hast du schon einmal A kopiert, dann aus Versehen B und C kopiert und A wieder suchen müssen? Visual Studio merkt sich die letzten 20 Kopien, und mit Ctrl + Shift + V wählst du die gewünschte aus.
fe4e0a1e8e09f813b9a448dff49198d4
Ctrl + , - Datei finden/Ctrl + ; : Suche
987c49fecb17dd097570d1193da5878f
Statt Ctrl + F lieber diese 2 Funktionen nutzen, sie sind viel hilfreicher
Paste JSON/XML as class
Ich weiß nicht, seit wann Microsoft diese Funktion in Visual Studio hat, aber sie ist sehr praktisch. Heute nutzt jedes Projekt REST-APIs, JSON und XML sind überall. In C# kann man JSON nicht direkt verwenden, sondern muss es in ein Objekt umwandeln. Diese Funktion erzeugt die passende Klasse dafür.
Zum Beispiel habe ich diesen JSON-String (aus der Facebook Graph API):
1
2
3
4
5
6
7
8
9
10
11
12
{
  "id": "1000017928484843",
  "first_name": "Mạnh Chức",
  "gender": "male",
  "last_name": "Lê",
  "locale": "vi_VN",
  "name": "Lê Mạnh Chức",
  "timezone": 7,
  "updated_time": "2017-02-06T12:56:23+0000",
  "verified": true
}
Einfach Paste JSON as class wählen, das JSON einfügen und den ersten Buchstaben jedes Felds großschreiben, fertig. VS ist ziemlich schlau: Manche Felder erkennt es als datetime oder boolean, alles andere wird string.
Wn6mLGm6Hh
Der Beitrag lädt wegen der vielen animierten Bilder etwas langsam, sorry. Viel Erfolg mit deiner IDE. Diese Tastenkürzel und Snippets eignen sich bestens, um „Können zu zeigen“ oder „neue Kollegen zu beeindrucken“.

0 nhận xét:

Đăng nhận xét