Outils pour utilisateurs

Outils du site


issue126:tutoriel2

In the previous article, we have learnt what is tmux, and how to install and start it. Now it is time to begin the real usage of this amazing tool. We are going to learn how to send commands to the program, how to manage different sessions, and how to organize our workspace with windows and panes.

Dans l'article précédent, nous avons appris ce qu'est tmux et comment l'installer et le démarrer. Il est temps, maintenant, de commencer à utiliser réellement cet outil étonnant. Nous allons apprendre comment envoyer des commandes au programme, comment gérer les différentes sessions et comment organiser notre espace de travail avec des fenêtres et des panneaux.

Command tmux The tmux is an application framework that can run several virtual terminals inside. As users, we can run several command-line programs inside these virtual terminals. Therefore it is an essential need to be able to send any commands directly to tmux without disturbing the running virtual terminals and programs. In order to achieve this goal, tmux uses the so-called “command prefix”. Its default value is <Ctrl-b>. Every command that is intended to be sent to tmux, has to be started with this key combination. The exact usage is: • type the prefix combination • release the keys • immediately press the command key. Let's try it with an example. Start tmux and type the following: <Ctrl-b t> (press “Ctrl-b” then press “t”). You have to see a blue digital clock in the middle of the screen. Pressing “Esc” or “Enter” will eliminate the clock. From now on, we will use the “Prefix” word instead of “Ctrl-b”. The benefit of this is to avoid any misunderstanding caused by a further reconfiguration of the Prefix key combination.

La commande tmux

tmux est un framework d'application qui fait tourner plusieurs terminaux virtuels à la fois. Comme utilisateurs, nous pouvons lancer plusieurs programmes en ligne de commande dans ces terminaux virtuels. Par conséquent, il est essentiel de pouvoir envoyer des commandes directement à tmux sans perturber le fonctionnement des terminaux virtuels et des programmes.

Pour y arriver, tmux utilise ce qu'il appelle un « préfixe de commande ». Sa valeur par défaut est <Ctrl-b>. Chaque commande qui est à destination de tmux doit commencer avec ce jeu de touches.

L'utilisation exacte est : • taper la combinaison de touches, • relâcher les touches, • appuyer immédiatement sur la touche de commande.

Essayons avec un exemple. Démarrez tmux et tapez la commande suivante : <Ctrl-b t> (appuyez sur « Ctrl-b » puis appuyez sur « t»). Vous devez voir une horloge digitale bleue au milieu de l'écran. Appuyez sur « Esc » ou « Entrée » pour masquer l'horloge.

À partir de maintenant, nous utiliserons le mot « Préfixe » au lieu de « Ctrl-b ». L'avantage de ceci est d'éviter toute erreur de compréhension due à une reconfiguration ultérieure de la combinaison de touches de Préfixe.

Session handling The most powerful feature of tmux is the session handling. In a normal terminal environment, if you start an application, then you exit from the terminal, the already started job will also be terminated. In tmux, if you start a job, and leave the tmux instead of quit (it is called “detach”), the job will still be running in the background (exactly on the tmux server). Later on, you can join again (this is called “attach”) to this session, and you will find the previously started job running. For session handling, tmux has to be started with the following commands: $ tmux new-session -s Temp or $ tmux new -s Temp

Gestion d'une session

La fonctionnalité la plus puissante de tmux est la gestion des sessions. Dans l'environnement d'un terminal ordinaire, si vous démarrez une application, puis que vous sortez du terminal, le travail déjà démarré va aussi se terminer. Dans tmux, si vous lancez un travail et délaissez tmux plutôt que de le quitter (ça s'appelle « se détacher » (detach)), le travail continue de tourner en tâche de fond (sur le serveur tmux pour être précis). Par la suite, vous pouvez retourner (ça s'appelle « se rattacher » (attach)) dans cette session et vous retrouverez le travail précédemment lancé en train de tourner.

Pour gérer les sessions, tmux doit être démarré avec les commandes suivantes :

$ tmux new-session -s Temp

ou

$ tmux new -s Temp

This will create a new session called Temp, and it will automatically attach to it. Almost everything will be the same as in a normal starting of tmux without session handling, except that the name of the session can be read on the first place of the status bar. The picture shows the previously mentioned digital clock in a named session. If -d is used at the end of the new session command, then the session will be created but it will not attach to it automatically. First of all, run again the clock command <Prefix b>. Now we have an opened session inside tmux. If we were to type the command “exit”, then tmux would terminate this session and we would not be able to attach again. Therefore we have to use the detach command: <Prefix d>. We can see the terminal output: [detached (from session Temp)]

Ceci créera une nouvelle session nommée Temp et il s'attachera automatiquement à elle. Presque tout sera identique au démarrage normal de tmux sans gestion des sessions, à part que le nom de la session peut être lu en première position sur la barre d'état. L'image montre l'horloge mentionnée précédemment, dans une session nommée. Si -d est utilisé à la fin de la commande de la nouvelle session, alors la session sera créée mais elle ne lui sera pas attachée automatiquement.

Tout d'abord, lancez à nouveau la commande de l'horloge, <Préfixe d>. Nous venons d'ouvrir une session dans tmux. Si l'on tapait la commande « exit », tmux terminerait cette session et on ne pourrait pas s'attacher à nouveau. Par conséquent, il faudra utiliser la commande detach : <Prefix d>. Nous pouvons en voir le résultat sur le terminal :

[detached (from session Temp)]

This message means that we have an opened session. In order to list all of the available sessions, just type the following command: $ tmux list-session Temp: 1 windows (created Sat Jun 10 21:38:16 2017) [80×23] The shortened version can also be used (ls). The result indicates that we have one opened session, named “Temp”, with one window (some additional information can be read as well). Now let’s try to attach to this session again: $ tmux attach -t Temp Now we get the same session “Temp” with the already running clock.

Ce message signifie que nous avons une session ouverte. Pour lister toutes les sessions disponibles, il suffit de taper la commande suivante :

$ tmux list-session

Temp: 1 windows (created Sat Jun 10 21:38:16 2017) [80×23]

La version raccourcie peut aussi être utilisée (ls). Le résultat indique que nous avons une session ouverte, nommée « Temp », avec une seule fenêtre (des informations complémentaires peuvent aussi être lues). Maintenant, essayons de rattacher cette session :

$ tmux attach -t Temp

Maintenant, nous retrouvons la même session « Temp » avec l'horloge en fonctionnement.

We can use more sessions as well. Detach from the currently running “Temp” session, start a new one with name “Pmet”, detach from it, and finally get the list of the sessions again: [detached (from session Temp)] $ tmux new -s Pmet [detached (from session Pmet)] $ tmux ls Temp: 1 windows (created Sat Jun 10 21:38:16 2017) [80×23] Pmet: 1 windows (created Sat Jun 10 21:40:42 2017) [80×23] If a session is not needed any more, there are two ways to terminate it. Method (1) can be performed inside tmux, just attach to the required session and type the exit command. Method (2) can be used outside of tmux: $ tmux kill-session -t Temp After killing all of the sessions, we will get the following output of the list-session command: no server running on /tmp/tmux-1000/default That was all about the session handling. Let’s jump to the window handling.

Nous pouvons utiliser aussi des sessions en plus. Détachez la session « Temp » tournant actuellement, lancez-en une nouvelle avec « Pmet » pour nom, détachez-la et, enfin, faites à nouveau la liste des sessions :

[detached (from session Temp)] $ tmux new -s Pmet [detached (from session Pmet)] $ tmux ls Temp: 1 windows (created Sat Jun 10 21:38:16 2017) [80×23] Pmet: 1 windows (created Sat Jun 10 21:40:42 2017) [80×23]

Si une session n'est plus du tout utile, il y a deux façons de la terminer. La méthode (1) peut être exécutée dans tmux, simplement en s'attachant à la session et en tapant la commande exit. La méthode (2) peut être utilisée hors de tmux :

$ tmux kill-session -t Temp

Après avoir tué toutes les sessions, nous obtiendrons la sortie suivante de la commande de liste des sessions :

no server running on /tmp/tmux-1000/default

Ce sera tout sur la gestion des sessions. Passons à la gestion des fenêtres.

Window handling Windows inside a session are very similar to the tabs of a text editor or a web browser. Each and every windows are able to run different (or the same) commands. After detaching from a running session, all of the opened windows will keep running the started application in the background. When we start a new tmux session, the name of the window (the word after the number of the window) will be the currently running application by default, and it is changed every time when we start a new command. Right after starting tmux, it will be the name of our bash. If we start the top application, the window name will be top.

Gestion des fenêtres

Les fenêtres dans une session sont très similaires aux onglets dans un éditeur de texte ou un navigateur Web. Chaque fenêtre est capable de faire tourner des commandes différentes (ou les mêmes). Après avoir été détachées d'une session en cours, toutes les fenêtres ouvertes continueront de faire tourner en tache de fond les applications lancées.

Quand nous commençons une nouvelle session tmux, le nom de la fenêtre (le mot après le numéro de la session) sera par défaut celui de l'application tournant actuellement et il est modifié chaque fois que nous démarrons une nouvelle commande. Juste après avoir lancé tmux, ce sera le nom de notre bash. Si nous démarrons l'application top, le nom de la fenêtre sera top.

In order to avoid the continuous name change, the window name has to be defined explicitly. This can be done in two ways: (a) at the same time when we start the tmux, (b) inside tmux by a command. The method (a) is: $ tmux new -s temp_w -n first This terminal command starts tmux with a session (-s) named temp_w, and with a window (-n) named first. Now, if we run the top command, the name of the window will remain temp_w. The method (b) is to run the following command inside tmux: <Prefix ,> (comma). By performing the command, the status bar will be active and the name of the window can be modified.

Pour éviter un changement permanent de nom, le nom de la fenêtre doit être défini explicitement. Ceci peut être fait de deux manières : (a) en même temps que nous démarrons tmux, (b) dans tmux par une commande.

La méthode (a) est :

$ tmux new -s temp_w -n first

Cette commande de terminal démarre tmux avec une session (-s) nommée temp_w, et avec une fenêtre (-n) nommée first. Maintenant, si nous lançons la commande top, le nom de la fenêtre reste first.

La méthode (b) consiste à lancer la commande suivante dans tmux : <Préfixe ,> (virgule). En exécutant cette commande, la barre d'état sera active et le nom de la fenêtre pourra être modifié.

A brand new window can be created inside tmux with the command <Prefix c>. It has no defined name, but this can be changed by the command mentioned previously. There is no limitation on the amount of windows in tmux. In the picture, three windows have been created inside session temp_w: There are several ways to change between the windows. The active window is always marked with * character after the name of the window in the status bar. The – character signs the previously used window. Method (1) is to use the <Prefix n> command to go to the next window. The command will go through the open windows as a circle, so, after the last one, the first one will be the next. Obviously the <Prefix p> command will go to the previous window.

Une toute nouvelle fenêtre peut être créée dans tmux par la commande <Préfixe c>. Elle n'a pas de nom, mais celui-ci peut être modifié par la commande indiquée précédemment.

Le nombre de fenêtres dans tmux n'est pas limité. Sur l'image, trois fenêtres ont été créées dans la session temp_w.

Il y a plusieurs façons de passer d'une fenêtre à l'autre. La fenêtre active est toujours signalée avec un caractère * après le nom de la fenêtre dans la barre d'état. Le caractère - signale la fenêtre utilisée précédemment. Dans la méthode (1), la commande <Préfixe n> est utilisée pour passer à la fenêtre suivante. La commande passe d'une fenêtre ouverte à l'autre de manière circulaire ; aussi, après la dernière, la première est la suivante. Évidemment, la commande <Préfixe p> permettra de passer à la fenêtre précédente.

All of the windows have a unique identifier - starting with 0. Method (2) is to use the <Prefix w_number> command, where the w_number is the number of the window. Zero-based indexing is familiar to programmers - like array numbering, but later on we will learn how to change it to a one-based index for better usage. Finally, method (3) is to use the <Prefix w> command. This will list all of the available windows as a visual menu, and the desired one can be chosen with the arrow keys. In order to close one window, the exit command can be used. This will terminate the active window without any questions. The safer method is to use the <Prefix &> command. This will request a confirmation in the status bar before killing the active window. Terminating the last window will also close the session.

Chaque fenêtre a un identifiant unique - commençant à 0. Dans la méthode (2), la commande <Préfixe w_number> est utilisée, où w_number est le numéro de la fenêtre. L'indexation à base 0 est bien connue des programmeurs - comme dans la numérotation des matrices -, mais plus loin, nous apprendrons comment changer cela en une indexation à base 1 pour une meilleure utilisation.

Enfin, dans la méthode (3), la commande <Préfixe w> est utilisée. Celle-ci fera la liste de toutes les fenêtres disponibles sous forme d'un menu affiché et celle désirée pourra être choisie avec les touches fléchées.

Pour fermer une fenêtre, la commande exit peut être utilisée. Celle-ci terminera la fenêtre active sans aucune question. La méthode plus sûre est l'utilisation de la commande <Préfixe &>. Celle-ci demandera une confirmation dans la barre d'état avant de tuer la fenêtre active. En fermant la dernière fenêtre, la session sera aussi terminée.

Pane handling The usage of multiple windows is quite useful, but still not the best way to manage different tasks at the same time. If parallel monitoring of the tasks is important, then panes will be our best friend. Let’s start a new session named temp_p. Any window can be split vertically and/or horizontally. For vertical split, use the <Prefix %> command. For horizontal split, use the <Prefix “> (double quote) command. The window looks like in the picture: Now, it is easy to open a vim editor in the left pane to edit a source file of an embedded application, and run the compiler in the right top pane, while flashing of the binary can be monitored in the right bottom pane. Moving between the panes can be performed with two methods. Method (1) is to use the <Prefix o> command, this will go through the panes continuously. Method (2) is to use the <Prefix arrow_keys> command where the arrow_keys can be the Up, Down, Left or Right key.

Gestion des panneaux

L'utilisation de plusieurs fenêtres est vraiment utile, mais ce n'est toujours pas la meilleure méthode pour gérer différentes tâches en même temps. Si la gestion parallèle des tâches est importante, alors les panneaux seront votre meilleur ami. Commençons une nouvelle session nommée temp_p. Chaque fenêtre peut être découpée verticalement et/ou horizontalement. Pour une découpe verticale, utilisez la commande <Préfixe %>. Pour une coupure horizontale, utilisez la commande <Préfixe “> (guillemets doubles). La fenêtre ressemble à celle de l'image :

Maintenant, il est facile d'ouvrir un éditeur vim dans le panneau de gauche pour éditer le fichier source d'une application embarquée et de lancer le compilateur dans le panneau du haut à droite, tandis qu'un flashage de binaire peut être suivi dans le panneau du bas à droite.

Deux méthodes existent pour passer d'un panneau à l'autre. La méthode (1) utilise la commande <Préfixe o>, qui passe d'un panneau à l'autre successivement. La méthode (2) utilise la commande <Préfixe touches fléchées> où les touches fléchées peuvent être les touches Haut, Bas, Gauche ou Droite.

In order to close one pane, the exit command can be used. This will terminate the pane without any questions. The safer method is to use the <Prefix x> command. This will request a confirmation in the status bar before killing the active pane. Terminating the last pane will also close the active window, and, if it is the last window, then the entire session will be closed as well. Conclusion This article covers the most important topics regarding tmux. Now we can use it as our main development environment as programmers, or as the main monitoring environment as server administrators. In the next article, we will learn how to configure tmux via its configuration file. We will change some commands for better usage, and also the visual styling can be modified for pleasant usage. Get Productive! Get tmux!

Pour fermer un panneau, la commande exit peut être utilisée. Celle-ci fermera le panneau sans aucune question. La méthode plus sûre est d'utiliser la commande <Préfixe x>. Celle-ci demandera une confirmation dans la barre d'état avant de tuer le panneau. Le fermeture du dernier panneau fermera aussi la fenêtre active, et, si elle est la dernière fenêtre, la session sera aussi complètement fermée.

Conclusion

Cet article couvre les points les plus importants concernant tmux. Maintenant, nous pouvons l'utiliser comme environnement principal de développement pour les programmeurs, ou comme l'environnement principal de supervision pour les administrateurs de serveurs. Dans le prochain article, nous apprendrons comment configurer tmux via son fichier de configuration. Nous modifierons certaines commandes pour une meilleure utilisation ; le style visuel peut aussi être modifié pour une utilisation plus agréable. Soyez productif ! Utilisez tmux !

Command Reference tmux new-session -s <name> Start a new session with a defined name tmux new -s <name> (Shorter) Start a new session with a defined name tmux new -s <name> -n <name> (Shorter) Start a new session and window with a defined name tmux attach -t <name> Join to an already opened session with a defined name tmux list-session List all of the opened sessions

Référentiel des commandes

tmux new-session -s <name> Démarre une nouvelle session avec un nom précisé.

tmux new -s <name> (Plus court.) Démarre une nouvelle session avec un nom précisé.

tmux new -s <name> -n <name> (Plus court.) Démarre une nouvelle session et une nouvelle fenêtre avec des noms précisés.

tmux attach -t <name> Aller dans une session déjà ouverte dont le nom est défini.

tmux list-session Lister toutes les sessions ouvertes.

tmux ls (Shorter) List all of the opened sessions tmux kill-session -t <name> Terminate (destroy) an already opened session Ctrl-B & Command Prefix Prefix t Show the digital clock in the middle of the screen Prefix c Create a new window Prefix , Rename a window in the status bar

tmux ls (Plus court.) Lister toutes les sessions ouvertes.

tmux kill-session -t <name> Terminer (détruire) une session déjà ouverte.

Ctrl-B & le Préfixe de commande.

Préfixe t Afficher l'horloge digitale au milieu de l'écran.

Préfixe c Créer une nouvelle fenêtre.

Préfixe , Renommer une fenêtre dans la barre d'état.

Prefix n Go to the next window Prefix p Go to the previous window Prefix w_number Go to the w_number window Prefix w List the opened windows as a visual menu Prefix & Close the active window with confirmation

Préfixe n Passer à la fenêtre suivante.

Préfixe p Passer à la fenêtre précédente.

Préfixe w_number Aller à la fenêtre w_number.

Préfixe w Lister les fenêtres ouvertes dans un menu affiché.

Préfixe & Fermer la fenêtre active avec confirmation.

Prefix % Split the active window vertically Prefix “ Split the active window horizontally Prefix o Go to the next pane Prefix arrow_key Go to the desired pane Prefix x Close the active pane with confirmation

Préfixe % Couper la fenêtre active en deux verticalement.

Préfixe “ Couper la fenêtre active en deux horizontalement.

Préfixe o Passer sur le panneau suivant.

Préfixe touche fléchée Passer sur le panneau souhaité.

Préfixe x Fermer le panneau actif avec confirmation.

issue126/tutoriel2.txt · Dernière modification : 2017/11/11 15:32 de andre_domenech